XSL 递归调用 - xsl:functions 与带有调用模板的 xsl:template

发布于 2024-11-04 00:30:48 字数 191 浏览 0 评论 0原文

我有基本的查询。我一直在使用 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 技术交流群。

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

发布评论

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

评论(4

聽兲甴掵 2024-11-11 00:30:48

这几乎是我如何回答类似问题 3年前:

使用的好处:

  1. 可组合性。

  2. 在使用它作为参数的表达式中看起来很棒,如
    返回值或作为部分
    申请。

  3. 可读性(紧凑性)和可维护性。

  4. 更松散的耦合(不依赖于隐式上下文节点)

  5. 可以在 XPath 表达式中引用

缺点:

  1. 参数仅通过位置(而不是名称)标识

  2. 可能是不纯的(可能会产生副作用,例如创建新节点)
    只需看一个表情
    人们可能会引用这个函数
    不明白它有一面
    影响。然而这种可能性
    如果适当的话可以消除混乱
    使用命名。

我总是倾向于使用
在函数创建的情况下
新节点我遵循以下约定
其本地名称以
字符串“make”,如 makePerson() 中。

我只能补充一点:

尽可能使用

在 XPath 3.0 中,函数是该语言的一流数据类型(又名 HOF - 高阶函数)。它们可以作为参数传递或作为结果返回到其他函数或从其他函数返回。

与使用命名模板相比,这是一个令人难以置信的强大飞跃

This is how I replied to a similar question almost 3 years ago:

Benefits of using <xsl:function/>:

  1. Composability.

  2. Looks great in an expression which uses it as a parameter, as
    return value or as a partial
    application.

  3. Readability (compactness) and maintainability.

  4. More loose coupling (does not depend on an implicit context node)

  5. Can be referenced in an XPath expression

Shortcomings:

  1. Parameters are identified only by position (not by name)

  2. Can be impure (can have a side effect, such as creating new node(s))
    and just by looking at an expression
    referencing this function people may
    not understand that it has a side
    effect. However this possibility of
    confusion can be eliminated if proper
    naming is used.

I tend always to use <xsl:function/>.
In the cases when the function creates
new node(s) I follow the convention of
starting its local-name with the
string "make", as in makePerson().

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.

最美的太阳 2024-11-11 00:30:48

从概念上讲,xsl:apply-templates 是一个具有多态函数的映射,该多态函数表示您已声明的所有规则。 xsl:function 声明一个“常规”函数,您可以在任何其他接受 XPath 表达式的指令或声明中使用。 xsl:call-template 指令“调用”特定的命名模板(您可以以某种方式将其视为函数)。

因此,每个评估上下文的参与方式都存在差异:xsl:apply-templates 定义一个新的上下文列表,从中获取上下文节点以及邻近位置; xsl:function 没有定义上下文节点(依赖它是错误的); xsl:call-template 不会更改计算上下文。

其他明显的区别是它们与输出的关系:xsl:apply-templatesxsl: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 and xsl:call-template as XSLT instructions output their constructed sequence; xsl:function as part of an XPath expression it doesn't.

jJeQQOZ5 2024-11-11 00:30:48

我找到了 Dimitre 的回复 - http://www.stylusstudio.com/xsllist/200811/post00400。 html - 有帮助。

使用的好处:

  1. 可组合性。

  2. 在使用它作为参数的表达式中看起来很棒,如
    返回值或作为部分
    申请。

  3. 可读性(紧凑性)和可维护性。

  4. 更松散的耦合(不依赖于隐式上下文节点)

  5. 可以在 XPath 表达式中引用

缺点:

  1. 参数仅通过位置(而不是名称)标识

  2. 可能是不纯的(可能会产生副作用,例如创建新节点)
    只需看一个表情
    人们可能会引用这个函数
    不明白它有一面
    影响。然而这种可能性
    如果适当的话可以消除混乱
    使用命名。

我总是倾向于使用
在函数创建的情况下
新节点我遵循以下约定
其本地名称以
字符串“make”,如 makePerson() 中。

I found Dimitre's response - http://www.stylusstudio.com/xsllist/200811/post00400.html - helpful.

Benefits of using <xsl:function/>:

  1. Composability.

  2. Looks great in an expression which uses it as a parameter, as
    return value or as a partial
    application.

  3. Readability (compactness) and maintainability.

  4. More loose coupling (does not depend on an implicit context node)

  5. Can be referenced in an XPath expression

Shortcomings:

  1. Parameters are identified only by position (not by name)

  2. Can be impure (can have a side effect, such as creating new node(s))
    and just by looking at an expression
    referencing this function people may
    not understand that it has a side
    effect. However this possibility of
    confusion can be eliminated if proper
    naming is used.

I tend always to use <xsl:function/>.
In the cases when the function creates
new node(s) I follow the convention of
starting its local-name with the
string "make", as in makePerson().

巡山小妖精 2024-11-11 00:30:48
Templates are useful when you have the requirement to store the results of each recursion into a variable as a attribute ( at the end of each recursion before calling the next ).

**Example:**

    <xsl:variable name="test">
     <record>
        <xsl:call-template name="templateRecursion">
              <xsl:with-param name="xyz" select="xyz"/>   
        </xsl:call-template>
     <record>
    </xsl:variable>

**templateRecursion:**

    <xsl:template name="templateRecursion">

    <!-- Do processing -->
     <xsl:attribute name="" value=""

    </xsl:template>

So, the variable test will have 

    <record>
     <attribute_name="" value=""/>
      .
      .
    </record>
Templates are useful when you have the requirement to store the results of each recursion into a variable as a attribute ( at the end of each recursion before calling the next ).

**Example:**

    <xsl:variable name="test">
     <record>
        <xsl:call-template name="templateRecursion">
              <xsl:with-param name="xyz" select="xyz"/>   
        </xsl:call-template>
     <record>
    </xsl:variable>

**templateRecursion:**

    <xsl:template name="templateRecursion">

    <!-- Do processing -->
     <xsl:attribute name="" value=""

    </xsl:template>

So, the variable test will have 

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