“调用模板”和“调用模板”之间有什么区别?和“应用模板”在 XSL 中?

发布于 2024-10-08 17:23:23 字数 200 浏览 1 评论 0原文

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

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

那么

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

您能列出它们之间的区别吗?

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

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

and

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

So can you list out the difference between them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酷遇一生 2024-10-15 17:23:23

与调用传统编程语言中的函数非常等效。

您可以在 XSLT 中定义函数,例如这个输出字符串的简单函数。

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

可以通过 调用此函数。

略有不同,这就是 XSLT 的真正威力:它需要任意数量的 XML 节点(无论您在 select 中定义什么)属性),处理它们中的每一个(不一定以任何预定义的顺序),有人可能会说 apply-templates 的工作方式就像一个循环,但事实并非如此,因为节点可以按任何顺序处理,甚至是并行处理,并为它们找到匹配的模板:

<!-- 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 处理器的一点控制 - 不是由您决定程序流的走向,而是由处理器为它当前正在处理的节点找到最合适的匹配。

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

您可以更加专注于开发模板,而需要更少的时间来进行“管道疏通”。您的程序将变得更强大、模块化、嵌套深度更少且速度更快(因为 XSLT 处理器针对模板匹配进行了优化)。

XSLT 需要理解的一个概念是“当前节点”。使用 时,当前节点会在每次迭代时继续移动,而 不会更改当前节点。即被调用模板中的. 引用与调用模板中的. 相同的节点。 apply-templates 的情况并非如此。

这是基本的区别。模板还有一些其他方面会影响其行为:它们的模式优先级,模板可以同时具有名称和<代码>匹配。模板是否已导入 () 也会产生影响。这些都是高级用途,您可以在到达那里后处理它们。

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

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

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.

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

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.

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.

各空 2024-10-15 17:23:23

添加到 @Tomalak 的好答案:

这里有一些未提及的重要差异

  1. xsl:apply-templates

    xsl:apply-templates 更加丰富和深入code>xsl:call-templates 甚至来自 xsl:for-each 只是因为我们不知道什么代码将应用于
    选择——在一般情况下,此代码将有所不同
    节点列表的不同节点。

  2. 将应用的代码
    可以在编写xsl:apply templates之后编写
    并通过
    不知道原作者的人。

FXSL 库不可能在 XSLT 中实现高阶函数 (HOF)< /strong> 如果 XSLT 没有 指令。

摘要:模板和 指令是 XSLT 实现和处理多态性的方式。

参考:查看整个帖子:http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200411/msg00546.html

To add to the good answer by @Tomalak:

Here are some unmentioned and 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.

  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.

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.

Summary: Templates and the <xsl:apply-templates> instruction is how XSLT implements and deals with polymorphism.

Reference: See this whole thread: http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200411/msg00546.html

一刻暧昧 2024-10-15 17:23:23

xsl:apply-templates 通常(但不一定)用于处理具有所有适用模板的当前节点的全部或子集。这支持 XSLT 应用程序的递归性,该应用程序与已处理的 XML 的(可能的)递归性相匹配。

另一方面,xsl:call-template 更像是普通的函数调用。您只执行一个(命名的)模板,通常带有一个或多个参数。

因此,如果我想拦截感兴趣的节点的处理并(通常)将某些内容注入到输出流中,我会使用 xsl:apply-templates。一个典型的(简化的)示例是,

<xsl:template match="foo">
  <bar>
    <xsl:apply-templates/>
  </bar>
</xsl:template>

而使用 xsl:call-template 我通常会解决诸如将某些子节点的文本添加在一起、将选择的节点集转换为文本或其他节点集等问题 - 任何您想要的问题编写一个专门的、可重用的函数。

编辑:

作为对您的特定问题文本的附加说明:

<xsl:call-template name="nodes"/> 

这调用了一个名为“nodes”的模板:

    <xsl:template name="nodes">...</xsl:template>

这与以下语义不同:

<xsl:apply-templates select="nodes"/>

...它将所有模板应用于名称为“nodes”的当前 XML 节点的所有子节点'。

xsl:apply-templates is usually (but not necessarily) used to process all or a subset of children of the current node with all applicable templates. This supports the recursiveness of XSLT application which is matching the (possible) recursiveness of the processed XML.

xsl:call-template on the other hand is much more like a normal function call. You execute exactly one (named) template, usually with one or more parameters.

So I use xsl:apply-templates if I want to intercept the processing of an interesting node and (usually) inject something into the output stream. A typical (simplified) example would be

<xsl:template match="foo">
  <bar>
    <xsl:apply-templates/>
  </bar>
</xsl:template>

whereas with xsl:call-template I typically solve problems like adding the text of some subnodes together, transforming select nodesets into text or other nodesets and the like - anything you would write a specialized, reusable function for.

Edit:

As an additional remark to your specific question text:

<xsl:call-template name="nodes"/> 

This calls a template which is named 'nodes':

    <xsl:template name="nodes">...</xsl:template>

This is a different semantic than:

<xsl:apply-templates select="nodes"/>

...which applies all templates to all children of your current XML node whose name is 'nodes'.

甲如呢乙后呢 2024-10-15 17:23:23

功能确实相似(除了调用语义,其中 call-template 需要 name 属性和相应的名称模板)。

但是,解析器不会以相同的方式执行。

来自 MSDN

不同, 不会更改当前节点或当前节点列表。 p>

The functionality is indeed similar (apart from the calling semantics, where call-template requires a name attribute and a corresponding names template).

However, the parser will not execute the same way.

From MSDN:

Unlike <xsl:apply-templates>, <xsl:call-template> does not change the current node or the current node-list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文