2010-10-24 84 views
5

假设我有一个模板foo,它可以输出给定参数的内容。现在我想使用该输出作为我的另一个模板loop的参数,这样我就可以循环输出一定次数。我已经试过的东西沿着XST - 使用呼叫模板的输出作为返回值

<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> 

换句话说,output的方式现在应该包含从调用输出到fooloopfoo都可以独立工作,但看起来像我不能以这种方式嵌套它们。我应该如何做到这一点?提前致谢。

+0

问得好,+1。查看我的答案,了解您提供的代码的工作示例以及一些建议。 – 2010-10-24 15:30:59

回答

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> 

当任何XML(不适用使用),产生想要的,正确的结果

80 

文体推荐

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

将中间结果转换成(正确命名)变量要好得多。这样不仅代码更具可读性和可维护性,而且任何中间结果都可以多次重复使用,而无需重新评估。

这里是相同的变换而分解,使其达到建议的文体要求

<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> 
+0

+1正确答案。 – 2010-10-24 20:30:42