View Old View New View Both View Only Previous Next

This draft contains only sections that have differences from the version that it modified.

W3C

XML Path Language (XPath) 4.0 WG Review Draft

W3C Editor's Draft 23 February 2026

This version:
https://qt4cg.org/specifications/xpath-40/
Most recent version of XPath:
https://qt4cg.org/specifications/xpath-40/
Most recent Recommendation of XPath:
https://www.w3.org/TR/2017/REC-xpath-31-20170321/
Editor:
Michael Kay, Saxonica <mike@saxonica.com>

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.


Abstract

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.

Status of this Document

This is a draft prepared by the QT4CG (officially registered in W3C as the XSLT Extensions Community Group). Comments are invited.

Dedication

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.


2 Basics

2.3 Processing Model

Changes in 4.0  

  1. The static typing option has been dropped.   [Issue 1343 PR 1344 3 September 2024]

The semantics of XPath 4.0 are defined in terms of the data model and the expression context.

Processing_ModelclusterQT4XPath Expression ProcessingclusterEPExternal processingExecExecutionEngineXDMXPath DataModelExec->XDM DQ4SerializeSerializeXDM->Serialize DM4ASTAbstractSyntax TreeAST->Exec DQ1AST->AST SQ5StaticStaticContextStatic->AST SQ4DynamicDynamicContextStatic->Dynamic DQ2Dynamic->Exec DQ5SchemaIn-ScopeSchemaDefinitionsSchema->StaticXPathXPathXPath->AST SQ1XMLXMLPSVIInfoset/PSVIXML->PSVI DM1JSONJSONJSON->XDM DM2PSVI->XDM DM2DirectOther/DirectGenerationDirect->XDM DM3XSDXMLSchemaXSD->Schema SI1DirectXSDOther/DirectGenerationDirectXSD->Schema SI2HostHostEnvironmentHost->Static SQ2Host->Dynamic DQ3

Figure 1: Processing Model Overview

Figure 1 provides a schematic overview of the processing steps that are discussed in detail below. Some of these steps are completely outside the domain of XPath 4.0; in Figure 1, these are depicted outside the line that represents the boundaries of the language, an area labeled external processing. The external processing domain includes generation of XDM instances that represent the data to be queried (see 2.3.1 Data Model Generation), schema import processing (see 2.3.2 Schema Import Processing), and serialization. The area inside the boundaries of the language is known as the XPath processing domain, which includes the static analysis and dynamic evaluation phases (see 2.3.3 Expression Processing). Consistency constraints on the XPath processing domain are defined in 2.3.6 Consistency Constraints.

2.3.3 Expression Processing

XPath 4.0 defines two phases of processing called the static analysis phase and the dynamic evaluation phase (see Figure 1). During the static analysis phase, static errors, dynamic errors, or type errors may be raised. During the dynamic evaluation phase, only dynamic errors or type errors may be raised. These kinds of errors are defined in 2.4.1 Kinds of Errors.

Within each phase, an implementation is free to use any strategy or algorithm whose result conforms to the specifications in this document.

2.3.3.1 Static Analysis Phase

[Definition: The static analysis phase depends on the expression itself and on the static context. The static analysis phase does not depend on input data (other than schemas).]

During the static analysis phase, the XPath expression is typically parsed into an internal representation called the operation tree (step SQ1 in Figure 1). A parse error is raised as a static error [err:XPST0003]. The static context is initialized by the implementation (step SQ2). The static context is used to resolve schema type names, function names, namespace prefixes, and variable names (step SQ4). If a name of one of these kinds in the operation tree is not found in the static context, a static error ([err:XPST0008] or [err:XPST0017]) is raised (however, see exceptions to this rule in 3.2.7.2 Element Types and 3.2.7.3 Attribute Types.)

The operation tree is then typically normalized by making explicit the implicit operations such as atomization and extraction of effective boolean values (step SQ5).

During the static analysis phase, a processor may perform type analysis. The effect of type analysis is to assign a static type to each expression in the operation tree. [Definition: The static type of an expression is the best inference that the processor is able to make statically about the type of the result of the expression.] This specification does not define the rules for type analysis nor the static types that are assigned to particular expressions: the only constraint is that the inferred type must match all possible values that the expression is capable of returning.

Examples of inferred static types might be:

  • For the expression concat(a,b) the inferred static type is xs:string

  • For the expression $a = $v the inferred static type is xs:boolean

  • For the expression $s[exp] the inferred static type has the same item type as the static type of $s, but a cardinality that allows the empty sequence even if the static type of $s does not allow an empty sequence.

  • The inferred static type of the expression data($x) (whether written explicitly or inserted into the operation tree in places where atomization is implicit) depends on the inferred static type of $x: for example, if $x has type element(*, xs:integer) then data($x) has static type xs:integer.

In XQuery 1.0 and XPath 2.0, rules for static type inferencing were published normatively in [XQuery 1.0 and XPath 2.0 Formal Semantics], but implementations were allowed to refine these rules to infer a more precise type where possible. In subsequent versions, the rules for static type inferencing are entirely implementation-dependent.

Every kind of expression also imposes requirements on the type of its operands. For example, with the expression substring($a, $b, $c), $a must be of type xs:string (or something that can be converted to xs:string by the function calling rules), while $b and $c must be numeric.

A processor may raise a type error during static analysis if the inferred static type of an expression has no overlap (intersection) with the required type, and cannot be converted to the required type using the coercion rules. For example, given the call fn:upper-case($s), the processor may raise an error if the declared or inferred type of $s is xs:integer, but not if it is xs:anyAtomicType.

In addition, type analysis may conclude that an expression is implausibleimplausible. Implausible expressions may be considered erroneous unless such checks have been disabled. For example, the expression round(tokenize($input)) is implausible because the required type for fn:round is xs:numeric?, while the static type of tokenize($input) is xs:string*, and these two sequence types are substantively disjoint. This topic is described further in 2.4.6 Implausible Expressions.

Alternatively, the processor may defer all type checking until the dynamic evaluation phase.