Re: [xsl] sequential numbering with for-each

Subject: Re: [xsl] sequential numbering with for-each
From: "Heiko Niemann kontakt@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 30 Aug 2016 21:40:49 -0000
You could save your strings (result) in a variable first and then do the
numbering in a second step (output):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs"
version="2.0">
  <xsl:output method="text" encoding="UTF-8"/>
  <xsl:template match="/">

    <xsl:variable as="xs:string*" name="foos">
      <xsl:for-each select="1 to 5">
        <xsl:sequence select="concat('Data', .,' - ok')"/>
        <xsl:sequence select="concat('Data', .,' - not ok')"/>
      </xsl:for-each>
    </xsl:variable>

    <xsl:value-of separator="{codepoints-to-string(10)}">
      <xsl:for-each select="$foos">
        <xsl:sequence select="concat(position(), '. ', .)"/>
      </xsl:for-each>
    </xsl:value-of>

  </xsl:template>
</xsl:stylesheet>


Heiko


> Hello:
>
> I have a situation where I need to iterate over an element twice and
> output sequential numbers.
>
> I have two input files:
>
> First XML file. foo occurrence is 1 to infinity
>
> <root>
>
> <foo type="f1">
> <foo1>ok</foo1>
> <foo1>not ok</foo1>
> </foo>
>
> </root>
>
> Second XML file. tables can occur 1 to infinity. tab1 can also occur 1
> to infinity.
>
> <root1>
>
> <table ttype="f1">
>
> <tables>
> <tab1>
> Data1
> </tab1>
>
> <tab1>
> Data2
> </tab1>
>
>
> </tables>
> <tables>
> <tab1>
> ......
> </tab1>
> <tab1>
> Data3</tab1>
> </tables>
> </table>
> </root1>
>
> The logic is:
>
> for each /root/foo[@type = @ttype from 'root1']/foo1
>
> repeat tab1
>
> Desired output:
>
> 1. Data1 - ok
> 2.Data1 - not ok
> 3.Data2 - ok
> 4.Data2 - not ok
> 5.Data3 -ok
> 6.Data3-not ok
>
> I am able to get the individual 'tab1' elements to repeat. But my
> sequential numbering label is off. If I use position() within for-each
> it resets for every call in the for-each loop.
>
> I get:
>
> 1. Data1 - ok
> 2.Data1 - not ok
> 1.Data2 - ok
> 2.Data2 - not ok
> 1.Data3 -ok
> 2.Data3-not ok
>
>
> Any ideas or suggestions on how to make this work will be appreciated.

Current Thread