[xsl] Coalesce namespaces for XHTML

Subject: [xsl] Coalesce namespaces for XHTML
From: Adam Retter <adam.retter@xxxxxxxxxxxxxx>
Date: Mon, 9 Feb 2009 21:00:00 +0000
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