XPath 1.0 defines 27 built-in functions for use in XPath expressions. Various technologies that use XPath, such as XSLT and XPointer, also extend this list with functions they need. XSLT even allows user-defined extension functions.
Every function is evaluated in the context of a particular node, called the context node. The higher-level specification in which XPath is used, such as XSLT or XPointer, decides exactly how this context node is determined. In some cases the function operates on the context node. In other cases it operates on the argument, if present, and the context node, if no argument exists. The context node is ignored in other cases.
In the following sections, each function is described with at least one signature in this form:
return-type function-name(type argument, type argument, ...)
Compared to languages like Java, XPath argument lists are quite loose. Some XPath functions take a variable number of arguments and fill in the arguments that are omitted with default values or the context node.
Furthermore, XPath is weakly typed. If you pass an argument of the wrong type to an XPath function, it generally converts that argument to the appropriate type using the boolean( ), string( ), or number( ) functions, described later. The exceptions to the weak-typing rule are the functions that take a node-set as an argument. Standard XPath 1.0 provides no means of converting anything that isn't a node-set into a node-set. In some cases a function can operate equally well on multiple argument types. In this case, its type is given simply as object.
boolean( ) |
boolean boolean(object o) |
Zero and NaN are false. All other numbers are true.
Empty node-sets are false. Nonempty node-sets are true.
Empty strings are false. Nonempty strings are true.
ceiling( ) |
number ceiling(number x) |
concat( ) |
string concat(string s1, string s2) string concat(string s1, string s2, string s3) string concat(string s1, string s2, string s3, string s4) ... |
contains( ) |
boolean contains(string s1, string s2) |
count( ) |
number count(node-set set) |
false( ) |
boolean false( ) |
floor(3e ) |
number floor(number x) |
id( ) |
node-set id(string IDs) node-set id(node-set IDs) |
lang( ) |
boolean lang(string languageCode) |
The lang( ) function takes into account country and other subcodes before making its determination. For example, lang('fr') returns true for elements whose language code is fr-FR, fr-CA, or fr. However, lang('fr-FR') is not true for elements whose language code is fr-CA or fr.
last( ) |
number last( ) |
local-name( ) |
string local-name( ) string local-name(node-set nodes) |
name( ) |
string name( ) string name(node-set nodes) |
namespace-uri( ) |
string namespace-uri( ) string namespace-uri(node-set nodes) |
normalize-space( ) |
string normalize-space( ) string normalize-space(string s) |
not( ) |
boolean not(boolean b) |
number( ) |
number number( ) number number(object o) |
A string is converted by first stripping leading and trailing whitespace and then picking the IEEE 754 value that is closest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string. If the string does not seem to represent a number, it is converted to NaN. Exponential notation (e.g., 75.2E-12) is not recognized.
True Booleans are converted to 1; false Booleans are converted to 0.
Node-sets are first converted to a string as if by the string( ) function. The resulting string is then converted to a number.
If the argument is omitted, then it converts the context node.
position( ) |
number position( ) |
round( ) |
number round(number x) |
starts-with( ) |
boolean starts-with(string s1, string s2) |
string( ) |
string string( ) string string(object o) |
A node-set is converted to the string value of the first node in the node-set. If the node-set is empty, it's converted to the empty string.
A number is converted to a string as follows:
NaN is converted to the string NaN.
Positive Inf is converted to the string Infinity.
Negative Inf is converted to the string -Infinity.
Integers are converted to their customary English form with no decimal point and no leading zeros. A minus sign is used if the number is negative, but no plus sign is used for positive numbers.
Nonintegers (numbers with nonzero fractional parts) are converted to their customary English form with a decimal point, with at least one digit before the decimal point and at least one digit after the decimal point. A minus sign is used if the number is negative, but no plus sign is used for positive numbers.
A Boolean with the value true is converted to the English word "true." A Boolean with the value false is converted to the English word "false." Lowercase is always used.
The object to be converted is normally passed as an argument, but if omitted, the context node is converted instead.
WARNING: The XPath specification specifically notes that the "string function is not intended for converting numbers into strings for presentation to users." The primary problem is that it's not localizable and not attractive for large numbers. If you intend to show a string to an end user, use the format-number( ) function and/or xsl:number element in XSLT instead.
string-length( ) |
number string-length(string s) number string-length( ) |
substring( ) |
string substring(string s, number index, number length) string substring(string s, number index) |
substring-after( ) |
string substring-after(string s1, string s2) |
substring-before( ) |
string substring-before(string s1, string s2) |
sum( ) |
number sum(node-set nodes) |
translate( ) |
string translate(string s1, string s2, string s3) |
true( ) |
boolean true( ) |
Copyright © 2002 O'Reilly & Associates. All rights reserved.