Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:从1循环到60_Xslt - Fatal编程技术网

XSLT:从1循环到60

XSLT:从1循环到60,xslt,Xslt,在XSLT中从1循环到60的最佳方法是什么? 我在net中研究,有一些模板可以这样做,有没有其他方法,例如内置函数?XSLT基于模板工作,您需要一个模板来运行该循环 您需要构建一个接收起始值和结束值的模板,并在其中使用start+1进行递归调用计算。当$start等于$end时,您将返回模板,无需再次调用 实际上:在XSLT2.0中 <xsl:for-each select="1 to 60">...</xsl:for-each> 。。。 但我猜您一定在使用XSLT1

在XSLT中从1循环到60的最佳方法是什么?
我在net中研究,有一些模板可以这样做,有没有其他方法,例如内置函数?

XSLT基于模板工作,您需要一个模板来运行该循环

您需要构建一个接收起始值和结束值的模板,并在其中使用start+1进行递归调用计算。当$start等于$end时,您将返回模板,无需再次调用

实际上:

在XSLT2.0中

<xsl:for-each select="1 to 60">...</xsl:for-each>
。。。
但我猜您一定在使用XSLT1.0,否则您就不会问了

在XSLT1.0中,您应该使用递归:一种模板,它使用每次调用时递增的计数器调用自己,当达到所需值时,递归终止

另外,XSLT1.0中还有一个变通方法:如果源文档至少包含60个节点,您可以这样做

<xsl:for-each select="(//node())[60 >= position()]">...</xsl:for-each>
。。。

在处理长序列时,简单递归的问题是,调用堆栈的空间通常不足,并且处理由于堆栈溢出而结束。这通常在序列长度>=1000时发生

避免这种情况的一种通用技术是DVC(分而治之)风格的递归(可在任何XSLT处理器上实现,即使它不识别尾部递归)

以下是成功打印1到1000000(1M)数字的转换示例:

当应用于任何XML文档(未使用)时,此转换将生成所需的结果——从1到1000000的所有数字

您可以对任何需要“做N次某件事”的任务使用/调整此转换。

在foreach循环中进行非常简单的检查

做点什么
根据答案

示例:

<xsl:variable name="maxItems" select="10" />
<xsl:variable name="sequence" select="any-sequence"/>

<xsl:for-each select="$sequence">

    <!-- Maybe sort first -->
    <xsl:sort select="@sort-by" order="descending" />

    <!-- where the magic happens -->
    <xsl:if test="$maxItems > position()">
        do something
    </xsl:if>
</xsl:for-each>

做点什么

V1.0使用递归的基本示例如下:

<xsl:template match="/">
<Root>
      <!-- Main Call to MyTemplate -->
     <xsl:call-template name="MyTemplate" />
</Root>
</xsl:template>

<xsl:template name="MyTemplate">
  <xsl:param name="index" select="1" />
  <xsl:param name="maxValue" select="60" />

  <MyCodeHere>
     <xsl:value-of select="$index"/>
  </MyCodeHere>

  <!-- &lt; represents "<" for html entities -->
  <xsl:if test="$index &lt; $maxValue">
    <xsl:call-template name="MyTemplate">
        <xsl:with-param name="index" select="$index + 1" />
        <xsl:with-param name="total" select="$maxValue" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>


您是XSLT大师!我不得不降级我的XSLT2.0模板,而你的答案是一个救世主!只使用一个参数并向后计数要简单得多。@Alejandro你说得对。我更改了验证。您如何访问当前值?(在第一段代码的三点中)尝试使用
,但没有成功。请使用
position()
<xsl:variable name="maxItems" select="10" />
<xsl:variable name="sequence" select="any-sequence"/>

<xsl:for-each select="$sequence">

    <!-- Maybe sort first -->
    <xsl:sort select="@sort-by" order="descending" />

    <!-- where the magic happens -->
    <xsl:if test="$maxItems > position()">
        do something
    </xsl:if>
</xsl:for-each>
<xsl:template match="/">
<Root>
      <!-- Main Call to MyTemplate -->
     <xsl:call-template name="MyTemplate" />
</Root>
</xsl:template>

<xsl:template name="MyTemplate">
  <xsl:param name="index" select="1" />
  <xsl:param name="maxValue" select="60" />

  <MyCodeHere>
     <xsl:value-of select="$index"/>
  </MyCodeHere>

  <!-- &lt; represents "<" for html entities -->
  <xsl:if test="$index &lt; $maxValue">
    <xsl:call-template name="MyTemplate">
        <xsl:with-param name="index" select="$index + 1" />
        <xsl:with-param name="total" select="$maxValue" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>