I wanna know how to count the number of an attribute with particular values??
Created May 7, 2012
Roseanne Zhang Here is a complete answer for you!
Q. Can we do statistics on a specific
attribute value?
For examplw, how to get the statistics of status attribute of result element:
XSLT sourece
For examplw, how to get the statistics of status attribute of result element:
<results> <result status="-1" >aaa</result> <result status="-5" >bbb</result> <result status="-1" >ccc</result> <result status="-5" >ddd</result> <result status="-5" >lll</result> <result status="-6" >vvv</result> <result status="-1" >vvv</result> </results>A: Using XSLT Grouping. Here is using muenchain method.
XSLT sourece
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:key name="stvalkey" match="//result" use="@status" /> <xsl:template match="/"> <xsl:for-each select="//result/" > <xsl:variable name="tmp" > <xsl:value-of select="./@status" /> </xsl:variable> <xsl:if test="generate-id(.) = generate-id(key('stvalkey',$tmp)[1])"> <xsl:value-of select="$tmp" />:<xsl:value-of select="count(key('stvalkey',$tmp))" /><xsl:text> </xsl:text> </xsl:if> </xsl:for-each> </xsl:template> <xsl:template match="text()" /> </xsl:stylesheet>Text result to show the statistics
-1:3 -5:3 -6:1