XSLT Sequence Number Generation

116 views Asked by At

I am preparing one flat file from XMl using XSLT 2.0. My input XML is

<city>
  <family>
    <parent>A</parent>
    <child>A1</child>
  </family>
  <family>
    <parent>A</parent>
    <child>A2</child>
  </family>
  <family>
    <parent>B</parent>
    <child>B1</child>
  </family>
  <family>
    <parent>B</parent>
    <child>B2</child>
  </family>
  <family>
    <parent>B</parent>
    <child>B3</child>
  </family>
  <family>
    <parent>C</parent>
    <child>C1</child>
  </family>


 </city>

Expected Output
-----------------
01 A 
02 B
03 C

I have to group parent and each parent has one entry in flat file and it should have correct sequence no. I am not able to generate sequence no correcly.

1

There are 1 answers

0
Martin Honnen On BEST ANSWER

Assuming you use for-each-group then outputting and formatting the position() should suffice:

  <xsl:template match="/">
      <xsl:for-each-group select="city/family" group-by="parent">
          <xsl:value-of select="concat(format-number(position(), '00'), ' ', current-grouping-key(), '&#10;')"/>
      </xsl:for-each-group>
  </xsl:template>