鉴于以下XML和XSL转换,我期望输出中包含文本first
和second
的两个<node/>
元素。此预期基于以下推理:XSL模板处理所有后代,而不仅仅是儿童
- 将首先执行带有
match="/root"
的模板。该模板中的<xsl:apply-templates/>
指令将应用最具体的模板for all child nodes。 - 这些
<foo/>
子节点将由第二个模板处理。该模板还包含<xsl:apply-templates/>
指令,该指令将模板应用于这些节点的子节点。 <foo/>
节点只包含<bar/>
节点,它们不匹配任何模板。这意味着没有更多的节点会被处理。
XML
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<root>
<foo name="first">
<bar>
<foo name="nested"/>
</bar>
</foo>
<foo name="second">
<bar>
<baz>
<foo name="nested"/>
</baz>
</bar>
</foo>
</root>
XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/root">
<output>
<xsl:apply-templates/>
</output>
</xsl:template>
<xsl:template match="foo">
<node>
<xsl:value-of select="@name"/>
</node>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
然而,上述变换的实际结果包括四个<node/>
元件,其中两个包含文本nested
。这意味着第二个模板也适用于最内层的节点。但是根据上述步骤3,处理引擎不应该到达这些节点。
我测试了多个Web浏览器中的转换,并使用Notepad ++ XML Tools插件,并且它们都显示“错误”结果。所以我想我错了,但是我的推理有什么问题?
你是对的,我不知道[内置模板](http://www.w3.org/TR/xslt#built-in-rule)。谢谢。 – 2010-07-20 08:07:40