XST - 使用call-template的输出作为返回值

pg-*_*ban 5 xml xslt xpath

假设我有一个模板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>
Run Code Online (Sandbox Code Playgroud)

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

Dim*_*hev 9

问题出在您未向我们展示的代码中.这是链/管道模板的正确方法,虽然我不推荐它(参见本答案的末尾),

这种转变:

<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>
Run Code Online (Sandbox Code Playgroud)

当应用于任何XML(未使用)时,生成所需的正确结果:

80
Run Code Online (Sandbox Code Playgroud)

文体推荐:

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

将中间结果获得(适当命名)变量要好得多.代码不仅更具可读性和可维护性,而且任何中间结果都可以多次重复使用,而无需重新评估.

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

<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>
Run Code Online (Sandbox Code Playgroud)