Please check the errata for any errors or issues reported since publication.
See also translations.
This document is also available in these non-normative formats: XML.
Copyright © 2000 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and document use rules apply.
XPath 4.0 is an expression language that allows the processing of values conforming to the data model defined in [XQuery and XPath Data Model (XDM) 4.0]. The name of the language derives from its most distinctive feature, the path expression, which provides a means of hierarchic addressing of the nodes in an XML tree. As well as modeling the tree structure of XML, the data model also includes atomic items, function items, maps, arrays, and sequences. This version of XPath supports JSON as well as XML, and adds many new functions in [XQuery and XPath Functions and Operators 4.0].
XPath 4.0 is a superset of XPath 3.1. A detailed list of changes made since XPath 3.1 can be found in I Change Log.
This is a draft prepared by the QT4CG (officially registered in W3C as the XSLT Extensions Community Group). Comments are invited.
The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).
Michael was central to the development of XML and many related technologies. He brought a polymathic breadth of knowledge and experience to everything he did. This, combined with his indefatigable curiosity and appetite for learning, made him an invaluable contributor to our project, along with many others. We have lost a brilliant thinker, a patient teacher, and a loyal friend.
The grammar in A.1 EBNF normatively defines built-in precedence among the operators of XPath. These operators are summarized here to make clear the order of their precedence from lowest to highest. The associativity column indicates the order in which operators of equal precedence in an expression are applied (further details below).
| # | Operator | Associativity |
|---|---|---|
| 1 | , (comma) | eitherassociative |
| 2 | for,let,some, every, if | NA |
| 3 | or | eitherassociative |
| 4 | and | eitherassociative |
| 5 | eq, ne, lt, le, gt, ge, =, !=, <, <=, >, >=, is, <<, >> | NA |
| 6 | otherwise | eitherassociative |
| 7 | || | left-to-rightassociative |
| 8 | to | NA |
| 9 | +, - (binary) | left-to-right |
| 10 | *, div, idiv, mod | left-to-right |
| 11 | union, | | eitherassociative |
| 12 | intersect, except | left-to-right |
| 13 | instance of | NA |
| 14 | treat as | NA |
| 15 | castable as | NA |
| 16 | cast as | NA |
| 17 | =>, =!> | left-to-right |
| 18 | -, + (unary) | right-to-left |
| 19 | ! | left-to-right |
| 20 | /, // | left-to-right |
| 21 | a[ b], a?[b], a?b, a??b, a(b) | left-to-right |
| 22 | ? (unary) | NA |
In the "Associativity" column, "either" indicates that all the operators at that level have the associative property (i.e., (A op B) op C is equivalent to A op (B op C)), so their associativity is inconsequential. "NA" (not applicable) indicates that the EBNF does not allow an expression that directly contains multiple operators from that precedence level, so the question of their associativity does not arise.
Entries in the Associativity column have the following meaning (where the symbol ⊙ represents any operator):
associative means that the order of evaluation is immaterial: for example a, b, c can be evaluated either as (a, b), c or as a, (b, c), producing the same result either way.
NA indicates that it is not possible to write an expression of the form a ⊙ b ⊙ c for this particular operator: for example, the grammar does not allow a = b = c.
left-to-right means that for expressions using these operators, a ⊙ b ⊙ c is evaluated as (a ⊙ b) ⊙ c. As a special case, the operators + and * are associative provided they are not mixed with other operators of the same precedence.
right-to-left is used only for unary operators, and indicates that ⊙ ⊙ a is evaluated as ⊙ (⊙ a)
These rules do not constrain the order in which the operands of an expression are evaluated (which might affect error behavior). See also 2.4 Error Handling.
Note:
Parentheses can be used to override the operator precedence in the usual way. Square brackets in an expression such as A[B]A?[B]or serve two roles: they act as an operator causing B to be evaluated once for each item in the value of A, and they act as parentheses enclosing the expression B.