7 - Counting with the count() function

XSLT script: iliad7.xsl
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">

The name "Zeus" appears <xsl:value-of select="count(document/poem/line[name='Zeus'])" /> times.


The name "Achilles" appears <xsl:value-of select="count(document/poem/line[name='Achilles'])" /> times. </xsl:template> </xsl:stylesheet>
Functions are always used within XSL commands. In this example it is used in the select="" attribute of the <xsl:value-of> command so that the browser displays the results of the count().

The syntax count(document/poem/line[name='Zeus']) tells the XSLT script to count each instance in which the XML tag <name> found at the XPath document/poem/line contains the text "Zeus".

[Review the XML document]


RESULTS of iliad7.xsl
The name "Zeus" appears 1 times.

The name "Achilles" appears 2 times.