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

查看:138
本文介绍了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>非常类似于以传统编程语言调用函数.

<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>

可以通过<xsl:call-template name="dosomething">调用此函数.

<xsl:apply-templates>稍有不同,它是XSLT的真正功能:它需要任意数量的XML节点(无论您在select属性中定义了什么),都对其进行迭代(这很重要:应用-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), iterates them (this is important: apply-templates works like a loop!) 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可以理解的概念是当前节点"的概念.对于<xsl:apply-templates>,当前节点将在每次迭代时继续前进,而<xsl:call-template>不会更改当前节点. 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,即模板可以同时具有namematch的事实.模板是否已导入(<xsl:import>)也都会产生影响.这些是高级用途,您可以在到达那里时对其进行处理.

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天全站免登陆