xslt页面中的for循环 [英] for loop in xslt page

查看:29
本文介绍了xslt页面中的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的 xml 中得到一些整数,我想得到 1 和得到的数字之间的所有数字.

I get from my xml some integer and I want to get all numbers between 1 and the gotten number .

我想将这些数字作为列名插入.

I want to insert those numbers as a columns names .

可以吗?

谢谢.

推荐答案

I.XSLT 2.0 解决方案:

 <xsl:sequence select="1 to $N"/>

<小时>

二.XSLT 1.0 DVC 解决方案

@nd(使用原始递归)的答案很好.

The answer by @nd (using primitive recursion) is a good one.

但是,如果要生成具有足够大长度(1000 或更多)的数字序列,则原始递归通常会由于太深的递归而以堆栈溢出异常结束.

DVC(分而治之)方法可用于避免任何实际情况下的调用堆栈溢出:

<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>&#xA;</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>

当此转换应用于任何 XML 文档(未使用)时,它会输出从一到一百万的数字.这个转换过程中调用栈的最大深度只有19(log2(N)).

When this transformation is applied on any XML document (not used), it outputs the numbers from one to one million. The maximum dept of the call stack during this transformation is only 19 (log2(N)).

三.非递归 XSLT 1.0 解决方案:

如果要生成的数字序列的长度不是太大(更准确地说,不超过 XML 文档中可用节点的数量(例如 XSLT 样式表本身))),然后我们可以使用非递归解决方案,称为Piez方法:

In case the length of the numeric sequence to be generated is not too big (more precisely , doesn't exceed the number of the available nodes in an XML document (such as the XSLT stylesheet itself)), then we can use a non-recursive solution, known as the Piez method:

<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>&#xA;</xsl:text>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于任何 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

这篇关于xslt页面中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆