my input xml:
<citizens> <category type="plumbers"> <citizen>john</citizen> </category> <category type="doctors"> <citizen>ram</citizen> <citizen>kumar</citizen> </category> <category type="farmers"> <citizen>ganesh</citizen> <citizen>suresh</citizen> </category> </citizens>
i had tried following xslt count citizen
irrelevant of category
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:variable name="citizentotal" select="0" /> <xsl:for-each select="citizens/category"> <xsl:variable name="currentcount" select="count(citizen)" /> <xsl:variable name="citizentotal" select="$citizentotal+$currentcount" /> </xsl:for-each> total citizen nodes: <xsl:value-of select="$citizentotal"></xsl:value-of> </xsl:template> </xsl:stylesheet>
the expected output 5 , gives 0 initiated value outside for-each
. missing / messing <xsl:variable/>
scope. tried jslt scope page
default. in xslt mention scope of variable in xslt ?
the problem variables immutable in xslt. after setting not possible change it.
so assign citizentotal = 0. although make "assignment" of variable same name in for-each - in fact - declaring new variable in scope of cycle of loop. when exit for-each out of scope of - got variable declared before loop , set zero.
if need total number of citizens without cycle xpath count(//citizens/category/citizen)
Comments
Post a Comment