The XPath language


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:

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:

Location paths

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.

Location steps

A location step consists of three parts:

  1. an axis, which specifies the tree relationship between the nodes selected by the location step and the context node;
  2. a node test, which specifies the node type and expanded-name of the nodes selected by the location step;
  3. zero or more predicates, which use arbitrary expressions to further refine the set of nodes selected by the location step.

    axis-name :: node-test[predicate]*

Axes

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'

Abbreviated syntax examples

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.

Expressions

Parentheses may be used for grouping.

Function calls

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.

Booleans

The precedence order is (from least important to most important):

  1. or
  2. and
  3. =, !=
  4. <=, <, =>=>

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 &lt;. However, it might be easier to invert the inequality and use > instead.

XPath core function library (in alphabetical order)

For each function its type, argument(s), and the section where it is defined in the XPath Specification is given.

boolean boolean(object)
boolean function (Section 4.3).
number ceiling(number)
number function (Section 4.4).
string concat(string,string,string*)
string function (Section 4.2).
string contains(string,string)
string function (Section 4.2).
number count(node-set)
node-set function (Section 4.1).
boolean false()
boolean function (Section 4.3).
number floor(number)
number function (Section 4.4).
node-set id(object)
node-set function (Section 4.1).
boolean lang(string)
boolean function (Section 4.3).
number last()
node-set function (Section 4.1).
string local-name(node-set?)
node-set function (Section 4.1).
string name(node-set?)
node-set function (Section 4.1).
string namespace-uri(node-set?)
node-set function (Section 4.1).
string normalize(string?)
string function (Section 4.2).
boolean not(boolean)
boolean function (Section 4.3).
number number(object?)
number function (Section 4.4).
number position()
node-set function (Section 4.1).
number round(number)
number function (Section 4.4).
string starts-with(string,string)
string function (Section 4.2).
string string(object?)
string function (Section 4.2).
number string-length(string?)
string function (Section 4.2).
string substring(string,number,number?)
string function (Section 4.2).
string substring-after(string,string)
string function (Section 4.2).
string substring-before(string,string)
string function (Section 4.2).
number sum(node-set)
number function (Section 4.4).
string translate(string,string,string)
string function (Section 4.2).
boolean true()
boolean function (Section 4.3).

PREVIOUSFIRSTLASTNEXT