tags:

views:

16129

answers:

5

How to get a counter inside xsl:for-each loop that would reflect the number of current element processed.
For example my source XML is

<books>
    <book>
     <title>The Unbearable Lightness of Being </title>
    </book>
    <book>
     <title>Narcissus and Goldmund</title>
    </book>
    <book>
     <title>Choke</title>
    </book>
</books>

What I want to get is:

<newBooks>
    <newBook>
     <countNo>1</countNo>
     <title>The Unbearable Lightness of Being </title>
    </newBook>
    <newBook>
     <countNo>2</countNo>
     <title>Narcissus and Goldmund</title>
    </newBook>
    <newBook>
     <countNo>3</countNo>
     <title>Choke</title>
    </newBook>
</newBooks>

The XSLT to modify:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
     <newBooks>
      <xsl:for-each select="books/book">
       <newBook>
        <countNo>???</countNo>
        <title>
         <xsl:value-of select="title"/>
        </title>
       </newBook>
      </xsl:for-each>
     </newBooks>
    </xsl:template>
</xsl:stylesheet>

So the question is what to put in place of ???. Is there any standard keyword or do I simply must declare a variable and increment it inside the loop?

As the question is pretty long I should probably expect one line or one word answer :)

+24  A: 

position(), e.g:

<countNo><xsl:value-of select="position()" /></countNo>
redsquare
Need to add a closing quote " to the attribute value
Bill Michell
+1  A: 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <newBooks>
                <xsl:for-each select="books/book">
                        <newBook>
                                <countNo><xsl:value-of select="position()"/></countNo>
                                <title>
                                        <xsl:value-of select="title"/>
                                </title>
                        </newBook>
                </xsl:for-each>
        </newBooks>
    </xsl:template>
</xsl:stylesheet>
santiiiii
+5  A: 

Try inserting <xsl:number format="1. "/><xsl:value-of select="."/><xsl:text> in the place of ???.

Note the "1. " - this is the number format. More info: here

m_pGladiator
never knew that, thanks
redsquare
Thanks, that is an interesting read as well.I will probably use that approach when some formatting is required
kristof
Really old post I know, but helped me, so here's your +1
MJB
A: 

Try:

<xsl:value-of select="count(preceding-sibling::*) + 1" />

Edit - had a brain freeze there, position() is more straightforward!

Luke Bennett
A: 

I have two nested loop in XSL like this, at this moment I use position() but it's not what I need.

<xsl:for-each select="abc">
  <xsl:for-each select="def">
   I wanna my variable in here increasing fluently 1,2,3,4,5.....n
not like 1,2,3,1,2,3
  </xsl:for-each>
</xsl:for-each>

Can you give me some idea for this stub. Thank you very much!

gacon