XSLT 如何做一个经典的 for x to y 循环? [英] XSLT How to do a classic for x to y loop?

查看:22
本文介绍了XSLT 如何做一个经典的 for x to y 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个经典的 for i=0 to N 循环,在 xstl 1.0 中怎么做?

谢谢.

<fo:block><xsl:value-of select="$i"/></fo:block></xsl:for-each>

举个例子,我有

<bar>嘿!</bar></foo>

并希望输出

嘿!嘿!

解决方案

XSLT 是一种函数式 编程语言,因此它与您已知的任何过程语言都大不相同.

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

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

因此,您可能需要加倍努力,确定您面临的问题,而不是讨论您的解决方案.然后,XSLT 社区可能会建议一个更实用的解决方案.您可能已经成为XY问题的受害者.><小时>

现在,XSLT 固有的擅长就是递归.通常,在过程语言中用循环解决的问题在 XSLT 中用递归模板解决.

<xsl:param name="var" select="5"/><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:否则/></xsl:选择></xsl:模板>

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

<小时>

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

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

I need to do a classic for i=0 to N loop, how can it be done in xstl 1.0?

thanks.

<xsl:for-each select="¿¿¿$i=0..5???">
    <fo:block>
        <xsl:value-of select="$i"/>
    </fo:block>
</xsl:for-each>

To give an example, I have

<foo>
    <bar>Hey!</bar>
</foo>

And want an output of

Hey!
Hey!

解决方案

XSLT is a functional programming language and as such it is very different to any procedural languages you already know.

Although for loops are possible in XSLT, they do not make use of the inherent strengths of XSLT (and functional programming in general).

for loops are routinely misused to address problems that are best solved with a functional approach instead (that is, matching templates). In other words, a loop is not really a "classic" in XSLT.

So, you might have to double back, identify the problem you are facing instead of discussion your solution. Then, the XSLT community might be able to suggest a solution that is more functional in nature. It might be that you've fallen victim to the XY problem.


Now, among the things XSLT is inherently good at is recursion. Often, problems that are solved with loops in procedural languages are solved with recursive templates in 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>

To summarize, I suggest you look at "classic" recursion instead of "classic" for loops. You find more information about exactly this topic in an IBM article here.


EDIT as a response to your edited question. If your problem really boils down to outputting text content twice:

<?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>

This is not feasible of course for a dynamic number of iterations.

这篇关于XSLT 如何做一个经典的 for x to y 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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