count Function

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Reference

count Function

Returns the number of nodes in the node-set argument.

number count(node-set)

Examples

XML File (test.xml)

<?xml version="1.0"?>
<test>
    <x a="1">
      <x a="2">
        <x>
          <y>y31</y>
          <y>y32</y>
        </x>
      </x>
    </x>
    <x a="1">
      <x a="2">
        <y>y21</y>
        <y>y22</y>
      </x>
    </x>
    <x a="1">
      <y>y11</y>
      <y>y12</y>
    </x>
    <x>
      <y>y03</y>
      <y>y04</y>
    </x>
</test>

XSLT File (test.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

<xsl:template match="/">
    //x,         <xsl:value-of select="count(//x)"/>
    //x[1],      <xsl:value-of select="count(//x[1])"/>
    //x/y,       <xsl:value-of select="count(//x/y)"/>
    //x/y[1],    <xsl:value-of select="count(//x/y[1])"/>
    //x[1]/y[1], <xsl:value-of select="count(//x[1]/y[1]"/></xsl:template>

</xsl:stylesheet>

Output

The XSLT stylesheet, when applied to the XML file above, produces the following results:

node-set count(node-set)
//x 7
//x[1] 4
//x/y 8
//x/y[1] 4
//x[1]/y[1] 2

See Also

Data Types in Schemas | XDR Schema Data Types Reference | XML Data Types Reference | Processing Node-Sets by Using Node-Set Functions