xml - How to count entries in XSLT? -
i quite new xslt , generate count of participants list of events
. xml:
<events> <event name="christmas"/> <event name="halloween"/> <event name="easter"/> <event name="easter"/> </events>
what need this:
christmas: 1 participant halloween: 1 participant easter: 2 participants
can done xslt in way?
thanks help!
try stylesheet, uses muenchian method group event
elements @name
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <!-- based on http://stackoverflow.com/a/16509871/2115381 dimitre novatchev --> <xsl:key name="keventval" match="event" use="@name"/> <xsl:template match="*"> <xsl:apply-templates select= "event[generate-id() = generate-id(key('keventval',@name)[1])]"/> </xsl:template> <xsl:template match="event"> <xsl:value-of select="@name"/> <xsl:text>: </xsl:text> <xsl:value-of select="count(key('keventval',@name))"/> <xsl:text> participant</xsl:text> <xsl:if test="count(key('keventval',@name)) > 1 "> <xsl:text>s</xsl:text> </xsl:if> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet>
which generate following output:
christmas: 1 participant halloween: 1 participant easter: 2 participants
Comments
Post a Comment