4.2. XPath functions

XPath 2.0 expressions can use more than 200 predefined functions described in [33]. It is also possible for a user to define her own functions with an XSLT function template described in Section 5.1. Table 4.1 presents a personal selection of what we consider to be the most useful XPath functions separated in broad categories. The XSL stylesheets of the next chapter will show many more uses of these functions.

Table 4.1. A selection of XPath functions

Document information
document-uri(node) a string describing the URI of node
   
Mathematics
abs(numeric) absolute value of numeric
ceiling(numeric) smallest integer greater than numeric
floor(numeric) biggest integer smaller than numeric
   
Strings
concat(arg1,arg2 ...) string concatenating the string values of argi
string-join(strings, sep) string concatenating the members of the sequence strings using the string sep as a separator
substring(string, start, length) string portion with string, from position start (the first character is numbered 1) for length characters; if length is not given, then the rest of the string is returned.
string-length(string) number of characters in string
   
normalize-space(string) string after removing leading and trailing blanks and replacing sequences of one or more than one whitespace character with a single space (#x20)
translate(string, mapStr, transStr) string in which every character appearing at position N in the mapStr is replaced by the character at position N in the transStr
lower-case upper-case (string) all lower or upper case version of string
   
contains(string1, string2) checks if string1 contains string2
starts-with(string1, string2) checks if string1 starts with string2
ends-with(string1, string2) checks if string1 ends with string2
substring-before(string1, string2) string of chars of string1 before the first occurrence of string2
substring-after(string1, string2) string of chars of string1 after the first occurrence of string2
   
matches(string,pattern) checks if string matches the regular expression pattern; unless ^ and $ are used as anchors, the string matches if one of its substring does
replace(string,pattern,replacement) string obtained after replacing within string every non-overlapping occurrence of pattern by replacement
tokenize(string,pattern) break string into a sequence of strings, treating every substring matching pattern as a separator
   
Sequences
distinct-values(seq) sequence of different values that appear at least once in seq
remove(seq, position) sequence of all elements of seq except for the one at position
reverse(seq) sequence of all elements of seq but in reverse order
subsequence(seq, start, length) sequence of elements of seq, from position start (the first element is numbered 1) for length elements; if length is not given, then the rest of the sequence is returned.
   
count(seq) number of elements within seq
avg|max|min|sum(seq) average | maximum | minimum | sum of the values within seq
   
Context
position() position of the context item within the current sequence of items
last() the size of the current sequence of items
current-date|current-time() date or time value of now

A selection of XPath functions (taken from [6]) grouped in categories. The names of the formal parameters are chosen to indicate the expected type or use of the actual parameter. When many functions with the same parameters have a similar behavior, their names are separated by a vertical bar (|); the actual call uses only one choice.