XPath is the result of an effort to provide a common syntax and semantics for functionality shared between XSL Transformations (XSLT) and XPointer.
The primary purpose of XPath is to address parts of an XML document.
XPath also provides basic facilities for manipulation of strings, numbers and booleans.
The primary syntactic construct in XPath is the expression. An expression matches the production Expr. An expression is evaluated to yield an object, which has one of the following four basic types:
node-set, an unordered collection of nodes without duplicates;
boolean, true or false;
number, a floating-point number;
string, a sequence of Unicode characters;
Expression evaluation occurs with respect to a context. XSLT and XPointer specify how the context is determined for XPath expressions used in XSLT and XPointer respectively. The context consists of:
the context node;
a pair of non-zero positive integers (the context position and the context size);
a set of variable bindings;
a function library;
the set of namespace declarations in scope for the expression.
Location paths, the most important construct in XPath, can be expressed using a straightforward but rather verbose syntax. There are also a number of syntactic abbreviations that allow common cases to be expressed concisely.
A location step consists of three parts:
axis-name :: node-test[predicate]*
|
The list of allowed axis types is the following:
'ancestor' 'ancestor-or-self' 'attribute' 'child' 'descendant' 'descendant-or-self' 'following' 'following-sibling' 'namespace' 'parent' 'preceding' 'preceding-sibling' 'self' |
div/para is short for child::div/child::para
../title is short for parent::node()/child::title
//para is short for /descendant-or-self::node()/child::para
.//para is short for self::node()/descendant-or-self::node()/child::para
para[@type="warning"] is short for child::para[attribute::type="warning"]
The following figure explains in a visual way most of the axes.
Parentheses may be used for grouping.
An argument is converted to type string as if by calling the string function. An argument is converted to type number as if by calling the number function. An argument is converted to type boolean as if by calling the boolean function. An argument that is not of type node-set cannot be converted to a node-set. It is an error if the number or type of arguments is wrong.
The precedence order is (from least important to most important):
The operators are all left associative. For example, 3 > 2 > 1 is equivalent to (3 > 2) > 1, which evaluates to false.
Note that the < character must be escaped as <. However, it might be easier to invert the inequality and use > instead.
For each function its type, argument(s), and the section where it is defined in the XPath Specification is given.
![]() | ![]() ![]() | ![]() |