xml XST - 使用调用模板的输出作为返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4007658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:22:19  来源:igfitidea点击:

XST - using output from call-template as return value

xmlxsltxpath

提问by pg-robban

Suppose i have a template foowhich can output something given a parameter. Now I want to use that output as a parameter to my other template, loopso I can loop the output a certain number of times. I have tried something along the way of

假设我有一个模板foo,可以输出给定参数的内容。现在我想将该输出用作我的其他模板的参数,loop这样我就可以循环输出一定次数。我一直在尝试一些东西

    <xsl:call-template name="loop">
        <xsl:with-param name="times" select="someParam"/>
        <xsl:with-param name="output">
            <xsl:call-template name="foo">
                <xsl:with-param name="type" select="something"/>
            </xsl:call-template>
        </xsl:with-param>
    </xsl:call-template>

In other words, outputshould now contain the output from the call to foo. Both loopand foowork fine independantely but it seems like I can't nest them this way. How should I accomplish this? Thanks in advance.

换句话说,output现在应该包含调用的输出foo。无论loopfoo做工精细independantely但似乎我不能嵌套的这种方式。我应该如何做到这一点?提前致谢。

回答by Dimitre Novatchev

The problem is in the code you haven't shown to us. This is a correct way to chain/pipe templates, although I would not recommend it(see at the end of this answer),

问题出在您没有向我们展示的代码中。这是链接/管道模板的正确方法,尽管我不推荐它(见本答案末尾),

This transformation:

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
    <xsl:call-template name="loop">
        <xsl:with-param name="times" select="3"/>
        <xsl:with-param name="output">
            <xsl:call-template name="foo">
                <xsl:with-param name="pN" select="5"/>
            </xsl:call-template>
        </xsl:with-param>
    </xsl:call-template>
 </xsl:template>

 <xsl:template name="loop">
  <xsl:param name="times" select="1"/>
  <xsl:param name="output" select="2"/>

  <xsl:choose>
      <xsl:when test="not($times > 0)">
       <xsl:value-of select="$output"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:call-template name="loop">
        <xsl:with-param name="times" select="$times -1"/>
        <xsl:with-param name="output" select="2*$output"/>
       </xsl:call-template>
      </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="foo">
  <xsl:param name="pN" select="1"/>

  <xsl:value-of select="2*$pN"/>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML (not used), produces the wanted, correct result:

当应用于任何 XML(未使用)时,会产生想要的、正确的结果

80

Stylistic recommendation:

文体推荐

Try to avoid chaining templates in this way as this results in unreadable and unmaintainable code.

尽量避免以这种方式链接模板,因为这会导致代码不可读和不可维护。

It is much better to obtain the intermediate results into (properly named) variables. Not only is the code more readable and maintainable this way, but any intermediate result can be re-used multiple times without the need to re-evaluate it.

将中间结果获取到(正确命名的)变量中要好得多。通过这种方式不仅代码更具可读性和可维护性,而且任何中间结果都可以多次重复使用而无需重新评估它。

Here is the same transformation but factored to meet the recommended stylistic requirements:

这是相同的转换,但考虑到满足推荐的风格要求

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
   <xsl:variable name="vTwice">
    <xsl:call-template name="twice">
      <xsl:with-param name="pN" select="5"/>
    </xsl:call-template>
   </xsl:variable>

    <xsl:call-template name="loop">
        <xsl:with-param name="pTtimes" select="3"/>
        <xsl:with-param name="pN" select="$vTwice"/>
    </xsl:call-template>
 </xsl:template>

 <xsl:template name="loop">
  <xsl:param name="pTtimes" select="1"/>
  <xsl:param name="pN" select="2"/>

  <xsl:choose>
      <xsl:when test="not($pTtimes > 0)">
       <xsl:value-of select="$pN"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:call-template name="loop">
        <xsl:with-param name="pTtimes" select="$pTtimes -1"/>
        <xsl:with-param name="pN" select="2*$pN"/>
       </xsl:call-template>
      </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="twice">
  <xsl:param name="pN" select="1"/>

  <xsl:value-of select="2*$pN"/>
 </xsl:template>
</xsl:stylesheet>