问题出在您未向我们展示的代码中。这是一个正确的方式来链/管的模板,虽然我不会推荐它(见这个答案的结束),
这种转变:
<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>
问得好,+1。查看我的答案,了解您提供的代码的工作示例以及一些建议。 – 2010-10-24 15:30:59