XSLT and XPath function reference in alphabetical order

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

fn:count

Category:

Aggregation

Origin:

XPath 1.0

Return value:

A number – the number of sequence elements contained in a sequence passed on (items). However, in XPath 1.0 the nodes contained in a node set are counted. In both cases the return value is an integer: in XPath 1.0 of the number type, in XPath 2.0 of the xs:integer type.

Call/Arguments:

fn:count($sequence)

$sequence:

Obligatory. A sequence of items which may be nodes as well as atomic values or a combination thereof. (In XPath 1.0 the input value must be a node set.)

Purpose of use:

Only the sequence elements (items) directly contained in the sequence (XPath 1.0: in the node set) are counted, but not if they are element nodes, their possible child nodes or descendants. If the empty sequence is passed on (XPath 2.0) or if the node set is empty (XPath 1.0), the function returns the integer value 0.

Example 1 - counting elements:

<xsl:value-of select="fn:count(//product)"/>

With the help of fn:count(), this expression counts all <product> elements contained in a document and outputs their number.

Example 2 - storage of the number of attributes in a variable:

<xsl:variable name="att-number" select="fn:count(@*)"/>

In this example, the number of the attributes (having any name) of the current node is stored in an att-number variable. For this purpose, the fn:count() function is simply used in the select attribute of the variable within a XPath expression.

Example 3 - counting items of a sequence:

fn:count(1, 3, 7) results in 3.

The passing on of a sequence consisting of three items to the function results in the return value 3.

Example 4 - passing on an empty sequence:

fn:count(()) results in 0.

The passing on of an empty sequence to the function results in the return value 0.

Function definition:

XPath 1.0:

count(node-set) => number

XPath 2.0:

fn:count($arg as item()*) as xs:integer

Possible compatibility problems with XPath 2.0 to XPath 1.0:

None. Under XPath 2.0 the function returns the value 0 in case of the passing on of the empty sequence. The same applies to the passing on of an empty node set under XPath 1.0.

   

<< back next >>

 

 

 


Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany