xsl-list
[Top] [All Lists]

RE: [xsl] Coalesce namespaces for XHTML

2009-02-09 16:30:25

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(_dot_)retter(_at_)googlemail(_dot_)com] 
Sent: 09 February 2009 21:00
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>