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.
It is a static error if analysis of an expression relies on some component of the static context that is absentDM.
It is a type error if evaluation of an expression relies on some part of the dynamic context that is absentDM.
Note:
In version 4.0 this has been reclassified as a type error rather than a dynamic error. This change allows a processor to report the error during static analysis where possible; for example if the body of a user-defined function is written as fn($x) { @code }. The error code is prefixed XPDY rather than XPTY for backwards compatibility reasons.
It is a static error if an expression is not a valid instance of the grammar defined in A.1 EBNF.
It is a type error if, during the static analysis phase, an expression is found to have a static type that is not appropriate for the context in which the expression occurs, or during the dynamic evaluation phase, the dynamic type of a value does not match a required type as specified by the matching rules in 3.1.2 Sequence Type Matching.
During the analysis phase, an expression is classified as implausible if the inferred static typeS and the required type R are substantively disjoint; more specifically, if neither of the types is a subtype of the other, and if the only values that are instances of both types are one or more of: the empty sequence, the empty map, and the empty array.
It is a static error if an expression refers to an element name, attribute name, schema type name, or variable name that is not defined in the static context, except for an ElementName in an ElementTest or an AttributeName in an AttributeTest.
An implementation that does not support the namespace axis must raise a static error if it encounters a reference to the namespace axis and XPath 1.0 compatibility mode is false.
It is a static error if the expanded QName and number of arguments in a static function call do not match the name and arity range of a function definition in the static context, or if an argument keyword in the function call does not match a parameter name in that function definition, or if two arguments in the function call bind to the same parameter in the function definition.
It is a type error if the result of a path operator contains both nodes and non-nodes.
It is a type error if E1 in a path expression E1/E2 does not evaluate to a sequence of nodes.
It is a type error if, in an axis step, the context item is not a node.
It is a static error if two fields in a record declaration have the same name.
It is a static error if a recursive record type cannot be instantiated (typically because it contains a self-reference that is neither optional nor emptiable). Processors are not required to detect this error.
It is a static error for an inline function expression to have more than one parameter with the same name.
An implementation MAY raise a static error if the value of a BracedURILiteral is of nonzero length and is neither an absolute URI nor a relative URI.
It is a dynamic error if the dynamic type of the operand of a treat expression does not match the sequence type designated by the treat expression. This error might also be raised by a path expression beginning with / or // if the context node is not in a tree that is rooted at a document node. This is because a leading / or // in a path expression is an abbreviation for an initial step that includes the clause treat as document-node().
It is a static error if an expanded QName used as an ItemType in a is not defined in the static context either as a named item type in the in-scope named item types, or as a generalized atomic type in the in-scope schema type.
The type named in a cast or castable expression must be the name of a type defined in the in-scope schema types, and the type must be simple.
A static error is raised if any of the following conditions is statically detected in any expression:
The prefix xml is bound to some namespace URI other than http://www.w3.org/XML/1998/namespace.
A prefix other than xml is bound to the namespace URI http://www.w3.org/XML/1998/namespace.
The prefix xmlns is bound to any namespace URI.
A prefix other than xmlns is bound to the namespace URI http://www.w3.org/2000/xmlns/.
It is a static error if the target type of a cast or castable expression is xs:NOTATION, xs:anySimpleType, or xs:anyAtomicType.
It is a static error if a QName used in an expression contains a namespace prefix that cannot be expanded into a namespace URI by using the statically known namespaces.
It is a static error if a variable bound in a for expression, and its associated positional variable, do not have distinct names (expanded QNames).
It is a static error if the annotation %method is used on a focus function.
When applying the coercion rules, if an item is of type xs:untypedAtomic and the expected type is namespace-sensitive, a type error [err:XPTY0117] is raised.
An implementation-dependent limit has been exceeded.
The namespace axis is not supported.
No two keys in a map may have the same key value.
It is a static error if a named item type declaration is recursive, unless it satisfies the conditions defined in 3.2.8.3.1 Recursive Record Types.
In a forexpression, when the keyword member is present, the value of the binding collection must be a single array; and when either or both of the keywords key and value are present, the value of the binding collection must be a single map.
During the analysis phase, an axis step is classified as implausible if the combination of the inferred context item type, the choice of axis, and the supplied node test, is such that the axis step will always return an empty sequence.
During the analysis phase, a unary or postfix lookup expression is classified as implausible if the combination of the inferred type of the left-hand operand (or the context item type in the case of a unary expression) and the choice of key specifier is such that the lookup expression will always return an empty sequence.
It is a static error if a key type named in a is not a generalized atomic type.