count - XSLT on each looped node, output number of preceding nodes containing a specific element -
given xml
<employee> <record> <id>1</id> <amount>10</amount> </record> <record> <id>2</id> <amount>0</amount> </record> <record> <id>3</id> <amount>0</amount> </record> <record> <id>4</id> <amount>20</amount> </record> <record> <id>5</id> <amount>50</amount> </record> <record> <id>6</id> <amount>0</amount> </record> <record> <id>7</id> <amount>40</amount> </record> </employee>
using xslt 1.0, want output follows:
zero amount node: 0 0 amount node: 0 0 amount node: 1 0 amount node: 2 0 amount node: 2 0 amount node: 2 0 amount node: 3
i want use for-each
, loop in record
nodes , on each iteration, output number of preceding nodes contained 0 amount
.
my xslt:
<xsl:output method="text" indent="no"/> <xsl:template match="/"> <xsl:for-each select="employee/record"> <xsl:variable name="amount-so-far" select=". | preceding-sibling::record"/> <xsl:variable name="amount-so-far-not-zero" select="$amount-so-far[not('0' = preceding-sibling::record/amount)]"/> <xsl:value-of select="count($amount-so-far-not-zero)"/> <xsl:text> </xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
try xslt desired output:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="employee"> <xsl:for-each select="record"> <xsl:value-of select="concat('zero amount node: ', count(preceding-sibling::record[amount='0']))"/><xsl:text> </xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment