Functions
XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.
Xpath Functions return a string, a number, or a Boolean value.
For example, the expression /PROJECT/text() gets the string-value of PROJECT nodes.
Many functions depend on the current context. In the preceding example, the context for each invocation of the text() function is the PROJECT node that is currently selected.
For more information about functions, see section 4 of the XPath specification.
Articles Related
Type
Namespace
These functions let you determine the namespace characteristics of a node.
- local-name(): Returns the name of the current node, minus the namespace prefix. Ie the element name (9/10 what we need). For example, /*[local-name()!='HEAD'] selects all elements excepted the HEAD elements.
- local-name(…): Returns the name of the first node in the specified node set, minus the namespace prefix.
- namespace-uri(): Returns the namespace URI from the current node.
- namespace-uri(…): Returns the namespace URI from the first node in the specified node-set.
- name(): Returns the expanded name (URI plus local name) of the current node.
- name(…): Returns the expanded name (URI plus local name) of the first node in the specified node-set.
Node-Set
Many XPath expressions select a set of nodes. In essence, they return a node-set.
The id(…) function returns the node with the specified ID (
not the position (ie index)
)
Positional
These functions return positionally based numeric values.
- last(): Returns the index of the last element. For example, /HEAD[last()] selects the last HEAD element.
- position(): Returns the index position. For example, /HEAD[position() ⇐ 5] selects the first five HEAD elements.
- count(…): Returns the count of elements. For example, /HEAD[count(HEAD)=0] selects all HEAD elements that have no subheads.
String
These functions operate on or return strings.
- concat(string, string, …): Concatenates the string values.
- starts-with(string1, string2): Returns true if string1 starts with string2.
- contains(string1, string2): Returns true if string1 contains string2.
- substring-before(string1, string2): Returns the start of string1 before string2 occurs in it.
- substring-after(string1, string2): Returns the remainder of string1 after string2 occurs in it.
- substring(string, idx): Returns the substring from the index position to the end, where the index of the first char = 1.
- substring(string, idx, len): Returns the substring of the specified length from the index position.
- string-length(): Returns the size of the context node's string-value; the context node is the currently selected node-the node that was selected by an XPath expression in which a function such as string-length() is applied.
- string-length(string): Returns the size of the specified string.
- normalize-space(): Returns the normalized string-value of the current node (no leading or trailing white space, and sequences of white space characters converted to a single space).
- normalize-space(string): Returns the normalized string-value of the specified string.
- translate(string1, string2, string3): Converts string1, replacing occurrences of characters in string2 with the corresponding character from string3.
Note - XPath defines three ways to get the text of an element:
- text(),
- string(object),
- and the string-value implied by an element name in an expression like this: /PROJECT[PERSON=“Fred”].
Boolean
These functions operate on or return Boolean values.
- not(…): Negates the specified Boolean value.
- true(): Returns true.
- false(): Returns false.
- lang(string): Returns true if the language of the context node (specified by xml:Lang attributes) is the same as (or a sub-language of) the specified language; for example, Lang(“en”) is true for …
.
Numeric
These functions operate on or return numeric values.
- sum(…): Returns the sum of the numeric value of each node in the specified node-set.
- floor(N): Returns the largest integer that is not greater than N.
- ceiling(N): Returns the smallest integer that is not less than N.
- round(N): Returns the integer that is closest to N.
Conversion
These functions convert one data type to another.
- string(…): Returns the string value of a number, Boolean, or node-set.
- boolean(…): Returns a Boolean value for a number, string, or node-set (a non-zero number, a non-empty node-set, and a non-empty string are all true).
- number(…): Returns the numeric value of a Boolean, string, or node-set (true is 1, false is 0, a string containing a number becomes that number, the string-value of a node-set is converted to a number).