On a very basic level, you use <xsl:apply-templates> when you want to let the processor handle nodes automatically, and you use <xsl:call-template/> when you want finer control over the processing. So if you have:
<foo>
<boo>World</boo>
<bar>Hello</bar>
</foo>
And you have the following 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>
You will get the result WorldHello. Essentially, you've said "handle bar and boo this way" and then you've let the XSLT processor handle these nodes as it comes across them. In most cases, this is how you should do things in XSLT.
Sometimes, though, you want to do something fancier. In that case, you can create a special template that doesn't match any particular node. For example:
<xsl:template name="print-hello-world">
<xsl:value-of select="concat( bar, ' ' , boo )" />
</xsl:template>
And you can then call this template while you're processing <foo> rather than automatically processing foo's child nodes:
<xsl:template match="foo">
<xsl:call-template name="print-hello-world"/>
</xsl:template>
In this particular artificial example, you now get "Hello World" because you've overriden the default processing to do your own thing.
本文介绍了XSLT中<xsl:apply-templates>与<xsl:call-template>的区别及其使用场景。通过示例说明了<xsl:apply-templates>适用于自动处理节点,而<xsl:call-template>则提供更精细的控制,允许创建特殊模板以实现更复杂的功能。

3535

被折叠的 条评论
为什么被折叠?



