The count function returns the number of nodes in the argument node-set. : count « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <AAA>
      <CCC/>
      <BBB>
        <CCC>Carl</CCC>
      </BBB>
      <BBB/>
      <BBB/>
    </AAA>
    <AAA>
      <CCC/>
      <BBB/>
      <BBB>
        <CCC>John</CCC>
        <CCC>Charles</CCC>
        <CCC>Robert</CCC>
        <CCC>Anthony</CCC>
      </BBB>
    </AAA>
</data>
File: Transform.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <DIV>
        <B>
          <xsl:text>//AAA : </xsl:text>
        </B>
        <xsl:value-of select="count(//AAA)"/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>//CCC : </xsl:text>
        </B>
        <xsl:value-of select="count(//CCC)"/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>//AAA/CCC : </xsl:text>
        </B>
        <xsl:value-of select="count(//AAA/CCC)"/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>//CCC[text()]) : </xsl:text>
        </B>
        <xsl:value-of select="count(//CCC[text()])"/>
      </DIV>
    </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><DIV><B>//AAA : </B>2</DIV><DIV><B>//CCC : </B>7</DIV><DIV><B>//AAA/CCC : </B>2</DIV><DIV><B>//CCC[text()]) : </B>5</DIV>








5.10.count
5.10.1.count()- Takes a node-set argument and returns a value equal to the number of nodes in the node-set
5.10.2.Use count function in math calculation
5.10.3.Count matches
5.10.4.Count node
5.10.5.count elements which occured in XML source
5.10.6.The count function returns the number of nodes in the argument node-set.