简体   繁体   English

目的是什么<xsl:call-template…>

[英]What is the purpose of <xsl:call-template…>

I have a XML file looks like below 我有一个XML文件,如下所示

- <select1>
   - <sel_details>
      <type>Primary Type</type> 
      <name>Some Name</name> 
      <id>ID Num</id>
     <sel_details>
     .....
  <select1>

And XSL looks like this XSL看起来像这样

<xsl:for-each select="//select1">
     <xsl:call-template name="sel_details" />
     <xsl:with-param name="xmlSection" select="sel_details" />
     </xsl:call-template>
     <xsl:call-template name="....." />
     <xsl:with-param name="xmlSection" select="....." />
     </xsl:call-template>
     ..........
</xsl:for-each>

<xsl:template name="sel_details">
     <xsl:param name="xmlSection" />
        <xsl:for-each select="xmlSection">
            <xsl:value-of select="./type" />
        </xsl:for-each>
</xsl:template>

What is the purpose of <xsl:call-template...> then <xsl:template name...> ? <xsl:call-template...>然后<xsl:template name...>什么?

If I write only 3 lines like this, I could get the same value: 如果我只写3行,我可以得到相同的值:

<xsl:for-each select="//select1/sel_details">
     <xsl:value-of select="./type" />
</xsl:for-each>

What is a named template? 什么是命名模板?

In an XSLT stylesheet, xsl:call-template : 在XSLT样式表中, xsl:call-template

<xsl:call-template name="construct-header"/>

means invoking a so-called named template - an xsl:template element that has a name attribute: 表示调用所谓的命名模板 -具有name属性的xsl:template元素:

<xsl:template name="construct-header">

As opposed to a template that matches input nodes and therefore has a match attribute: 与匹配输入节点并因此具有match属性的模板相反:

<xsl:template match="book">

Since named templates are never matched to input nodes, calling them explicitly with xsl:call-template is the only way to execute the code inside them. 由于命名模板永远不会与输入节点匹配,因此使用xsl:call-template显式调用它们是在其中执行代码的唯一方法。 Once a named template is invoked, the code is evaluated with no change to the context ( broadly speaking, at least ). 一旦调用了命名模板,就可以在不更改上下文的情况下( 至少广义上来说 )对代码进行评估。 So, a very legitimate question is 因此,一个非常合理的问题是

Why use a named template in general? 为什么通常使用命名模板?

Named templates are used 使用命名模板

  • to implement recursive algorithms in XSLT. 在XSLT中实现递归算法。 A well-known example is mimicking the tokenize() function in XSLT 1.0 which requires a recursive named template. 一个著名的示例是模仿XSLT 1.0中的tokenize()函数,函数需要递归命名模板。
  • to reuse code blocks in several places to avoid repetition 在多个位置重用代码块以避免重复
  • to organize your stylesheet into meaningful parts. 将样式表组织成有意义的部分。 This can also be done by modularizing your code into several stylesheets that are included or imported by the main stylesheet. 这也可以通过将代码模块化为主要样式表包含或导入的多个样式表来完成。

Using a named template in your specific case 在特定情况下使用命名模板

If the code is really as short as you show, I can see no reason to use a named template here. 如果代码确实如您所显示的那么短,那么我看不到没有理由在此处使用命名模板。 There is no recursive processing in the named template. 命名模板中没有递归处理。 But perhaps you have shortened it considerably before posting here or the named template is reused elsewhere in the stylesheet. 但是,也许您在将其发布到此处之前已将其大大缩短了,否则命名的模板将在样式表中的其他地方重用。 Both would be sound reasons to keep the named template. 两者都是保留命名模板的合理理由。 (If you'd like clearer guidance on this, please show a full stylesheet.) (如果您需要更清晰的指导,请显示完整的样式表。)

If you decide to reduce the code to an xsl:for-each , then also get rid of ./ because it is redundant, and, speaking from experience, I'd say there should not be a // in front of the path expression: 如果您决定将代码简化为xsl:for-each ,那么也要摆脱./因为它是多余的,并且根据经验,我会说路径表达式前不应//

<xsl:for-each select="select1/sel_details">
     <xsl:value-of select="type" />
</xsl:for-each>

The code looks to me as if it was written by someone who had not yet mastered XSLT's apply-templates idiom. 在我看来,该代码似乎是由尚未掌握XSLT的apply-templates习惯用法的人编写的。 I imagine a more experienced XSLT programmer might have written something like this: 我想象一个经验丰富的XSLT程序员可能写了这样的东西:

<xsl:template match="/">
  ...
  <xsl:apply-templates select="//select1"/>
  ...
</xsl:template>

<xsl:template match="select1">
     <xsl:apply-templates select="sel_details" />
     <xsl:apply-templates select="....." />
</xsl:template>

<xsl:template match="sel_details">
     <xsl:value-of select="type" />
</xsl:template>

Stylesheets in which xsl:for-each and xsl:call-template and xsl:choose are the dominant control structures generally suggest a programmer who is not yet comfortable with the XSLT idiom. 通常,以xsl:for-each和xsl:call-template和xsl:choose为主要控制结构的样式表提示程序员还不熟悉XSLT习惯用法。 Experienced XSLT coders use match templates and xsl:apply-templates instead. 经验丰富的XSLT编码人员使用匹配模板和xsl:apply-templates代替。

Think of named function in an ordinary programming language. 考虑一下普通编程语言中的命名函数。 <xsl:call-template> is a direct analog of function invocation. <xsl:call-template>是函数调用的直接模拟。 Whether it's worth to move a certain piece of code to a separate function or not - it's the matter of coding style, number of code repetions and other other "fuzzy" considerations. 是否值得将某个代码段移到一个单独的函数上-这是编码风格,代码重复次数和其他“模糊”考虑因素的问题。 In this particular case an author probably wished to assure that every section will be transfomed uniformly. 在这种特殊情况下,作者可能希望确保将每个部分统一变形。 Perhaps, in previous revisions of the file, the body of the function was less trivial. 也许,在文件的先前版本中,该函数的主体不是那么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM