在非常基本的层面上,您可以使用<xsl:apply-templates>
当你想让处理器自动处理节点时,你使用<xsl:call-template/>
当您想要更好地控制处理时。所以如果你有:
<foo>
<boo>World</boo>
<bar>Hello</bar>
</foo>
并且您有以下 XSLT:
<xsl:template match="foo">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="bar">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="boo">
<xsl:value-of select="."/>
</xsl:template>
你会得到结果WorldHello
。本质上,您已经说过“以这种方式处理 bar 和 boo”,然后让 XSLT 处理器在遇到这些节点时处理这些节点。在大多数情况下,这就是您应该在 XSLT 中执行操作的方式。
但有时,您想做一些更奇特的事情。在这种情况下,您可以创建一个不与任何特定节点匹配的特殊模板。例如:
<xsl:template name="print-hello-world">
<xsl:value-of select="concat( bar, ' ' , boo )" />
</xsl:template>
然后您可以在处理时调用此模板<foo>
而不是自动处理foo
的子节点:
<xsl:template match="foo">
<xsl:call-template name="print-hello-world"/>
</xsl:template>
在这个特定的人工示例中,您现在得到“Hello World”,因为您已经覆盖了默认处理来执行您自己的操作。
希望有帮助。