I. XSLT 2.0解决方案:
<xsl:sequence select="1 to $N"/>
Run Code Online (Sandbox Code Playgroud)
II.XSLT 1.0 DVC解决方案
@nd(使用原始递归)的答案是一个很好的答案.
但是,如果要生成具有足够大的长度(1000或更多)的数字序列,则由于过于递归的递归,原始递归通常会因堆栈溢出而异常结束.
DVC(Divide and Conquer)方法可用于避免任何实际情况下的调用堆栈溢出:
<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文档(未使用)时,它会输出一到一百万的数字.此转换期间调用堆栈的最大部分仅为19(log2(N)).
III.非递归XSLT 1.0解决方案:
如果要生成的数字序列的长度不是太大(更准确地说,不超过XML文档中可用节点的数量(例如XSLT样式表本身)),那么我们可以使用非递归解决方案,称为Piez方法:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="vDoc" select="document('')"/>
<xsl:variable name="vNodes" select=
"$vDoc//node() | $vDoc//@* | $vDoc//namespace::*"/>
<xsl:template match="/">
<xsl:for-each select="$vNodes[not(position() > 40)]">
<xsl:value-of select="position()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于任何XML文档(未使用)时,它会生成1到40之间的数字:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Run Code Online (Sandbox Code Playgroud)