XML

XPath Functions

The XPath functions can be used in XSLT documents. We have already seen the name function being used as a parameter for the select attribute of the value-of element. The XPath functions can be divided into four groups: Boolean, string, node-set, and number functions.

Boolean functions

The Boolean functions can be used with comparison operators in filter patterns and return strings or numbers. They are as follows:

Boolean Functions

Name Description
boolean (XPathObject) Converts the argument to a Boolean. A number is true if it's not positive, negative zero, or NaN. A node-set is true if it's non-empty. A string is true if its length is non-zero.
false () Returns false.
true () Returns true.
lang (string) Used to determine what language should be used for characters; not currently implemented in the Internet Explorer 5 DOM.

String functions

The string functions are used to convert XPath objects to strings and to manipulate strings. They are shown in the following table:

String Functions

Name Description
concat (string, string, [string]) Concatenates strings. Can be passed to any number of string parameters.
contains (string1, string2) Returns true if the first argument contains the second argument. Returns false otherwise.
normalize-space (string) Returns the argument with all the white space normalized.
starts-with (string1, string2) Returns true if the first argument starts with the second. Returns false otherwise.
substring (string1, number1, number2) Returns the substring of the first argument starting at the second argument with length number2. If number2 is omitted, the function will continue to the end of the string.
substring-after (string1, string2) Returns the substring of the first argument that follows the first occurrence of the second argument in the first argument.
substring-before (string1, string2) Returns the substring of the first argument that precedes the first occurrence of the second argument in the first argument.
string (object) Converts an object to a string.
string-length (string) Returns the number of characters in a string.
translate (string1, string2, string3) Returns the first argument with occurrences of characters the second argument replaced by the character at the corresponding position in the third argument.

Node-set functions

The XPath node-set functions are used for filtering and selecting node-sets. The following functions are node-set functions:

Node-set Functions

Name Description
count (node-set) Returns the number of nodes in the node-set.
id (object) Selects an item by its unique ID. The object is an XPath object. Returns a node-set consisting of node/nodes with ID/IDs corresponding to the parameter passed after it is converted to a string.
last () Returns the number of nodes in the node-set.
local-name (node-set) Returns the local part of the expanded name.
name (node-set) Returns the expanded name as a string.
namespace-uri (node-set) Returns the namespace URI of the expanded-name of the node-set.
postion () Returns the index number of the node. The first node returns a position of 1.

Number functions

The number functions can be used with comparison operators and can return strings or numbers. They are shown in the following table:

Number Functions

Name Description
number (object) Converts its argument to a number. This function is not supported in the Internet Explorer 5 DOM.
ceiling (number) Returns the smallest (closest to negative infinity) number that is not less than the argument and that is an integer. This function is not supported in the Internet Explorer 5 DOM.
floor (number) Returns the largest (closest to positive infinity) number that is not greater than the argument and that is an integer. This function is not supported in the Internet Explorer 5 DOM.
round (number) Returns the number that is closest to the argument and that is an integer. If there are two such numbers, the one that is closest to positive infinity is returned. This function is not supported in the Internet Explorer 5 DOM.
sum (node-set) Returns the sum for each node in the argument node-set. The nodes are first converted to number values before summing. This function is not supported in the Internet Explorer 5 DOM.