[xquery-talk] Generating xhtml from xml in no namespace

Jesper Tverskov jesper.tverskov at gmail.com
Thu Dec 18 14:15:58 PST 2008

As has been said, there are many more or less cumbersome ways to
output XHTML from XML input in no namespace, most of them bad in my
opinion because they smell to much of fancy work-arounds and not that
easy to reuse.

The only correct way, I feel, is to "use the long form of element
constructor to put the namespace in each time", as Carlisle formulated
it. I tried it out, it works, naturally, but it is a hell of a lot

So I came up with the following method, nice because there is some
natural logic to it, now when it not possible to do it directly. Also,
most of all, it is nice because it is easy to reuse.

1) Declare the following change-namespace function  (borrowed
unchanged from Priscilla Walmsley):

declare namespace functx = "http://www.functx.com";
declare function functx:change-element-ns-deep
($element as element(), $newns as xs:string) as element() {
let $newName := QName($newns, local-name($element))
return (element {$newName}
for $child in $element/node()
return if ($child instance of element())
then functx:change-element-ns-deep($child, $newns)
else $child

2) Create a variable $x and put good old XHTML output, build the
literal way, into it. The only thing that is missing is
xmlns="http://www.w3.org/1999/xhtml" in the html element.

3) Now use the following:

functx:change-element-ns-deep($x, "http://www.w3.org/1999/xhtml")

Jesper Tverskov

More information about the talk mailing list