(: to produce legal and validable XHTML ... :)
declare option saxon:output "method=xml";
declare option saxon:output "doctype-public=-//W3C//DTD XHTML 1.0 Strict//EN";
declare option saxon:output "doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
declare option saxon:output "indent=yes";
(: remove all whitespace nodes from elements of the input tree :)
declare function local:strip-space($element){
element {local-name($element)} {
$element/@*,
for $child in $element/node()
return if ($child instance of text() and normalize-space($child)='')
then ()
else if ($child instance of element())
then local:strip-space($child)
else $child
}
};
declare function local:root($root,$uri){
HTML compaction of "{replace($uri,'.*/(.*)','$1')}"
};
declare function local:attribute($attribute){
concat(" ",local-name($attribute),'="',$attribute,'"')
};
declare function local:element($element){
{local-name($element)}
{for $attr in $element/@*
return local:attribute($attr),
if (count($element/*)=0) (: single text node ?:)
then concat(" ",data($element))
else {
for $child in $element/(*|text()) (: possible mixed node :)
return if($child instance of element())
then local:element($child)
else local:text($child)
}
}
};
declare function local:text($text){
{$text}
};
(: change the namespace of a subtree starting at an element
adapted from Priscilla Walmsley, XQuery, O'Reilly, p. 258 :)
declare function local:change-element-ns-deep($element,$new-ns){
element {QName($new-ns,local-name($element))} {
$element/@*,
for $child in $element/node()
return if ($child instance of element())
then local:change-element-ns-deep($child,$new-ns)
else $child
}
};
local:change-element-ns-deep( (: put the whole tree in the XHTML namespace :)
local:root(local:strip-space(/*),document-uri(/)),
"http://www.w3.org/1999/xhtml")