An unpublished document used for training.

Available under GFDL, no invariant sections

Copyleft 2013 Martin Holmes

Structure/code adapted from basic_xhtml_cribsheet.xml. Content adapted from author's brain, W3C standards document, and djb_ten-functions.xml by David Birnbaum.

This is a small subset of useful XPath 2.0 functions. For a full reference, check out http://www.w3schools.com/xpath/xpath_functions.asp and http://www.w3.org/TR/xpath-functions/.

String functions (operations on text)

- Trims leading and trailing spaces, and collapses all
internal sequences of spaces to a single space.
returns: - Returns true or false, depending on whether the first string contains the second string or not.
returns: - A regular-expression replace function. Returns a copy
of string in which all instances of the pattern have been
replaced by the replacement.
returns: - Returns true if string1 starts with string2, and false if not.
returns:in the firstin the begins with "Chapter", false() if it doesn't. Beware: this is case-sensitive.]]>- Returns true if string1 ends with string2, and false if not.
returns:in the firstin the ends with "ide", false() if it doesn't. Beware: this is case-sensitive.]]>Functions for handling sequencesSequences are rather like arrays in traditional programming. In XPath, they are ubiquitous; most simple XPath expressions will return a sequence. For instance,

`//div/@type`

will return a sequence of all thetype attributes ondiv elements in the document.- This returns a sequence of only the
distinct values in a sequence. In other words, it removes duplicates.returns:elements.]]> - Returns true or false depending on whether the sequence is empty.
returns:elements in the, false() if not.]]>

Mathematical functions- Counts the items fed to it, and returns the total.

- Calculates the average from a sequence of numbers.
NOTE: the second example includes two sets of parentheses. The outer set contains the argument to the avg() function, and the inner set constructs a sequence which constitutes the argument to the function. In other words, avg() takes a single argument, which is a sequence; it cannot take a series of separate arguments. - Calculates the sum of a list of numbers.
Note the proliferating parentheses... - Returns the smallest from a list of numbers.
- Returns the largest a list of numbers.
- Rounds a number to the nearest integer. .5 is rounded up.
returns:

Boolean functions (dealing with true or false values)- returns true if the expression is false, and false if it is true.
returns:in the document, true() if there are 5 or fewer.]]> `true()`

and`false()`

are functions in XPath. if you're checking the result of a boolean operation, don't forget the parentheses!]]>

Context functions- returns the position of the node in its sequence.
returns:.]]> - returns the number of the last item in the sequence.
returns:.]]>

- Returns true if string1 ends with string2, and false if not.