The primary purpose of XPath is to address the nodes of XML trees and JSON trees. XPath gets its name from its use of a path notation for navigating through the hierarchical structure of an XML document. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath 4.0 adds a similar syntax for navigating JSON trees.
[Definition: XPath 4.0 operates on the abstract, logical structure of an XML document or JSON object, rather than its surface syntax. This logical structure, known as the data model, is defined in [XQuery and XPath Data Model (XDM) 3.1].]
XPath is designed to be embedded in a host language such as [XSL Transformations (XSLT) Version 3.0] or [XQuery 3.1: An XML Query Language]. [Definition: A host language for XPath is a language or specification that incorporates XPath as a sublanguage and that defines how the static and dynamic context for evaluation of XPath expressions are to be established.]
XPath 4.0 is a subset of XQuery 4.0. In general, any expression that is syntactically valid and executes successfully in both XPath 4.0 and XQuery 4.0 will return the same result in both languages. There are a few exceptions to this rule:
Because XQuery expands
predefined entity references and character references
and XPath does not, expressions containing these produce different
results in the two languages. For instance, the value of the string literal
"&"
is &
in XQuery,
and &
in XPath. (XPath is often embedded in other
languages, which may expand predefined entity references or character references
before the XPath expression is evaluated.)
If XPath 1.0 compatibility mode is enabled, XPath behaves differently from XQuery in a number of ways, which are noted throughout this document, and listed in H.4.2 Incompatibilities when Compatibility Mode is false.
Because these languages are so closely related, their grammars and language descriptions are generated from a common source to ensure consistency, and the editors of these specifications work together closely.
XPath 4.0 also depends on and is closely related to the following specifications:
[XQuery and XPath Data Model (XDM) 3.1] defines the data model that underlies all XPath 4.0 expressions.
The type system of XPath 4.0 is based on XML Schema. It is implementation-defined whether the type system is based on [XML Schema 1.0] or [XML Schema 1.1].
The built-in function library and the operators supported by XPath 4.0 are defined in [XQuery and XPath Functions and Operators 4.0].
This document specifies a grammar for XPath 4.0, using the same basic EBNF notation used in [XML 1.0]. Unless otherwise noted (see A.2 Lexical structure), whitespace is not significant in expressions. Grammar productions are introduced together with the features that they describe, and a complete grammar is also presented in the appendix [A XPath 4.0 Grammar]. The appendix is the normative version.
In the grammar productions in this document, named symbols are underlined and literal text is enclosed in double quotes. For example, the following productions describe the syntax of a static function call:
[79] | FunctionCall |
::= |
EQName
ArgumentList
|
/* xgc: reserved-function-names */ |
/* gn: parens */ | ||||
[61] | ArgumentList |
::= | "(" ((PositionalArguments ("," KeywordArguments)?) | KeywordArguments)? ")" |
The productions should be read as follows: A function call consists of an EQName followed by an ArgumentList. The argument list consists of an opening parenthesis, an optional list of one or more arguments (separated by commas), and a closing parenthesis.
This document normatively defines the static and dynamic semantics of XPath 4.0. In this document, examples and material labeled as "Note" are provided for explanatory purposes and are not normative.
Certain aspects of language processing are described in this specification as implementation-defined or implementation-dependent.
[Definition: Implementation-defined indicates an aspect that may differ between implementations, but must be specified by the implementor for each particular implementation.]
[Definition: Implementation-dependent indicates an aspect that may differ between implementations, is not specified by this or any W3C specification, and is not required to be specified by the implementor for any particular implementation.]
A language aspect described in this specification as implementation-defined or implementation dependent may be further constrained by the specifications of a host language in which XPath is embedded.