XSL 递归调用 - xsl:functions 与带有调用模板的 xsl:template
我有基本的查询。我一直在使用 xsl:template 并使用调用模板对模板进行递归调用。我看到 xsl:function 也可以像递归模板调用一样进行递归函数调用并实现相同的效果。何时应使用 xsl:function 以及何时应使用 xsl:template。我不确定两者之间有什么区别以及何时应该使用它们。他们每个人的特点是什么。有人可以帮助我更好地理解这一点吗?
I have basic query. I have been using xsl:template and use call tempate to make recursive calls to the template. I see xsl:function which also has feasibility to make recursive function calls like recursive template calls and achieve the same. When should xsl:function be used and when should xsl:template be used. I am not sure what is the diffence between the two and when should they be used. What are their special features of each of them. Can someone please help me understand this better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这几乎是我如何回答类似问题 3年前:
我只能补充一点:
尽可能使用
。在 XPath 3.0 中,函数是该语言的一流数据类型(又名 HOF - 高阶函数)。它们可以作为参数传递或作为结果返回到其他函数或从其他函数返回。
与使用命名模板相比,这是一个令人难以置信的强大飞跃。
This is how I replied to a similar question almost 3 years ago:
I can only add to this:
Always when possible use
<xsl:function>
.In XPath 3.0 functions are a first-class data type of the language (aka HOF -- Higher-Order Functions). They can be passed as parameters or returned as the result to/from other functions.
This is an incredibly powerful leap forward from using named templates.
从概念上讲,
xsl:apply-templates
是一个具有多态函数的映射,该多态函数表示您已声明的所有规则。xsl:function
声明一个“常规”函数,您可以在任何其他接受 XPath 表达式的指令或声明中使用。xsl:call-template
指令“调用”特定的命名模板(您可以以某种方式将其视为函数)。因此,每个评估上下文的参与方式都存在差异:
xsl:apply-templates
定义一个新的上下文列表,从中获取上下文节点以及邻近位置;xsl:function
没有定义上下文节点(依赖它是错误的);xsl:call-template
不会更改计算上下文。其他明显的区别是它们与输出的关系:
xsl:apply-templates
和xsl:call-template
作为 XSLT 指令输出其构造的序列;xsl:function
作为 XPath 表达式的一部分,但它没有。Conceptually
xsl:apply-templates
is a map with a polymorphic function expressed for all the rules you have declared.xsl:function
declares a "regular" function you can use in any any other instruction or declaration accepting XPath expressions.xsl:call-template
instruction "invokes" a particular named template (you could think of this as a function in some way).Because this, there are differences about how evaluation context is involve in each one:
xsl:apply-templates
define a new context list from which the context node is taken as well as the proximity position;xsl:function
doesn't have context node defined (it's an error to rely on it);xsl:call-template
doesn't change the evaluation context.Other evident difference is their relationship with the output: both
xsl:apply-templates
andxsl:call-template
as XSLT instructions output their constructed sequence;xsl:function
as part of an XPath expression it doesn't.我找到了 Dimitre 的回复 - http://www.stylusstudio.com/xsllist/200811/post00400。 html - 有帮助。
I found Dimitre's response - http://www.stylusstudio.com/xsllist/200811/post00400.html - helpful.