XSLT如何为x到y循环做经典之作?

jpa*_*tti 2 xslt xslt-1.0

我需要为i = 0到N循环做一个经典,怎么能在xstl 1.0中完成?

谢谢.

<xsl:for-each select="¿¿¿$i=0..5???">
    <fo:block>
        <xsl:value-of select="$i"/>
    </fo:block>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)

举个例子,我有

<foo>
    <bar>Hey!</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)

并希望输出

Hey!
Hey!
Run Code Online (Sandbox Code Playgroud)

Mat*_*ler 6

XSLT是一种函数式编程语言,因此它与您已经知道的任何过程语言非常不同.

尽管for在XSLT中可以进行循环,但它们并没有利用XSLT(以及一般的函数式编程)的固有优势.

for循环通常被误用来解决最好用功能方法解决的问题(即匹配模板).换句话说,循环在XSLT中并不是真正的"经典".

因此,您可能需要加倍,找出您面临的问题,而不是讨论您的解决方案.然后,XSLT社区可能会建议一个更具功能性的解决方案.可能是你已成为XY问题的受害者.


现在,XSLT固有的优点之一就是递归.通常,使用XSLT中的递归模板解决过程语言中的循环所解决的问题.

<xsl:template name="recursive-template">
   <xsl:param name="var" select="5"/>
   <xsl:choose>
     <xsl:when test="$var > 0">
       <xsl:value-of select="$var"/>
       <xsl:call-template name="recursive-template">
         <xsl:with-param name="var" select="$var - 1"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise/>
   </xsl:choose>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

总而言之,我建议你看看"经典"递归而不是"经典" for循环.您可以在此处的IBM文章中找到有关此主题的更多信息.


编辑作为对您编辑的问题的回复.如果您的问题真的归结为输出文本内容两次:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text"/>

   <xsl:template match="/foo">
      <xsl:apply-templates select="bar"/>
      <xsl:apply-templates select="bar"/>
   </xsl:template>

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

当然,对于动态的迭代次数,这是不可行的.