XPath/XQuery string-length function

Summary

Returns the number of characters in a string.

Signatures

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

Properties

The zero-argument form of this function is deterministic, context-dependent, and focus-dependent.
The one-argument form of this function is deterministic, context-independent, and focus-independent.

Rules

The function returns an xs:integer equal to the length in characters of the value of $arg.

Calling the zero-argument version of the function is equivalent to calling fn:string-length(fn:string(.)).

If the value of $arg is the empty sequence, the function returns the xs:integer value zero (0).

Examples

The expression fn:string-length("Harp not on that string, madam; that is past.") returns 45.

The expression fn:string-length(()) returns 0.

Error Conditions

If $arg is not specified and the context item is absent, a dynamic error is raised: .

Notes

Unlike some programming languages, a codepoint greater than 65535 counts as one character, not two.

There are situations where fn:string-length() has a different effect from fn:string-length(.). For example, if the context item is an attribute node typed as an xs:integer with the string value 000001, then fn:string-length() returns 6 (the length of the string value of the node), while fn:string-length(.) raises a type error (because the result of atomization is not an xs:string).