递归循环XSLT

kou*_*des 5 xslt

所有,

我有下面的XSLT

<xsl:template name="loop">
    <xsl:param name="count" select="1"/>
    <xsl:if test="$count > 0">
        <xsl:text> </xsl:text>
        <xsl:value-of select="$count"/>  
        <xsl:call-template name="loop">
            <xsl:with-param name="count" select="$count - 1"/>
        </xsl:call-template>
    </xsl:if>    
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

打电话的方式是:

<xsl:call-template name="loop
    <xsl:with-param name="count" select="100"/>
</xsl:call-template>
Run Code Online (Sandbox Code Playgroud)

目前它显示从100到0的数字以及它们之间的空格.(100 99 98 97 .....)

如何改变它来做相反的事情呢?(1 2 3 4 ....)

非常感谢,

中号

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="count" select="100"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template name="loop">
        <xsl:param name="count" select="1"/>
        <xsl:param name="limit" select="$count+1"/>

        <xsl:if test="$count > 0">
            <xsl:text> </xsl:text>
            <xsl:value-of select="$limit - $count"/>
            <xsl:call-template name="loop">
                <xsl:with-param name="count" select="$count - 1"/>
                <xsl:with-param name="limit" select="$limit"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当对任何XML文档(未使用)执行此转换时,生成所需结果:1到100.

请注意:此解决方案是尾递归的,并且将优化许多XSLT处理器,以便消除递归.这意味着您可以将其$count设置为数百万而不会出现堆栈溢出或执行缓慢.

一个非尾递归解决方案,就像@ 0xA3崩溃堆栈溢出(使用Saxon 6.5.4)一样count = 1000


Dir*_*mar 6

只需更改模板内的顺序:

<xsl:template name="loop">
    <xsl:param name="count" select="1"/>

    <xsl:if test="$count > 0">
        <xsl:call-template name="loop">
            <xsl:with-param name="count" select="$count - 1"/>
        </xsl:call-template>

        <xsl:value-of select="$count"/>  
        <xsl:text> </xsl:text>

    </xsl:if>    
</xsl:template>
Run Code Online (Sandbox Code Playgroud)