RE: [xsl] Coalesce namespaces for XHTML

Subject: RE: [xsl] Coalesce namespaces for XHTML
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 9 Feb 2009 21:29:54 -0000
xsl:copy-of produces an exact copy, namespace prefixes and all.

If you want to change prefixes, you need to use xsl:element as in your
example.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Adam Retter [mailto:adam.retter@xxxxxxxxxxxxxx] 
> Sent: 09 February 2009 21:00
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Coalesce namespaces for XHTML
> 
> Hi there,
> 
> I am trying to generate XHTML content that can be delivered 
> as text/html, which means that all elements must be in the 
> default namespace of xmlns="http://www.w3.org/1999/xhtml";
> 
> I have some source XML documents that contain some XML in my 
> own namespace and also some XHTML snippets, I wish to copy 
> these XHTML snippets into my output document, a simple 
> example follows -
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <o:a xmlns:o="http://other"; xmlns:xh="http://www.w3.org/1999/xhtml";>
>     <o:b>
>         <xh:a href="http://somewebsite.com"; title="some other 
> website">some website</xh:a>
>     </o:b>
> </o:a>
> 
> 
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="2.0" xmlns:o="http://other";
> xmlns:xh="http://www.w3.org/1999/xhtml";
> xmlns="http://www.w3.org/1999/xhtml";>
>     <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
> doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
> media-type="text/html" method="xhtml"/>
> 
>     <xsl:template match="o:a">
>         <html>
>             <body>
>                 <xsl:apply-templates/>
>             </body>
>         </html>
>     </xsl:template>
> 
>     <xsl:template match="o:b">
>         <xsl:apply-templates/>
>     </xsl:template>
> 
>     <xsl:template match="xh:a">
>         <xsl:copy-of select="."/>
>     </xsl:template>
> 
> </xsl:stylesheet>
> 
> 
> 
> 
> Which generates the following output (Saxon 9.1.0.3) -
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html
>   PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml"; xmlns:o="http://other";
> xmlns:xh="http://www.w3.org/1999/xhtml";>
>    <body>
> 
>       <xh:a href="http://somewebsite.com"; title="some other 
> website">some website</xh:a>
> 
> 
>    </body>
> </html>
> 
> 
> Now it all looks fine apart from the copied elements have 
> kept their namespace prefix of xh: and not been places in the 
> default namespace.
> The xh namespace and the default namespace are actually the 
> same. In case thats not very clear, the output I wish to 
> generate would look like this -
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html
>   PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml"; xmlns:o="http://other";
> xmlns:xh="http://www.w3.org/1999/xhtml";>
>    <body>
> 
>       <a href="http://somewebsite.com"; title="some other 
> website">some website</a>
> 
> 
>    </body>
> </html>
> 
> 
> I can achieve that if I replace the template that matches 
> xh:a with the following -
> 
> <xsl:template match="xh:a">
>         <xsl:element name="{local-name()}">
>             <xsl:copy-of select="@*|text()"/>
>         </xsl:element>
>     </xsl:template>
> 
> However it does not seem like a good solution - is there a 
> better way of achieving this?
> 
> Thanks Adam.
> 
> 
> --
> Adam Retter

Current Thread