I am currently trying to copy out an XML document into a new one and renaming some of the nodes as I go. My issue however is that my templates are not being hit and only the global one is:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
and my specific template is (adapted for below):
<xsl:template match="F">
<G>
<xsl:apply-templates/>
</G>
</xsl:template>
The xml structure is similar to this however adapted due to the nature of the work 🙂
<T>
<A>
<B>
</B>
<C>
</C>
</A>
<D>
<E>Data</E>
<F>Data</F>
</D>
</T>
When running this through in VS2010 I can see it hit the line of F on the XML however is still applies the general template and not the more specific one. Any ideas why this would be?
This OP has conceiled the most important fact about the XML document — as he tells us in a later comment, “The XML uses a namespace” …
Here is a solution to this so late-clarified question:
when this transformation is applied on the following XML document (based on the namespace the OP admitted…):
the wanted, correct result is produced:
Explanation:
The problem in the original code is here:
This template matches an element
Fthat is in no namespace. However all elements of the real XML document are in some undisclosed namespace — not in no namespace. Therefore, the above template cannot match anFelement in no namespace, because no such (Felement in no namespace) exists.The solution is to define the same namespace in the XSLT transformation and to have the template match an
Felement in that namespace.