Increment a value in XSLT

后端 未结 4 944
深忆病人
深忆病人 2020-12-09 02:36

I\'m reasonably new to xlst and am confused as to whether there is any way to store a value and change it later, for example incrementing a variable in a loop.

I\'m

4条回答
  •  -上瘾入骨i
    2020-12-09 03:03

    I ran into that myself two years ago. You need to do use recursion for this. I forget the exact syntax, but this site might help:

    Tip: Loop with recursion in XSLT

    The strategy works basically as follows: Replace for loop with a template "method". Have it recieve a parameter i. Do the body of the for loop in the template method. If i > 0 call the template method again (recursion) with i - 1 as parameter.

    Pseudocode:

    for i = 0 to 10:
       print i
    

    becomes:

    def printer(i):
       print i
       if i < 10:
          printer(i + 1)
    printer(0)
    

    Please note that using position() in a xsl:for-each (see other answers) can be simpler if all you want to do is have a variable increment. Use the kind of recursion explained here if you want a more complicated loop / condition.

提交回复
热议问题