Can't match namespace nodes with <xsl:template match>

Here should go questions about transforming XML with XSLT and FOP.
bbaker
Posts: 6
Joined: Fri Jul 07, 2006 10:34 pm

Can't match namespace nodes with <xsl:template match>

Post by bbaker »

I'm doing an XML to XML conversion using the identiy transformation code piece first before processing the namespace nodes. For some reason the sky:datasource tags aren't being matched at all. If I remove the sky: namespace, then I can match them after also removing it from the XSL style sheet. In the sample below i'm trying to add an element called <sky:testnode> inside the <sky:datasource> node(s). When I run the XSL everything is copied and passed through, but the new element is never added.

Here is my XML:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="NamespaceMatching.xsl"?>
<sky:item xmlns:sky="http://tempuri.com/item">
<sky:datasource id="LikertScale">
<sky:dataitem text="Unimportant" value="0"/>
<sky:dataitem text="Don't Care" value="1"/>
<sky:dataitem text="Important" value="2"/>
</sky:datasource>
<sky:datasource id="LikertScale2">
<sky:dataitem text="Unimportant" value="3"/>
<sky:dataitem text="Don't Care" value="4"/>
<sky:dataitem text="Important" value="5"/>
</sky:datasource>
</sky:item>
Here is my XSL stylesheet:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sky="http://ctlt.wsu.edu/skylight/sky">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- the standard identity transformation -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="sky:datasource">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:element name="sky:testnode"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
After the new element is copied, I need strip out the unwanted xmlns:sky attribute that was added inside the new node.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

The namespace is not sky, sky is a prefix and it is used to refer to the namespace it is bound to. The problem is that you have sky mapped to different namespaces in your document it is mapped to http://tempuri.com/item while in your stylesheet you map it to http://ctlt.wsu.edu/skylight/sky. If you change for instance in the stylesheet to replace

xmlns:sky="http://ctlt.wsu.edu/skylight/sky"

with

xmlns:sky="http://tempuri.com/item"

then you should get the behaviour you expect.

Best Regards,
George
bbaker
Posts: 6
Joined: Fri Jul 07, 2006 10:34 pm

Works!

Post by bbaker »

Thanks! It appears to be working correctly now. Big help.
Post Reply