Mic*_*Kay 46
在XSLT 2.0中,
<xsl:for-each select="1 to 60">...</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
但我想你必须使用XSLT 1.0,否则你不会问.
在XSLT 1.0中,您应该使用递归:一个模板,它使用在每次调用时递增的计数器调用自身,并且在达到所需值时递归终止.
或者,在XSLT 1.0中有一个解决方法:如果您的源文档包含至少60个节点,您可以这样做
<xsl:for-each select="(//node())[60 >= position()]">...</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
Dim*_*hev 26
处理长序列时简单递归的问题在于,调用堆栈的空间通常变得不足,并且由于堆栈溢出而导致处理结束.这通常发生在序列长度> = 1000的情况下.
避免这种情况的一般技术(可以用任何XSLT处理器实现,即使它不能识别尾递归)也是DVC(分而治之)样式递归.
以下是成功打印1到1000000(1M)数字的转换示例:
<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="displayNumbers">
<xsl:with-param name="pStart" select="1"/>
<xsl:with-param name="pEnd" select="1000000"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="displayNumbers">
<xsl:param name="pStart"/>
<xsl:param name="pEnd"/>
<xsl:if test="not($pStart > $pEnd)">
<xsl:choose>
<xsl:when test="$pStart = $pEnd">
<xsl:value-of select="$pStart"/>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vMid" select=
"floor(($pStart + $pEnd) div 2)"/>
<xsl:call-template name="displayNumbers">
<xsl:with-param name="pStart" select="$pStart"/>
<xsl:with-param name="pEnd" select="$vMid"/>
</xsl:call-template>
<xsl:call-template name="displayNumbers">
<xsl:with-param name="pStart" select="$vMid+1"/>
<xsl:with-param name="pEnd" select="$pEnd"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于任何XML文档(未使用)时,此转换将生成所需结果 - 所有数字从1到1000000.
您可以将此转换用于任何需要"执行N次"的任务.
| 归档时间: |
|
| 查看次数: |
57430 次 |
| 最近记录: |