You can't reassign to a variable in xslt. Looks like your best bet would be to include your condition in the variable and use the count function on that. Something like
The problem with using position() is that it isn't conditional, it would increase even if the xsl:when didn't match, so if it has to be conditional I would look at count().
If you absolutely need to have a 'counter' then you may want to consider an XSLT Extension that could do this for you. What are you trying to accomplish? There may be another way to solve your issue if we know more...
For the way you state your problem, Jeff's suggestion is the way to go - it looks like you just want to count how many of $items fit the condition, and you can literally tag the condition on to that as a predicate (i.e., in square brackets) and wrap the count() function around that; e.g., to count how many of $items that have the value "Royal Blue" in the property "penColor":
How to get number of items in for-each
Hi ,
I want to know how many items fit to 'WHEN":
<xsl:variable name="count" select="1>
<xsl:for-each select="$items">
<xsl:choose>
<xsl:when test="somthing">
<xsl:variable name="count">
<xsl:value-of select="$count+1"/>
</xsl:variable>
</xsl:when>
</xsl:choose>
</xsl:for-each>
but when I do:
<xsl:value-of select="$count"/> I get alwayse 1.
How can I get right number of items?
Thanks,
You can't reassign to a variable in xslt. Looks like your best bet would be to include your condition in the variable and use the count function on that. Something like
Use the position function, it offers quite a few extras:
In this example we check if the current item (this is within a foreach loop) is the last item.
if the current position is smaller than "5"
These examples are for the old XML scheme, but the position function works the same...
The problem with using position() is that it isn't conditional, it would increase even if the xsl:when didn't match, so if it has to be conditional I would look at count().
Regards,
Magnus
If you absolutely need to have a 'counter' then you may want to consider an XSLT Extension that could do this for you. What are you trying to accomplish? There may be another way to solve your issue if we know more...
Thanks,
Nik
Hi kukuwka,
For the way you state your problem, Jeff's suggestion is the way to go - it looks like you just want to count how many of $items fit the condition, and you can literally tag the condition on to that as a predicate (i.e., in square brackets) and wrap the count() function around that; e.g., to count how many of $items that have the value "Royal Blue" in the property "penColor":
/Chriztian
Thank you for all replies.
I want somethind like:
<xsl:for-each select="$items">
<xsl:variable name="content">
<xsl:choose>
<xsl:when>
something
</xsl:when>
<xsl:when>
something
</xsl:when>
<xsl:when>
something
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:choose>
</xsl:for-each>
<xsl:when test="string-length($content) > 0">
here count++
</xsl:when>
</xsl:choose>
is working on a reply...