为什么XSLT变换不是预期的输出



环境: XSLT 1.0

预期输出:仅级别2元素的文本节点

实际输出: latve1和Level2文本输出

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>

处理从文档节点/开始,您没有模板,因此使用了一个内置模板,即执行<xsl:apply-templates/>,即处理所有儿童节点,在您的情况下是data由您的模板 match="/*"匹配的元素,该元素是处理您没有模板的/data/section1,因此使用了 <xsl:apply-templates/>的内置元素,即处理 section1的所有子节点,其中包括您具有模板的 level1元素哪个处理所有子节点,对于这些文本节点,您没有模板,并且文本节点的内置为输出。

最新更新