<xsl:apply-template>和<xsl:call-template>之间的区别?

Bab*_*Bst 6 xml xslt

请你给我解释一下之间的区别<xsl:apply-template>,并<xsl:call-template>当我应该使用<xsl:call-template>
谢谢

Emm*_*ows 12

在最基本的层面上,<xsl:apply-templates>当您希望让处理器自动处理节点<xsl:call-template/>时使用,并在您希望更好地控制处理时使用.所以如果你有:

<foo>
    <boo>World</boo>
    <bar>Hello</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)

你有以下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>
Run Code Online (Sandbox Code Playgroud)

你会得到结果WorldHello.从本质上讲,你已经说过"以这种方式处理bar和boo",然后你就让XSLT处理器处理这些节点.在大多数情况下,这就是你应该在XSLT中做的事情.

但有时候,你想做一些比较漂亮的事情.在这种情况下,您可以创建一个与任何特定节点都不匹配的特殊模板.例如:

<xsl:template name="print-hello-world">
    <xsl:value-of select="concat( bar, ' ' , boo )" />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在处理时调用此模板,<foo>而不是自动处理foo子节点:

<xsl:template match="foo">
    <xsl:call-template name="print-hello-world"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

在这个特殊的人工示例中,您现在可以获得"Hello World",因为您已经覆盖了默认处理以执行自己的操作.

希望有所帮助.