A Simple Counter

Here should go questions about transforming XML with XSLT and FOP.
AshKutt
Posts: 8
Joined: Tue Oct 02, 2012 4:53 am

A Simple Counter

Post by AshKutt »

I'm from a procedural programming background, so I thought by declaring a variable and adding that variable count to +1 would get the running counter.

But it doesnt seem to work.

I also tried
<xsl:variable name="context" select="."/>
<xsl:for each select"/env:Xyx/env.Body/zbcf//fdf">

<xsl:value-of select"$context"/>
</xsl:for-each?
the above variable context displays all values from the node instead of just counter. Isnt there anything simple like X= 1 ; For look X=X+1
display X.

Thanks'
Ash
AshKutt
Posts: 8
Joined: Tue Oct 02, 2012 4:53 am

Re: A Simple Counter

Post by AshKutt »

K after some research decided to use position() function

<xsl for-each>

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

</xsl for-each>

This works fine. But I want to save the value into a variable and then print the total lines after the for loop. So i did this.

<xsl for-each>

<countNo><xsl:variable name="rownum" select="position()"></countNo>

</xsl for-each>
<xsl:value-of select=@rownum"/>

This does not work. Any ideas??
AshKutt
Posts: 8
Joined: Tue Oct 02, 2012 4:53 am

Re: A Simple Counter

Post by AshKutt »

Sorry i put the wrong sytax. It should be $ instead of @.

But still I get an error message that Variable rownum has not been delcared. Here is the code. When I try to declare the variable rownum above the for-each statement, then I get a message that variable already defined on the statement that's within the loop.

<xsl:for-each select="/env:Envelope/env:Body/
<xsl:variable name="rownum" select="position()"/>
</xsl:for-each>
<xsl:text>&#xa;</xsl:text>
<xsl>Total Row Count:</xsl>|<xsl:value-of select="$rownum"/>
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: A Simple Counter

Post by adrian »

You can't use a variable declared in an xsl:for-each loop outside of that loop (it's the same as in procedural programming, at least Java/C).

If you just want to count the number of elements use the count() function:
e.g.
<xsl>Total Row Count:</xsl>|<xsl:value-of select="count(/env:Envelope/env:Body/)"/>

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply