为什么XSLT变换不期望的输出
问题描述:
环境: XSLT 1.0为什么XSLT变换不期望的输出
预期输出:两者Level1和Level2文本输出
:仅用于第2级元件实际输出的文本节点
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>
答
处理开始于文档节点/
你有没有模板,以便用于其built-in template的其中一个确实<xsl:apply-templates/>
,即处理所有子节点而你的情况是data
元素由您的模板match="/*"
匹配,其中处理/data/section1
您没有模板,所以使用内置的<xsl:apply-templates/>
,即处理所有子节点section1
,其中包括level1
元素,您有模板,所以内置是用于处理所有子节点,对于那些文本节点再次没有模板,内置文本节点将它们复制到输出。
SECTION1有Level1和Level2的元素,虽然我只希望level2的 – Rod
所以,或许我的选择应该是这样的:'选择=“/数据/ SECTION1/Level2的”' – Rod
是,在简单的情况下,你已经有了' ''最好把它改成' '。另一个解决方案是确保'level1'元素不会产生任何输出。 ''。 –