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



String functions – analysis and manipulation


XPath 1.0

Return value:

An integer of xs:integer type; the number of characters in a string passed on.




Optional. The argument is a xs:string string whose number of characters shall be determined. If the argument is not a string, a type error is reported. The empty sequence is permitted as value and is treated like the empty string (the function returns 0).

If no argument is passed on, the number of characters of the string value of the context item is determined. Its value is, in case it is not a string, converted to a string according to the rules of the conversion function fn:string().

Purpose of use:

The fn:string-length() function counts the number of characters of a string passed on to it. In case no argument is passed on, it returns the number of characters of the currently valid context item:

fn:string-length() corresponds to fn:string-length(.).

If the context item is undefined at this moment, a runtime error is reported (err:XPDY0002).

The function returns the value 0 as input value for an empty string as well as for an empty sequence. Therefore, distinction cannot be made between both.

All characters are considered according to the usual XML rules, which means also characters consisiting of a so-called Unicode Surrogate Pair – a pair of 16bit values – are counted as a single character.

It should be noted that entities are replaced or expanded by the XML parser before the XSLT processor applies the fn:string-length() function. Characters which are represented in the document source code by character entities like E or XML entity references like & are therefore counted as single characters. This also applies for self-defined replacement text entities. For these entities, the characters of the expansion text are counted.


Function callResult
fn:string-length('abc') 3
fn:string-length('') 0
fn:string-length(()) 0
fn:string-length("This is an example")18
fn:string-length(@example)0, in case the attribute does not exist; otherwise the number of characters of the @ value

Table: function call of fn:string-length()

Function definition:

XPath 1.0:

string-length(string?) => number

XPath 2.0:

fn:string-length() as xs:integer

fn:string-length($arg as xs:string?) as xs:integer


<< 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