简体   繁体   English

之间的差异 <xsl:apply-template> 和 <xsl:call-template> ?

[英]difference between <xsl:apply-template> and <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> ? 您能给我解释一下<xsl:apply-template><xsl:call-template>之间的区别以及我什么时候应该使用<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. 在非常基本的级别上,如果要让处理器自动处理节点,请使用<xsl:apply-templates> ,而要对处理进行更好的控制时,请使用<xsl:call-template/> So if you have: 因此,如果您有:

<foo>
    <boo>World</boo>
    <bar>Hello</bar>
</foo>

And you have the following XSLT: 并且您具有以下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 . 您将得到结果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. 本质上,您说过“以这种方式处理bar和boo”,然后让XSLT处理器处理它们遇到的这些节点。 In most cases, this is how you should do things in XSLT. 在大多数情况下,这就是您应该在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: 然后,您可以在处理<foo>时调用此模板,而不是自动处理foo的子节点:

<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. 在这个特殊的人工示例中,您现在会得到“ Hello World”,因为您已经覆盖了默认处理以完成您自己的事情。

Hope that helps. 希望能有所帮助。

would you please explain me the difference between <xsl:apply-template> and <xsl:call-template> and when should i use <xsl:call-template> ? 您能给我解释一下<xsl:apply-template><xsl:call-template>之间的区别以及我什么时候应该使用<xsl:call-template>吗?

One can use <xsl:call-template> but almost never should . 可以使用<xsl:call-template>但几乎从来没有使用

It is in the spirit of XSLT to allow the XSLT processor to determine exactly which template best matches a node and to decide to use this template for processing that node. XSLT的精神是允许XSLT处理器确切确定哪个模板某个节点最匹配,并决定使用此模板来处理该节点。 This gives us clean, easy and powerful extensibility and polymorphism. 这为我们提供了干净,轻松且强大的可扩展性和多态性。

In general, comparing xsl:apply-templates to xsl:call-template is similar to comparing the invoking of a virtual method from a base class to that of calling a non-virtual method directly. 通常,将xsl:apply-templatesxsl:call-template进行比较类似于将虚拟方法从基类的调用与直接调用非虚拟方法的调用进行比较。

Here are some important differences : 以下是一些重要的区别

  1. xsl:apply-templates is much richer and deeper than xsl:call-templates and even from xsl:for-each , simply because we don't know what code will be applied on the nodes of the selection -- in the general case this code will be different for different nodes of the node-list. xsl:apply-templatesxsl:call-templates甚至从xsl:for-each都更加丰富和深入,这仅仅是因为我们不知道将在选择的节点上应用什么代码-通常情况下,对于节点列表的不同节点,代码将有所不同。

  2. The code that will be applied can be written way after the xsl:apply-templates was written and by people that do not know the original author. 可以在编写xsl:apply-templates以及由不认识原始作者的人来编写将要应用的代码

The FXSL library 's implementation of higher-order functions (HOF) in XSLT wouldn't be possible if XSLT didn't have the <xsl:apply-templates> instruction. 如果XSLT没有<xsl:apply-templates>指令,则FXSL库无法在XSLT中实现高阶函数(HOF)

Summary : Templates and the <xsl:apply-templates> instruction is how XSLT implements and deals with polymorphism. 摘要 :模板和<xsl:apply-templates>指令是XSLT如何实现和处理多态性的方法。 One can and should avoid using xsl:call-template , which doesn't allow polimorphism and limits reusability and flexibility. 可以并且应该避免使用xsl:call-template ,它不允许多态性并限制了可重用性和灵活性。

Reference : See this whole thread: http://www.stylusstudio.com/xsllist/200411/post60540.html 参考 :请参见整个主题: http : //www.stylusstudio.com/xsllist/200411/post60540.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM