XSL order of templates for nested elements



I have an XML file like so:



<text>
<a>foo1</a>
<a><b>foo2</b></a>
</text>


I have an XSL file designed to process <a> and <a><b> differently with templates 1 and 2:



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="text">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<!-- t1 -->
<xsl:template match="a">
<xsl:element name="keep">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

<!-- t2 -->
<xsl:template match="a/b" />

</xsl:stylesheet>


I expected that this would produce:



<text>
<keep>foo1</keep>
</text>


Because t2 should match and 'ignore' <a><b>foo2</a></b> and I thought that it would take precedence over t1 (both t1 and t2 match <a><b> but t2 is simply later in the XSL). But the output is in fact:



<text>
<keep>foo1</keep>
<keep>foo2</keep>
</text>


In fact if I take t2 away the output is the same, so clearly it is not even matching <a><b>. I must be missing something: please can anyone help?


No comments:

Post a Comment