Counter inside xsl:for-each loop

XmlXsltLoops

Xml Problem Overview


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">
	<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 :)

Xml Solutions


Solution 1 - Xml

position(). E.G.:

<countNo><xsl:value-of select="position()" /></countNo>

Solution 2 - Xml

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

Solution 3 - Xml

Try:

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

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

Solution 4 - Xml

You can also run conditional statements on the Postion() which can be really helpful in many scenarios.

for eg.

 <xsl:if test="(position( )) = 1">
     //Show header only once
    </xsl:if>

Solution 5 - Xml

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkristofView Question on Stackoverflow
Solution 1 - XmlredsquareView Answer on Stackoverflow
Solution 2 - Xmlm_pGladiatorView Answer on Stackoverflow
Solution 3 - XmlLuke BennettView Answer on Stackoverflow
Solution 4 - XmlArun ArangilView Answer on Stackoverflow
Solution 5 - XmlSantiago CepasView Answer on Stackoverflow