Hi,
I hope that you don't mind that I step in here.
1. let me explain why R7AF creates a global variable.
Variables in XSLT don't vary. Actually they are more like constants in other programming languages.
That means that they need to be calculated in advance.
XSLT is side effect free, for that variables need to be constant.
There is one good thing about this (except that you can develop reliable code)
Most of the variables you would need, (such as finding out at what position a certain value sits)
can be derived from the XPath model directly, as you will see in my example
2. I have some comments on the approach R7AF took.
I highly recommend NOT to use a for-each to get to the value, that would be the procedural approach.
You don't need to loop over all values, use a simple XPath expresion instead
By the way, R7AFs solution would break if there were a second cell having the value 'C'
Here is all the XSLT you need
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.o
<xsl:variable name="p" select="count(/row/cell[no
<xsl:template match="row">
<xsl:value-of select="$p"/>
</xsl:template>
</xsl:stylesheet>
I count the preceding siblings of the chosen cell, because that way I don't need position()
This is a lot more convenient than pulling the value out of loop.
Beware of procedural thinking when doing XSLT!
3. I strongly recommend to create variables at the correct depth in the stylesheet.
If you only need this value inside the template for row, you should create it there
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.o
<xsl:template match="row">
<xsl:variable name="p" select="count(/row/cell[no
<xsl:value-of select="$p"/>
</xsl:template>
</xsl:stylesheet>
If you don't like to add an arbitrary '+ 1' (to count the self node), make a union... it makes it a little bit more obvious for someone else reading the code, what you are counting
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.o
<xsl:template match="row">
<xsl:variable name="p" select="count(/row/cell[no
<xsl:value-of select="$p"/>
</xsl:template>
</xsl:stylesheet>
cheers
Geert
Main Topics
Browse All Topics





by: R7AFPosted on 2007-12-14 at 15:03:27ID: 20475136
Below is an example. You can use the variable $p anywhere in the stylesheet.
Select allOpen in new window