<xsl:apply-template> 之间的区别和 <xsl:call-template>? [英] difference between <xsl:apply-template> and <xsl:call-template>?

查看:26
本文介绍了<xsl:apply-template> 之间的区别和 <xsl:call-template>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下之间的区别,我应该什么时候使用 ?
谢谢

would you please explain me the difference between<xsl:apply-template> and <xsl:call-template> and when should i use <xsl:call-template> ?
thank you

推荐答案

在非常基础的层面上,当您想让处理器自动处理节点时,您可以使用 ,当您想要更好地控制处理时,您可以使用 .所以如果你有:

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>

您有以下 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 中应该如何做的事情.

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>

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

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>

在这个特殊的人工示例中,您现在得到了Hello World",因为您已经覆盖了默认处理来做自己的事情.

In this particular artificial example, you now get "Hello World" because you've overriden the default processing to do your own thing.

希望有所帮助.

这篇关于&lt;xsl:apply-template&gt; 之间的区别和 &lt;xsl:call-template&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆