XSL 中的“调用模板"和“应用模板"之间有什么区别? [英] What are the differences between 'call-template' and 'apply-templates' in XSL?

查看:34
本文介绍了XSL 中的“调用模板"和“应用模板"之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 新手,所以我对这两个标签有点困惑,

I am new in XSLT so I'm little bit confused about the two tags,

<xsl:apply-templates name="nodes">

<xsl:call-template select="nodes"> 

那么你能列出它们之间的区别吗?

So can you list out the difference between them?

推荐答案

与调用传统编程语言中的函数非常接近.

<xsl:call-template> is a close equivalent to calling a function in a traditional programming language.

您可以在 XSLT 中定义函数,就像这个输出字符串的简单函数一样.

You can define functions in XSLT, like this simple one that outputs a string.

<xsl:template name="dosomething">
  <xsl:text>A function that does something</xsl:text>
</xsl:template>

这个函数可以通过调用.

This function can be called via <xsl:call-template name="dosomething">.

<xsl:apply-templates> 有点不同,它是 XSLT 真正的力量所在:它采用任意数量的 XML 节点(无论您在 select 属性),处理它们中的每一个(不一定按任何预定义的顺序),有人可以说 apply-templates 像循环一样工作,但事实并非如此,因为节点可以按任何顺序处理,即使并行,并为它们找到匹配的模板:

<xsl:apply-templates> is a little different and in it is the real power of XSLT: It takes any number of XML nodes (whatever you define in the select attribute), processes each of them (not necessarily in any predefined order), somebody could say that apply-templates works like a loop, but this is not exactly the case, as the nodes may be processed in any order, even in parallel, and finds matching templates for them:

<!-- sample XML snippet -->
<xml>
  <foo /><bar /><baz />
</xml>

<!-- sample XSLT snippet -->
<xsl:template match="xml">
  <xsl:apply-templates select="*" /> <!-- three nodes selected here -->
</xsl:template>

<xsl:template match="foo"> <!-- will be called once -->
  <xsl:text>foo element encountered</xsl:text>
</xsl:template>

<xsl:template match="*"> <!-- will be called twice -->
  <xsl:text>other element countered</xsl:text>
</xsl:template>

通过这种方式,您放弃了对 XSLT 处理器的一些控制 - 不是您决定程序流的去向,而是处理器通过为它当前正在处理的节点找到最合适的匹配来完成.

This way you give up a little control to the XSLT processor - not you decide where the program flow goes, but the processor does by finding the most appropriate match for the node it's currently processing.

如果多个模板可以匹配一个节点,则具有更具体匹配表达式的模板获胜.如果存在多个具有相同特异性的匹配模板,则最后声明的模板获胜.

If multiple templates can match a node, the one with the more specific match expression wins. If more than one matching template with the same specificity exist, the one declared last wins.

您可以将更多精力集中在开发模板上,而花更少的时间来做管道".您的程序将变得更强大、更模块化、嵌套更少、速度更快(因为 XSLT 处理器针对模板匹配进行了优化).

You can concentrate more on developing templates and need less time to do "plumbing". Your programs will become more powerful and modularized, less deeply nested and faster (as XSLT processors are optimized for template matching).

使用 XSLT 理解的一个概念是当前节点"的概念.使用 当前节点会随着每次迭代而移动,而 不会改变当前节点.IE.被调用模板中的 . 引用与调用模板中的 . 相同的节点.应用模板不是这种情况.

A concept to understand with XSLT is that of the "current node". With <xsl:apply-templates> the current node moves on with every iteration, whereas <xsl:call-template> does not change the current node. I.e. the . within a called template refers to the same node as the . in the calling template. This is not the case with apply-templates.

这是基本的区别.模板还有一些其他方面会影响它们的行为:它们的 modepriority,事实上模板可以同时具有 name 和 <代码>匹配.它也会影响模板是否已导入 ().这些是高级用途,您可以在到达那里后处理它们.

This is the basic difference. There are some other aspects of templates that affect their behavior: Their mode and priority, the fact that templates can have both a name and a match. It also has an impact whether the template has been imported (<xsl:import>) or not. These are advanced uses and you can deal with them when you get there.

这篇关于XSL 中的“调用模板"和“应用模板"之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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