0
votes

Environment: XSLT 1.0

Expected output: Only the text node for level 2 element

Actual output: Both level1 and level2 text outputs

xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text"/>

  <xsl:template match="/*">
    <xsl:apply-templates select="/data/section1" />
  </xsl:template>

  <xsl:template match="level2">
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="SelectingAndMatching.xslt"?>

<data>
  <section1>
    <level1>hello world 1</level1>
    <level2>unlocked achievement level 2</level2>
  </section1>
  <section2>
    <product1></product1>
    <product2></product2>
    <product3></product3>
  </section2>
</data>
1

1 Answers

2
votes

Processing starts at the document node / for which you have no template so one of the built-in template is used which does <xsl:apply-templates/>, i.e. processes all child nodes which in your case is the data element matched by your template match="/*" which processes /data/section1 for which you have no template so the built-in one is used which does <xsl:apply-templates/>, i.e. processes all child nodes of section1 which includes level1 elements for which you have template so the built-in is used which processes all child nodes, for those text nodes again you have no template and the built-in for text nodes copies them to the output.