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 basic building block of XPath 4.0 is the expression, which is a string of [Unicode] characters; the version of Unicode to be used is implementation-defined. The language provides several kinds of expressions which may be constructed from keywords, symbols, and operands. In general, the operands of an expression are other expressions. XPath 4.0 allows expressions to be nested with full generality.
Note:
This specification contains no assumptions or requirements regarding the character set encoding of strings of [Unicode] characters.
Like XML, XPath 4.0 is a case-sensitive language. Keywords in XPath 4.0 use lower-case characters and are not reserved—that is, names in XPath 4.0 expressions are allowed to be the same as language keywords, except for certain unprefixed function-names listed in A.4 Reserved Function Names.
In this specification the phrases must, must not, should, should not, may, required, and recommended, when used in normative text and rendered in small capitals, are to be interpreted as described in [RFC2119].
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 implementer 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 implementer 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.
The term atomic value has been replaced by atomic item. [Issue 1337 PR 1361 2 August 2024]
The terms XNode and JNode are introduced; the existing term node remains in use as a synonym for XNode where the context does not specify otherwise. [Issue 2025 PR 2031 13 June 2025]
[Definition: In the data model, a value is always a sequence.]
[Definition: A sequence is an ordered collection of zero or more items.]
[Definition: An item is either an atomic item, a node, or a function item.]
[Definition: An atomic item is a value in the value space of an atomic type, as defined in [XML Schema 1.0] or [XML Schema 1.1].]
[DefinitionDefinition: AAn nodeXNode is an instance of one of the node kinds defined in [TITLE OF DM40 SPEC, TITLE OF Node SECTION]Section 7.1 XML NodesDM40DM.] Each nodeXNode has a unique node identity, a typed value, and a string value. In addition, some nodesXNodes have a name. The typed value of a nodean XNode is a sequence of zero or more atomic items. The string value of a nodean XNode is a value of type xs:string. The name of a nodean XNode is a value of type xs:QName.
[Definition: Except where the context indicates otherwise, the term node is used as a synonym for XNode.]
[Definition: A function item is an item that can be called using a dynamic function call.]
Maps (see 4.13.1 Maps) and arrays (see 4.13.2 Arrays) are specific kinds of function items.
[Definition: A sequence containing exactly one item is called a singleton.] An item is identical to a singleton sequence containing that item. Sequences are never nested—for example, combining the values 1, (2, 3), and ( ) into a single sequence results in the sequence (1, 2, 3). [Definition: A sequence containing zero items is called an empty sequence.]
[Definition: The term XDM instance is used, synonymously with the term value, to denote an unconstrained sequence of items.]
The semantics of XPath 4.0 are defined in terms of the data model and the expression context.
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.
The input data for an expression must be represented as one or more XDM instances. This process occurs outside the domain of XPath 4.0, which is why Figure 1 represents it in the external processing domain.
In many cases the input data might originate as XML. Here are some steps by which an XML document might be converted to an XDM instance:
A document may be parsed using an XML parser that generates an XML Information Set (see [XML Infoset]). The parsed document may then be validated against one or more schemas. This process, which is described in [XML Schema 1.0 Part 1] or [XML Schema 1.1 Part 1], results in an abstract information structure called the Post-Schema Validation Infoset (PSVI). If a document has no associated schema, its Information Set is preserved. (See DM1 in Figure 1)
The Information Set or PSVI may be transformed into an XDM instance by a process described in [XQuery and XPath Data Model (XDM) 4.0]. (See DM2 in Figure 1)
The above steps provide an example of how an XDM instance might be constructed. An XDM instance might also be constructed in some other way (see DM3 in Figure 1), for example it might be synthesized directly from a relational database, or derived by parsing a JSON text or a CSV file. Whatever the origin, XPath 4.0 is defined in terms of the data model, but it does not place any constraints on how XDM instances are constructed.
The remainder of this section is concerned with the common case where XML data is being processed.
[Definition: Each element node and attribute node in an XDM instance has a type annotation (described in Section 4.1 Schema InformationDM). The type annotation of a node is a reference to a schema type. ] The type-name of a node is the name of the type referenced by its type annotation (but note that the type annotation can be a reference to an anonymous type). If the XDM instance was derived from a validated XML document as described in Section 6.5.37.3.3 Construction from a PSVIDM, the type annotations of the element and attribute nodes are derived from schema validation. XPath 4.0 does not provide a way to directly access the type annotation of an element or attribute node.
The value of an attribute is represented directly within the attribute node. An attribute node whose type is unknown (such as might occur in a schemaless document) is given the type annotationxs:untypedAtomic.
The value of an element is represented by the children of the element node, which may include text nodes and other element nodes. The type annotation of an element node indicates how the values in its child text nodes are to be interpreted. An element that has not been validated (such as might occur in a schemaless document) is annotated with the schema typexs:untyped. An element that has been validated and found to be partially valid is annotated with the schema type xs:anyType. If an element node is annotated as xs:untyped, all its descendant element nodes are also annotated as xs:untyped. However, if an element node is annotated as xs:anyType, some of its descendant element nodes may have a more specific type annotation.
This section explains some concepts that are important to the processing of XPath 4.0 expressions.
An ordering called document order is defined among all the nodes accessible during processing of a given expression, which may consist of one or more trees (documents or fragments).
Document order applies both to XNodes (typically corresponding to nodes in an XML document, and generally referred to simply as nodes), and also to JNodesDM, often corresponding to the contents of a JSON source text. These are known collectively as GNodesDM (for "generalized node").
An ordering called document order is defined among all the nodes accessible during processing of a given expression, which may consist of one or more trees (documents or fragments). Document order is defined in Section 6.36.2 Document OrderDM, and its definition is repeated here for convenience. Document order is a total ordering, although the relative order of some nodes is implementation-dependent. [Definition: Informally, document order is the order in which nodes appear in the XML serialization of a document.] [Definition: Document order is stable, which means that the relative order of two nodes will not change during the processing of a given expression, even if this order is implementation-dependent.] [Definition: The node ordering that is the reverse of document order is called reverse document order.]
Within an XTreeDMa tree, (that is, a tree consisting of XNodes), document order satisfies the following constraints:
The root node is the first nodeprecedes all other nodes.
A parent node precedes its children (and therefore its descendants).
Every node occurs before all of its children and descendants.
The children of a node N precede the following siblings of N.
Namespace nodes immediately follow the element node with which they are associated. The relative order of namespace nodes is stable but implementation-dependent.
Attribute nodes immediately follow the namespace nodes of the element node with which they are associated. The relative order of attribute nodes is stable but implementation-dependent.
The relative order of siblings is the order in which they occur in the children property of their parent node.
Children and descendants occur before following siblings.
Similarly, within a an JTreeDM, (that is, a tree consisting of JNodes), document order satisfies the following constraints:
The root JNode precedes all other JNodes.
A parent JNode precedes its children (and therefore its descendants).
The children of a JNode N precede the following siblings of N.
The children of a JNode that wraps an array follow the ordering of the members of the array.
The children of a JNode that wraps a map follow the ordering of the entries in the map.
The relative order of nodes in distinct trees is stable but implementation-dependent, subject to the following constraint: If any node in a given tree T1 is before any node in a different tree T2, then all nodes in tree T1 are before all nodes in tree T2.
Every node (that is, every XNodeDM) has a typed value and a string value, except for nodes whose value is absentDM. [Definition: The typed value of a node is a sequence of atomic items and can be extracted by applying the Section 2.1.4 fn:dataFO function to the node.] [Definition: The string value of a node is a string and can be extracted by applying the Section 2.1.3 fn:stringFO function to the node.]
An implementation may store both the typed value and the string value of a node, or it may store only one of these and derive the other as needed. The string value of a node must be a valid lexical representation of the typed value of the node, but the node is not required to preserve the string representation from the original source document. For example, if the typed value of a node is the xs:integer value 30, its string value might be "30" or "0030".
The typed value, string value, and type annotation of a node are closely related. If the node was created by mapping from an Infoset or PSVI, the relationships among these properties are defined by rules in Section 4.1 Schema InformationDM.
The relationship between typed value and string value for various kinds of nodes is summarized and illustrated by examples below.
For text and document nodes, the typed value of the node is the same as its string value, as an instance of the type xs:untypedAtomic. The string value of a document node is formed by concatenating the string values of all its descendant text nodes, in document order.
The typed value of a comment, namespace, or processing instruction node is the same as its string value. It is an instance of the type xs:string.
The typed value of an attribute node with the type annotationxs:anySimpleType or xs:untypedAtomic is the same as its string value, as an instance of xs:untypedAtomic. The typed value of an attribute node with any other type annotation is derived from its string value and type annotation using the lexical-to-value-space mapping defined in [XML Schema 1.0] or [XML Schema 1.1] Part 2 for the relevant type.
Example: A1 is an attribute having string value "3.14E-2" and type annotation xs:double. The typed value of A1 is the xs:double value whose lexical representation is 3.14E-2.
Example: A2 is an attribute with type annotation xs:IDREFS, which is a list datatype whose item type is the atomic datatype xs:IDREF. Its string value is "bar baz faz". The typed value of A2 is a sequence of three atomic items ("bar", "baz"", "faz""), each of type xs:IDREF. The typed value of a node is never treated as an instance of a named list type. Instead, if the type annotation of a node is a list type (such as xs:IDREFS), its typed value is treated as a sequence of the generalized atomic type from which it is derived (such as xs:IDREF).
For an element node, the relationship between typed value and string value depends on the node’s type annotation, as follows:
If the type annotation is xs:untyped or xs:anySimpleType or denotes a complex type with mixed content (including xs:anyType), then the typed value of the node is equal to its string value, as an instance of xs:untypedAtomic. However, if the nilled property of the node is true, then its typed value is the empty sequence.
Example: E1 is an element node having type annotation xs:untyped and string value "1999-05-31". The typed value of E1 is "1999-05-31", as an instance of xs:untypedAtomic.
Example: E2 is an element node with the type annotation formula, which is a complex type with mixed content. The content of E2 consists of the character H, a child element named subscript with string value "2", and the character O. The typed value of E2 is "H2O" as an instance of xs:untypedAtomic.
If the type annotation denotes a simple type or a complex type with simple content, then the typed value of the node is derived from its string value and its type annotation in a way that is consistent with schema validation. However, if the nilled property of the node is true, then its typed value is the empty sequence.
Example: E3 is an element node with the type annotation cost, which is a complex type that has several attributes and a simple content type of xs:decimal. The string value of E3 is "74.95". The typed value of E3 is 74.95, as an instance of xs:decimal.
Example: E4 is an element node with the type annotation hatsizelist, which is a simple type derived from the atomic typehatsize, which in turn is derived from xs:integer. The string value of E4 is "7 8 9". The typed value of E4 is a sequence of three values (7, 8, 9), each of type hatsize.
Example: E5 is an element node with the type annotation my:integer-or-string which is a union type with member types xs:integer and xs:string. The string value of E5 is "47". The typed value of E5 is 47 as a xs:integer, since xs:integer is the member type that validated the content of E5. In general, when the type annotation of a node is a union type, the typed value of the node will be an instance of one of the member types of the union.
Note:
If an implementation stores only the string value of a node, and the type annotation of the node is a union type, the implementation must be able to deliver the typed value of the node as an instance of the appropriate member type.
If the type annotation denotes a complex type with empty content, then the typed value of the node is the empty sequence and its string value is the zero-length string.
If the type annotation denotes a complex type with element-only content, then the typed value of the node is absentDM. The fn:data function raises a type error [err:FOTY0012]FO40 when applied to such a node. The string value of such a node is equal to the concatenated string values of all its text node descendants, in document order.
Example: E6 is an element node with the type annotation weather, which is a complex type whose content type specifies element-only. E6 has two child elements named temperature and precipitation. The typed value of E6 is absentDM, and the fn:data function applied to E6 raises an error.
The semantics of some XPath 4.0 operators depend on a process called atomization. Atomization is applied to a value when the value is used in a context in which a sequence of atomic items is required. The result of atomization is either a sequence of atomic items or a type error [err:FOTY0012]FO40. [Definition: Atomization of a sequence is defined as the result of invoking the fn:data function, as defined in Section 2.1.4 fn:dataFO.]
The semantics of fn:data are repeated here for convenience. The result of fn:data is the sequence of atomic items produced by applying the following rules to each item in the input sequence:
If the item is an atomic item, it is returned.
If the item is a node (specifically, an XNode), its typed value is returned (a type error [err:FOTY0012]FO40 is raised if the node has no typed value.)
If the item is a JNodeDM, its ¶value property is atomized and the result is returned.
If the item is a function item (other than an array) or map a type error [err:FOTY0013]FO40 is raised.
If the item is an array $a, atomization is defined as $a?* ! fn:data(.), which is equivalent to atomizing the members of the array.
Note:
This definition recursively atomizes members that are arrays. Hence, the result of atomizing the array [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] is the sequence (1, 2, 3, 4, 5, 6).
Atomization is used in processing the following types of expressionsmany expressions that are designed to operate on atomic items, including:
Arithmetic expressions
Comparison expressions
Function calls and returns
Cast expressions
Atomization plays an important role in the coercion rules used when converting a supplied argument in a function call to the type declared in the function signature.
Under certain circumstances (some of which are listed below), it is necessary to find the effective boolean value of a value. [Definition: The effective boolean value of a value is defined as the result of applying the fn:boolean function to the value, as defined in Section 8.3.1 fn:booleanFO.]
The dynamic semantics of fn:boolean are repeated here for convenience:
If its operand is an empty sequence, fn:boolean returns false.
If its operand is a sequence whose first item is a GNodenodeDM, fn:boolean returns true.
If its operand is a singleton value of type xs:boolean or derived from xs:boolean, fn:boolean returns the value of its operand unchanged.
If its operand is a singleton value of type xs:string, xs:anyURI, xs:untypedAtomic, or a type derived from one of these, fn:boolean returns false if the operand value has zero length; otherwise it returns true.
If its operand is a singleton value of any numeric type or derived from a numeric type, fn:boolean returns false if the operand value is NaN or is numerically equal to zero; otherwise it returns true.
In all other cases, fn:boolean raises a type error [err:FORG0006]FO40.
Note:
For instance, fn:boolean raises a type error if the operand is a function, a map, or an array.
The effective boolean value of a sequence is computed implicitly during processing of the following types of expressions:
Logical expressions (and, or)
The fn:not function
Certain types of predicates, such as a[b]
Conditional expressions (if)
Quantified expressions (some, every)
General comparisons, in XPath 1.0 compatibility mode.
Note:
The definition of effective boolean value is not used when casting a value to the type xs:boolean, for example in a cast expression or. It also plays no role in the coercion rules used when passing a value to a function whose expected parameter issignature declares a parameter of type xs:boolean.
As noted in 2.1.2 Values, every value in XPath 4.0 is regarded as a sequence of zero, one, or more items. The type system of XPath 4.0, described in this section, classifies the kinds of value that the language can handle, and the operations permitted on different kinds of value.
The type system of XPath 4.0 is related to the type system of [XML Schema 1.0] or [XML Schema 1.1] in two ways:
atomic items in XPath 4.0 (which are one kind of item) have atomic types such as xs:string, xs:boolean, and xs:integer. These types are taken directly from their definitions in [XML Schema 1.0] or [XML Schema 1.1].
NodesXNodes (which are another kind of item) have a property called a type annotation which determines the type of their content. The type annotation is a schema type. The type annotation of a node must not be confused with the item type of the node. For example, an element <age>23</age> might have been validated against a schema that defines this element as having xs:integer content. If this is the case, the type annotation of the node will be xs:integer, and in the XPath 4.0 type system, the node will match the item typeelement(age, xs:integer).
This chapter of the specification starts by defining sequence types and item types, which describe the range of values that can be bound to variables, used in expressions, or passed to functions. It then describes how these relate to schema types, that is, the simple and complex types defined in an XSD schema.
Note:
In many situations the terms item type and sequence type are used interchangeably to refer either to the type itself, or to the syntactic construct that designates the type: so in the expression $x instance of xs:string*, the construct xs:string* uses the SequenceType syntax to designate a sequence type whose instances are sequences of strings. When more precision is required, the specification is careful to use the terms item type and sequence type to refer to the actual types, while using the production names ItemType and SequenceType to refer to the syntactic designators of these types.
[Definition: A sequence type is a type that can be expressed using the SequenceType syntax. Sequence types are used whenever it is necessary to refer to a type in an XPath 4.0 expression. Since all values are sequences, every value matches one or more sequence types.]
Whenever it is necessary to refer to a sequence type in an XPath 4.0 expression, the SequenceType syntax is used.
SequenceType | ::= | ("empty-sequence" "(" ")") |
ItemType | ::= | AnyItemTest | TypeName | KindTest | GNodeType | JNodeType | FunctionType | MapType | ArrayType | RecordType | EnumerationType | ChoiceItemType |
OccurrenceIndicator | ::= | "?" | "*" | "+" |
| /* xgc: occurrence-indicators */ |
[Definition: A sequence type designator is a syntactic construct conforming to the grammar rule SequenceType. A sequence type designator is said to designate a sequence type.]
With the exception of the special type empty-sequence(), a sequence type consists of an item type that constrains the type of each item in the sequence, and a cardinality that constrains the number of items in the sequence. Apart from the item type item(), which permits any kind of item, item types divide into node types (such as element()), generalized atomic types (such as xs:integer) and function types (such as function() as item()*).
The cardinality of a sequence type is represented in the sequence type designator syntax by an . The occurrence indicators +, *, and ? bind to the last ItemType in the SequenceType, as described in the occurrence-indicators constraint.
[Definition: An item type is a type that can be expressed using the ItemType syntax, which forms part of the SequenceType syntax. Item types match individual items.]
Note:
While this definition is adequate for the purpose of defining the syntax of XPath 4.0, it ignores the fact that there are also item types that cannot be expressed using XPath 4.0 syntax: specifically, item types that reference an anonymous simple type or complex type defined in a schema. Such types can appear as type annotations on nodes following schema validation.
In most cases, the set of items matched by an item type consists either exclusively of atomic items, exclusively of nodes, or exclusively of function itemsDM. Exceptions include the generic types item(), which matches all items, xs:error, which matches no items, and choice item types, which can match any combination of types.
[Definition: An item type designator is a syntactic construct conforming to the grammar rule ItemType. An item type designator is said to designate an item type.]
Note:
Two item type designators may designate the same item type. For example, element() and element(*) are equivalent, as are attribute(A) and attribute(A, xs:anySimpleType).
Lexical QNames appearing in an item type designator are expanded using the default type namespace rule. Equality of QNames is defined by the eq operator.
This section defines the syntax and semantics of different ItemTypes in terms of the values that they match.
Note:
For an explanation of the EBNF grammar notation (and in particular, the operators ++ and **), see A.1 EBNF.
An item type designator written simply as an EQName (that is, a TypeName) is interpreted as follows:
If the name is written as a lexical QName, then it is expanded using the default type namespace rule.
If the expanded name matches a named item type in the static context, then it is taken as a reference to the corresponding item type. The rules that apply are the rules for the expanded item type definition.
Otherwise, it must match the name of a type in the in-scope schema types in the static context: specifically, an atomic type or a pure union type. See 3.5 Schema Types for details.
Note:
A name in the xs namespace will always fall into this category, since the namespace is reserved. See 2.1.3 Namespaces and QNames.
If the name cannot be resolved to a type, a static error is raised [err:XPST0051].
The following sections describe the syntax for item types for functions, including arrays and maps.
The subtype relation among these types is described in the various subsections of 3.3.2 Subtypes of Item Types.
The keyword fn is allowed as a synonym for function in function types, to align with changes to inline function declarations. [Issue 1192 PR 1197 21 May 2024]
The terms FunctionType, ArrayType, MapType, and RecordType replace FunctionTest, ArrayTest, MapTest, and RecordTest, with no change in meaning.
Parameter names may be included in a function signature; they are purely documentary. [Issue 1136 PR 1696 12 January 2025]
A FunctionType matches selected function items, potentially checking their signatureDM (which includes the types of the arguments and results).
FunctionType | ::= | AnyFunctionType |
AnyFunctionType | ::= | ("function" | "fn") "(" "*" ")" |
TypedFunctionType | ::= | ("function" | "fn") "(" (TypedFunctionParam ** ",") ")" "as" SequenceType |
TypedFunctionParam | ::= | ("$" EQName "as")? SequenceType |
EQName | ::= | QName | URIQualifiedName |
SequenceType | ::= | ("empty-sequence" "(" ")") |
The keywords function and fn are synonyms.
An AnyFunctionType matches any function item, including a map or an array. For example, the following expressions all return true:
fn:name#1 instance of function(*)
fn { @id } instance of function(*)
fn:random-number-generator() instance of function(*)
[ 1, 2, 3 ] instance of fn(*)
{} instance of fn(*)
A TypedFunctionType matches a function item if the function’s type signature (as defined in Section 7.18.1 Function ItemsDM) is a subtype of the TypedFunctionType.
Note:
The keywords function and fn are synonymous.
If parameter names are included in a TypedFunctionType, they are purely documentary and have no semantic effect. In particular, they play no part in deciding whether a particular function item matches the function type, and they never appear as keywords in function calls. For example the construct function($x as node()) as xs:string designates exactly the same type as function(node()) as xs:string.
Any parameter names that are supplied must be distinct [err:XQST0039].
A TypedFunctionType may also match certain maps and arrays, as described in 3.2.8.2 Map Types and 3.2.8.4 Array Types
Here are some examples of expressions that use a TypedFunctionType:
fn:count#1 instance of function(item()*) as xs:integer returns true, because the signature of the function item fn:count#1 is function(item()*) as xs:integer.
fn:count#1 instance of function(xs:string*) as item() returns true, because the signature of the function item fn:count#1 is a subtype of function(xs:string*) as item().
Note:
The same type might also be written fn($x as xs:int, $y as xs:int) as xs:int.
function(xs:anyAtomicType) as item()* matches any map, or any other function item with the required signature.
function(xs:integer) as item()* matches any array, or any other function item with the required signature.
A MapType designates an item type that either matches any map, or that matches maps whose keys and values are constrained to specific types.
MapType | ::= | AnyMapType | TypedMapType |
AnyMapType | ::= | "map" "(" "*" ")" |
TypedMapType | ::= | "map" "(" ItemType "," SequenceType ")" |
ItemType | ::= | AnyItemTest | TypeName | KindTest | GNodeType | JNodeType | FunctionType | MapType | ArrayType | RecordType | EnumerationType | ChoiceItemType |
SequenceType | ::= | ("empty-sequence" "(" ")") |
The MapTypemap(K, V) matches any map where every key is an instance of K and every value is an instance of V.
The entry-orderDM of a map has no effect on whether the map matches a particular map type.
Although the grammar for TypedMapType allows the key to be described using the full ItemType syntax, the item type used must be a generalized atomic type [err:XPST0152].
For example, given a map $M whose keys are integers and whose results are strings, such as { 0: "no", 1: "yes" }, the following following expressions deliver the result shown:
$M instance of map(*) returns true()
$M instance of map(xs:integer, xs:string) returns true()
$M instance of map(xs:decimal, xs:anyAtomicType) returns true()
$M instance of map(xs:int, xs:string) returns false()
$M instance of map(xs:integer, xs:token)) returns false()
A map is also a function item, and therefore matches certain function types. Specifically, a map that matches map(K, V) also matches a function type of the form function(xs:anyAtomicType) as R provided that both the following conditions are satisfied:
Note:
To understand this rule, consider the use of a map $M in a function call $M($K), which is equivalent to the function call map:get($M, $K). This function accepts any atomic item for the argument $K, and hence matches a function type that requires an argument type of xs:anyAtomicType. If the key $K is present in the map, the result of the function will be a value of type V; if not, it will be an empty sequence. The map is therefore substitutable for the function type provided that the function type allows both a value of type V and the empty sequence as possible results.
The key type K does not enter into this rule. That is because in the function call $M($K), the sought key $K does not have to be of the same type as the keys actually present in the map.
The transitivity rules for item type matching mean that if an item M matches a type T, and T is a subtype of U, then M also matches type U. So the fact that a map from integers to strings (map(xs:integer, xs:string)) matches function(xs:anyAtomicType) as xs:string? means that it will also match other function types such as function(xs:integer) as xs:string? and function(xs:decimal) as xs:anyAtomicType?
Furthermore, the rules for function coercion mean that any map can be supplied as a value in a context where it does not actually match the required function type, but can be coerced to a function that does. For example a map of type map(xs:integer, xs:string) can be coerced to a function of type function(xs:integer) as xs:string; in this situation a type error will occur only if a call on the function actually returns an empty sequence.
Examples:
$M instance of fn(*) returns true()
$M instance of fn(xs:anyAtomicType) as item()* returns true()
$M instance of fn(xs:integer) as item()* returns true()
$M instance of fn(xs:int) as item()* returns true()
$M instance of fn(xs:string) as item()* returns true()
not($M instance of fn(xs:integer) as xs:string) returns true()
Note:
The last case might seem surprising; however, function coercion ensures that $M can be used successfully anywhere that the required type is fn(xs:integer) as xs:string.
Rules defining whether one map type is a subtype of another are given in 3.3.2.6 Subtyping Maps.
A GNodeType matches a generalized node (GNode): that is, it matches any XNode or JNodeDM.
GNodeType | ::= | "GNode" "(" ")" |
A JNodeType matches a JNodeDM.
JNodeType | ::= | "JNode" "(" SequenceType? ")" |
SequenceType | ::= | ("empty-sequence" "(" ")") |
The form JNode() matches any JNode. The form JNode(T) matches a JNode whose ¶value is an instance of the sequence type T.
xs:errorThe item type xs:error has an empty value space; it never appears as a dynamic type or as the content type of a dynamic element or attribute type. It was defined in XML Schema in the interests of making the type system complete and closed, and it is also available in XPath 4.0 for similar reasons.
Note:
Even though it cannot occur in an instance, xs:error is a valid type name in a sequence type. The practical uses of xs:error as a sequence type are limited, but they do exist. For instance, an error-handling function that always raises a dynamic error never returns a value, so xs:error is a good choice for the return type of the function.
The semantics of xs:error are well defined as a consequence of the fact that xs:error is defined as a union type with no member types. For example:
$x instance of xs:error always returns false, regardless of the value of $x.
$x cast as xs:error fails dynamically with error [err:FORG0001]FO40, regardless of the value of $x.
$x cast as xs:error? raises a dynamic error [err:FORG0001]FO40 if exists($x) returns true, and evaluates to the empty sequence if empty($x) returns true.
xs:error($x) has the same semantics as $x cast as xs:error? (see the previous bullet point)
$x castable as xs:error evaluates to false, regardless of the value of $x.
$x treat as xs:error raises a dynamic error [err:XPDY0050] if evaluated, regardless of the value of $x. It never fails statically.
All of the above examples assume that $x is actually evaluated. The rules specified in 2.4.4 Errors and Optimization permit an implementation to avoid evaluating $x if the result of the query does not depend upon the value of $x and thus to avoid raising an error.
[Definition: Given two sequence types or item types, the rules in this section determine if one is a subtype of the other. If a type A is a subtype of type B, it follows that every value matched by A is also matched by B.]
Note:
The relationship subtype(A, A) is always true: every type is a subtype of itself.
Note:
The converse is not necessarily true: we cannot infer that if every value matched by A is also matched by B, then A is a subtype of type B. For example, A might be defined as the set of strings matching the regular expression [A-Z]*, while B is the set of strings matching the regular expression [A-Za-z]*; no subtype relationship holds between these types.
The rules for deciding whether one sequence type is a subtype of another are given in 3.3.1 Subtypes of Sequence Types. The rules for deciding whether one item type is a subtype of another are given in 3.3.2 Subtypes of Item Types.
Note:
The subtype relationship is not acyclic. There are cases where subtype(A, B) and subtype(B, A) are both true. This implies that A and B have the same value space, but they can still be different types. For example this applies when A is a union type with member types xs:string and xs:integer, while B is a union type with member types xs:integer and xs:string. These are different types ("23" cast as A produces a string, while "23" cast as B produces an integer, because casting is attempted to each member type in order) but both types have the same value space.
We use the notation A ⊆ B, or itemtype-subtype(A, B) to indicate that an item typeA is a subtype of an item type B. This section defines the rules for deciding whether any two item types have this relationship.
The rules in this section apply to item types, not to item type designators. For example, if the name STR has been defined in the static context as a named item type referring to the type xs:string, then anything said here about the type xs:string applies equally whether it is designated as xs:string or as STR, or indeed as the parenthesized forms (xs:string) or (STR).
References to named item types are handled as described in 3.3.2.9 Subtyping of Named Item Types.
The relationship A ⊆ B is true if and only if at least one of the conditions listed in the following subsections applies:
The following subsections describe the subtype relationships among node types.
Given item types A and B, A ⊆ B is true if any of the following apply:
The term "function conversion rules" used in 3.1 has been replaced by the term "coercion rules". [ PR 254 29 November 2022]
The coercion rules allow “relabeling” of a supplied atomic item where the required type is a derived atomic type: for example, it is now permitted to supply the value 3 when calling a function that expects an instance of xs:positiveInteger. [Issue 117 PR 254 29 November 2022]
The coercion rules now allow any numeric type to be implicitly converted to any other, for example an xs:double is accepted where the required type is xs:decimal. [Issue 980 PR 911 30 January 2024]
The coercion rules now allow conversion in either direction between xs:hexBinary and xs:base64Binary. [Issues 130 480 PR 815 7 November 2023]
The coercion rules now apply recursively to the members of an array and the entries in a map. [Issue 1318 PR 1501 29 October 2024]
The coercion rules now reorder the entries in a map when the required type is a record type. [Issue 1862 PR 1874 25 March 2025]
[Definition: The coercion rules are rules used to convert a supplied value to a required type, for example when converting an argument of a function call to the declared type of the function parameter. ] The required type is expressed as a sequence type. The effect of the coercion rules may be to accept the value as supplied, to convert it to a value that matches the required type, or to reject it with a type error.
This section defines how the coercion rules operate; the situations in which the rules apply are defined elsewhere, by reference to this section.
Note:
In previous versions of this specification, the coercion rules were referred to as the function conversion rules. The terminology has changed because the rules are not exclusively associated with functions or function calling.
If the required type is empty-sequence(), no coercion takes place (the supplied value must be an empty sequence, or a type error occurs).
In all other cases, the required sequence typeT comprises a required item typeR and an optional occurrence indicator. The coercion rules are then applied to a supplied value V and the required type T as follows:
If XPath 1.0 compatibility mode is true and V is not an instance of the required type T, then the conversions defined in 3.4.1 XPath 1.0 Compatibility Rules are applied to V. Then:
Each item in V is processed against the required item type R using the item coercion rules defined in 3.4.2 Item Coercion Rules, and the results are sequence-concatenated into a single sequence V′.
A type error is raised if the cardinality of V′ does not match the required cardinality of T [err:XPTY0004].
The rules in this section are used to process each item J in a supplied sequence, given a required item typeR.
If R is a generalized atomic type (for example, if it is an atomic type, a pure union type, or an enumeration type), and J is not an atomic item, then:
J is atomized to produce a sequence of atomic items JJ.
Each atomic item in JJ is coerced to the required type R by recursive application of the item coercion rules (the rules in this section) to produce a value V.
The result is the sequence-concatenation of the V values.
Note:
For example, if J is an element with type annotation xs:integer, and R is the union type xs:numeric, then the effect is to atomize the element to an xs:integer, and then to coerce the resulting xs:integer to xs:numeric (which leaves the integer unchanged). This is not the same as attempting to coerce the element to each of the alternatives of the union type in turn, which would deliver an instance of xs:double.
Otherwise, if R is a choice item type or a pure union type (which includes the case where it is an enumeration type), then:
If J matches (is an instance of) one of the alternatives in R, then J is coerced to the first alternative in R that J matches.
Note:
There are two situations where coercing an item to a type that it already matches does not simply return the item unchanged:
When the required type is a typed function type (see 3.2.8.1 Function Types), then function coercion is applied to coerce J to that function type, as described in 3.4.4 Function Coercion.
When the required type is a record type and the supplied value is a map, then coercion may change the entry orderDM of the entries in the map.
Otherwise, the item coercion rules (the rules in this section) are applied to J recursively with R set to each of the alternatives in the choice or union item type, in order, until an alternative is found that does not result in a type error; a type error is raised only if all alternatives fail.
The error code used in the event of failure should be the error code arising from the first unsuccessful matching attempt. (The diagnostic information associated with the error may also describe how further attempts failed.)
Note:
Suppose the required type is (xs:integer | element(e))* and the supplied value is the sequence (<e>22</e>, 23, <f>24</f>). Item coercion is applied independently to each of the three items in this sequence. The first item matches one of the alternatives, namely element(e), so it is returned unchanged as an element node. The second item (the integer 23) also matches one of the alternatives, and is returned unchanged as an integer. The third item does not match any of the alternatives, so coercion is attempted to each one in turn. Coercion to type xs:integer succeeds (by virtue of atomization and untyped atomic conversion), so the final result is the sequence (<e>22</e>, 23, 24)
Note:
Suppose the required type is enum("red", "green", "blue") and the supplied value is "green". The enumeration type is defined as a choice item type whose alternatives are singleton enumerations, so the rules are applied first to the type enum("red") (which fails), and then to the type enum("green") (which succeeds). The strings in an enumeration type are required to be distinct so the order of checking is in this case immaterial. The supplied value will be accepted, and will be relabeled as an instance of enum("green"), which is treated as a schema type equivalent to a type derived from xs:string by restriction.
Note:
Schema-defined union types behave in exactly the same way as choice item types.
If R is an atomic type and J is an atomic item, then:
If J is an instance of R then it is used unchanged.
If J is an instance of type xs:untypedAtomic then:
If R is namespace-sensitive then a type error [err:XPTY0117] is raised.
Otherwise, J is cast to type R.
If there is an entry (from, to) in the following table such that J is an instance of from, and to is R, then J is cast to type R.
| from | to |
|---|---|
xs:decimal | xs:double |
xs:double | xs:decimal |
xs:decimal | xs:float |
xs:float | xs:decimal |
xs:float | xs:double |
xs:double | xs:float |
xs:string | xs:anyURI |
xs:anyURI | xs:string |
xs:hexBinary | xs:base64Binary |
xs:base64Binary | xs:hexBinary |
Note:
The item type in the to column must match R exactly; however, J may belong to a subtype of the type in the from column.
For example, an xs:NCName will be cast to type xs:anyURI, but an xs:anyURI will not be cast to type xs:NCName.
Similarly, an xs:integer will be cast to type xs:double, but an xs:double will not be cast to type xs:integer.
If R is derived from some primitive atomic type P, then J is relabeled as an instance of R if it satisfies all the following conditions:
J is an instance of P.
J is not an instance of R.
The datumDM of J is within the value space of R.
Relabeling an atomic item changes the type annotation but not the datumDM. For example, the xs:integer value 3 can be relabeled as an instance of xs:unsignedByte, because the datum is within the value space of xs:unsignedByte.
Note:
Relabeling is not the same as casting. For example, the xs:decimal value 10.1 can be cast to xs:integer, but it cannot be relabeled as xs:integer, because its datum not within the value space of xs:integer.
Note:
The effect of this rule is that if, for example, a function parameter is declared with an expected type of xs:positiveInteger, then a call that supplies the literal value 3 will succeed, whereas a call that supplies -3 will fail.
This differs from previous versions of this specification, where both these calls would fail.
This change allows the arguments of existing functions to be defined with a more precise type. For example, the $position argument of array:get could be defined as xs:positiveInteger rather than xs:integer.
Note:
If T is a union type with members xs:negativeInteger and xs:positiveInteger and the supplied value is the sequence (20, -20), then the effect of these rules is that the first item 20 is relabeled as type xs:positiveInteger and the second item -20is relabeled as type xs:negativeInteger.
Note:
Promotion (for example of xs:float to xs:double) occurs only when T is a primitive type. Relabeling occurs only when T is a derived type. Promotion and relabeling are therefore never combined.
Note:
A singleton enumeration type such as enum("green") is treated as an atomic type derived by restriction from xs:string; so if the xs:string value "green" is supplied in a context where the required type is enum("red", "green", "blue"), the value will be accepted and will be relabeled as an instance of enum("green").
If J is a JNodeDM and does not match R, then each item in the ¶value of J is coerced to type R by applying the coercion rules recursively.
Note:
For example, if $A is an array and the members of the array are maps, then $A?child::* returns a sequence of JNodes that encapsulate maps, and the average size of these maps can be obtained using the expression avg($A?child::* ! map:size(.)). The first argument of map:size does not accept a JNode directly, but it does (in effect) accept a JNode that encapsulates a map.
If R is an ArrayType other than array(*) and J is an array, then J is converted to a new array by converting each member to the required member type by applying the coercion rules recursively.
Note:
For example, if the required type is array(xs:double) and the supplied value is [ 1, 2 ], the array is converted to [ 1e0, 2e0 ].
If R is a MapType other than map(*) and J is a map, then J is converted to a new map as follows:
Each key in the supplied map is converted to the required map key type by applying the coercion rules. If the resulting map would contain duplicate keys, a type error is raised [err:XPTY0004].
The corresponding value is converted to the required map value type by applying the coercion rules recursively.
The order of entries in the map remains unchanged.
Note:
For example, if the required type is map(xs:string, xs:double) and the supplied value is { "x": 1, "y": 2 }, the map is converted to { "x": 1e0, "y": 2e0 }.
Note:
Duplicate keys can occur if the value space of the target type is more restrictive than the original type. For example, an error is raised if the map { 1.2: 0, 1.2000001: 0 }, which contains two keys of type xs:decimal, is coerced to the type map(xs:float, xs:integer).
If R is a RecordType and J is a map, then J is converted to a new map as follows:
The keys in the supplied map are unchanged.
In any map entry whose key is equal to the name of one of the field declarations in R (under the rules of the atomic-equal function), the corresponding value is converted to the required type defined by that field declaration, by applying the coercion rules recursively (but with XPath 1.0 compatibility mode treated as false).
The order of entries in the map is changed: entries whose keys correspond to the names of field declarations in R appear first, in the order of the corresponding field declarations, and (if the record type is extensible) other entries then follow retaining their relative order in J.
Note:
For example, if the required type is record(longitude as xs:double, latitude as xs:double) and the supplied value is { "latitude": 53.2, "longitude": 0 }, then the map is converted to { "longitude": 0.0e0, "latitude": 53.2e0 }.
If R is a TypedFunctionType and J is a function item, then function coercion is applied to J.
Note:
Function coercion applies even if J is already an instance of R.
Maps and arrays are functions, so function coercion applies to them as well.
If, after the above conversions, the resulting item does not match the expected item type R according to the rules for SequenceType Matching, a type error is raised [err:XPTY0004].
Note:
Under the general rules for type errors (see 2.4.1 Kinds of Errors), a processor may report a type error during static analysis if it will necessarily occur when the expression is evaluated. For example, the function call fn:abs("beer") will necessarily fail when evaluated, because the function requires a numeric value as its argument; this may be detected and reported as a static error.
Function coercion now allows a function with arity N to be supplied where a function of arity greater than N is expected. For example this allows the function true#0 to be supplied where a predicate function is required.
It has been clarified that function coercion applies even when the supplied function item matches the required function type. This is to ensure that arguments supplied when calling the function are checked against the signature of the required function type, which might be stricter than the signature of the supplied function item. [Issue 1020 PRs 1023 1128 9 April 2024]
Function coercion is a transformation applied to function items during application of the coercion rules. [Definition: Function coercion wraps a function item in a new function whose signature is the same as the expected type. This effectively delays the checking of the argument and return types until the function is called.]
Given a function F, and an expected function type T, function coercion proceeds as follows:
If F has higher arity than T, a type error is raised [err:XPTY0004]
If F has lower arity than T, then F is wrapped in a new function that declares and ignores the additional argument; the following steps are then applied to this new function.
For example, if T is function(node(), xs:boolean) as xs:string, and the supplied function is fn:name#1, then the supplied function is effectively replaced by function($n as node(), $b as xs:boolean) as xs:string { fn:name($n) }
Note:
This mechanism makes it easier to design versatile and extensible higher-order functions. For example, in previous versions of this specification, the second argument of the fn:filter function expected an argument of type function(item()) as xs:boolean. This has now been extended to function(item(), xs:integer) as xs:boolean, but existing code continues to work, because callback functions that are not interested in the value of the second argument simply ignore it.
A type error is raised [err:XPTY0004] if, for any parameter type, or for the result type, the relevant type in the signature of the supplied function and the relevant type in the expected function type are substantively disjoint.
For example, the types xs:integer and xs:string are substantively disjoint, so a function with signature function(xs:integer) as xs:boolean cannot be supplied where the expected type is function(xs:string) as xs:boolean.
Function coercion then returns a new function item with the following properties (as defined in Section 7.18.1 Function ItemsDM):
name: The name of F(if not absent).
identity: A new function identity distinct from the identity of any other function item.
Note:
See also 4.5.7 Function Identity.
signature: Annotations is set to the annotations of F. TypedFunctionType is set to the expected type.
implementation: In effect, a FunctionBody that calls F, passing it the parameters of this new function, in order.
nonlocal variable bindings: An empty mapping.
These rules have the following consequences:
SequenceType matching of the function’s arguments and result are delayed until that function is called.
When the coerced function is called, the supplied arguments must match the parameter typed defined in T; it is not sufficient to match the parameter types defined in F.
The coercion rules rules applied to the function’s arguments and result are defined by the SequenceType it has most recently been coerced to. Additional coercion rules could apply when the wrapped function is called.
If an implementation has static type information about a function, that can be used to type check the function’s argument and return types during static analysis.
When function coercion is applied to a map or an array, the resulting function is not a map or array, and cannot be used as such. For example, the expression
let $f as function(xs:integer) as xs:boolean := { 0: false(), 1: true() }
return $f?0raises a type error, because a lookup expression requires the left hand operand to be a map or array, and $f is neither.
When function types are used as alternatives in a choice item type, the supplied function is coerced to the first alternative for which coercion does not raise a type error. In this situation it is important to write the alternatives in order, with the most specific first.
Note:
The semantics of function coercion are specified in terms of wrapping the functions. Static typing may be able to reduce the number of places where this is actually necessary. However, it cannot be assumed that because a supplied function is an instance of the required function type, no function coercion is necessary: the supplied function might not perform all required checks on the types of its arguments.
Since maps and arrays are also functions in XPath 4.0, function coercion applies to them as well. For instance, consider the following expression:
let $m := {
"Monday" : true(),
"Wednesday" : false(),
"Friday" : true()
}
let $days := ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
return filter($days, $m) The map $m is an instance of function(xs:anyAtomicType?) as item()*. When the fn:filter() function is called, the following occurs to the map:
The map $m is treated as a function equivalent to map:get($m, ?).
The coercion rules result in applying function coercion to this function, wrapping it in a new function (M′) with the signature function(item(), xs:integer) as xs:boolean.
When M′ is called by fn:filter(), coercion and SequenceType matching rules are applied to the argument, resulting in an item() value ($a) or a type error.
The function map:get($m, ?) is called with $a as the argument; this returns either an xs:boolean or the empty sequence (call the result R).
The wrapper function $p applies the coercion rules to R. If R is an xs:boolean the matching succeeds. When it is an empty sequence (in particular, $m does not contain a key for "Tuesday"), a type error is raised [err:XPTY0004], since the expected type is xs:boolean and the actual type is an empty sequence.
Consider the following expression:
let $m := {
"Monday" : true(),
"Wednesday" : false(),
"Friday" : true(),
}
let $days := ("Monday", "Wednesday", "Friday")
return filter($days, $m)In this case the result of the expression is the sequence ("Monday", "Friday"). But if the input sequence included the string "Tuesday", the filter operation would fail with a type error.
Note:
Function coercion applies even if the supplied function matches the required type.
For example, consider this case:
declare function local:filter(
$s as item()*,
$p as function(xs:string) as xs:boolean
) as item()* {
$s[$p(.)]
};
let $f := function($a) { $a mod 2 = 0 }
return local:filter(1 to 10, $f)Here the supplied function $f is an instance of the required type, because its signature defaults the argument type to item()*, which is a supertype of xs:string. The expression $s[$p(.)] could in principle succeed. However, function coercion ensures that the supplied function is wrapped in a function that requires the argument to be of type xs:string, so the call fails with a type error when the wrapping function is invoked supplying an xs:integer as the argument.
This section discusses each of the basic kinds of expression. Each kind of expression has a name such as PathExpr, which is introduced on the left side of the grammar production that defines the expression. Since XPath 4.0 is a composable language, each kind of expression is defined in terms of other expressions whose operators have a higher precedence. In this way, the precedence of operators is represented explicitly in the grammar.
The order in which expressions are discussed in this document does not reflect the order of operator precedence. In general, this document introduces the simplest kinds of expressions first, followed by more complex expressions. For the complete grammar, see Appendix [A XPath 4.0 Grammar].
The highest-level symbol in the XPath grammar is XPath.
XPath | ::= | Expr |
Expr | ::= | (ExprSingle ++ ",") |
ExprSingle | ::= | ForExpr |
ExprSingle | ::= | ForExpr |
ForExpr | ::= | ForClauseForLetReturn |
LetExpr | ::= | LetClauseForLetReturn |
QuantifiedExpr | ::= | ("some" | "every") (QuantifierBinding ++ ",") "satisfies" ExprSingle |
IfExpr | ::= | "if" "(" Expr ")" (UnbracedActions | BracedAction) |
OrExpr | ::= | AndExpr ("or" AndExpr)* |
The XPath 4.0 operator that has lowest precedence is the comma operator, which is used to combine two operands to form a sequence. As shown in the grammar, a general expression (Expr) can consist of multiple ExprSingle operands, separated by commas.
The name ExprSingle denotes an expression that does not contain a top-level comma operator (despite its name, an ExprSingle may evaluate to a sequence containing more than one item.)
The symbol ExprSingle is used in various places in the grammar where an expression is not allowed to contain a top-level comma. For example, each of the arguments of a function call must be a ExprSingle, because commas are used to separate the arguments of a function call.
After the comma, the expressions that have next lowest precedence are ForExpr, LetExpr, QuantifiedExpr, IfExpr, and OrExpr. Each of these expressions is described in a separate section of this document.
[Definition: Primary expressions are the basic primitives of the language. They include literals, variable references, context value references, and function calls. A primary expression may also be created by enclosing any expression in parentheses, which is sometimes helpful in controlling the precedence of operators.] Map and Array Constructors are described in 4.13.1 Maps and 4.13.2 Arrays.
PrimaryExpr | ::= | Literal |
Literal | ::= | NumericLiteral | StringLiteral | QNameLiteral |
VarRef | ::= | "$" EQName |
ParenthesizedExpr | ::= | "(" Expr? ")" |
ContextValueRef | ::= | "." |
FunctionCall | ::= | EQNameArgumentList |
| /* xgc: reserved-function-names */ | ||
| /* gn: parens */ | ||
FunctionItemExpr | ::= | NamedFunctionRef | InlineFunctionExpr |
NamedFunctionRef | ::= | EQName "#" IntegerLiteral |
| /* xgc: reserved-function-names */ | ||
InlineFunctionExpr | ::= | MethodAnnotation* ("function" | "fn") FunctionSignature? FunctionBody |
MapConstructor | ::= | "map"? "{" (MapConstructorEntry ** ",") "}" |
ArrayConstructor | ::= | SquareArrayConstructor | CurlyArrayConstructor |
StringTemplate | ::= | "`" (StringTemplateFixedPart | StringTemplateVariablePart)* "`" |
| /* ws: explicit */ | ||
UnaryLookup | ::= | Lookup |
[Definition: A literal is a direct syntactic representation of an atomic item.] XPath 4.0 supports three kinds of literals: numeric literals, string literals, and QName literals.
NumericLiteral | ::= | IntegerLiteral | HexIntegerLiteral | BinaryIntegerLiteral | DecimalLiteral | DoubleLiteral |
IntegerLiteral | ::= | Digits |
| /* ws: explicit */ | ||
Digits | ::= | DecDigit ((DecDigit | "_")* DecDigit)? |
| /* ws: explicit */ | ||
DecDigit | ::= | [0-9] |
| /* ws: explicit */ | ||
HexIntegerLiteral | ::= | "0x" HexDigits |
| /* ws: explicit */ | ||
HexDigits | ::= | HexDigit ((HexDigit | "_")* HexDigit)? |
| /* ws: explicit */ | ||
HexDigit | ::= | [0-9a-fA-F] |
| /* ws: explicit */ | ||
BinaryIntegerLiteral | ::= | "0b" BinaryDigits |
| /* ws: explicit */ | ||
BinaryDigits | ::= | BinaryDigit ((BinaryDigit | "_")* BinaryDigit)? |
| /* ws: explicit */ | ||
BinaryDigit | ::= | [01] |
| /* ws: explicit */ | ||
DecimalLiteral | ::= | ("." Digits) | (Digits "." Digits?) |
| /* ws: explicit */ | ||
DoubleLiteral | ::= | (("." Digits) | (Digits ("." Digits?)?)) [eE] [+-]? Digits |
| /* ws: explicit */ |
The value of a numeric literal is determined as follows (taking the rules in order):
Underscore characters are stripped out. Underscores may be included in a numeric literal to aid readability, but have no effect on the value. For example, 1_000_000 is equivalent to 1000000.
Note:
Underscores must not appear at the beginning or end of a sequence of digits, only in intermediate positions. Multiple adjacent underscores are allowed.
A HexIntegerLiteral represents a non-negative integer expressed in hexadecimal: for example 0xffff represents the integer 65535, and 0xFFFF_FFFF represents the integer 4294967295.
A BinaryIntegerLiteral represents a non-negative integer expressed in binary: for example 0b101 represents the integer 5, and 0b1111_1111 represents the integer 255.
The value of a numeric literal containing no . and no e or E character is an atomic item of type xs:integer; the value is obtained by casting from xs:string to xs:integer as specified in Section 22.223.2 Casting from xs:string and xs:untypedAtomicFO.
The value of a numeric literal containing . but no e or E character is an atomic item of type xs:decimal; the value is obtained by casting from xs:string to xs:decimal as specified in Section 22.223.2 Casting from xs:string and xs:untypedAtomicFO.
The value of a numeric literal containing an e or E character is an atomic item of type xs:double; the value is obtained by casting from xs:string to xs:double as specified in Section 22.223.2 Casting from xs:string and xs:untypedAtomicFO.
Note:
The value of a numeric literal is always non-negative. An expression may appear to include a negative number such as -1, but this is technically an arithmetic expression comprising a unary minus operator followed by a numeric literal.
Note:
The effect of the above rules is that in the case of an integer or decimal literal, a dynamic error [err:FOAR0002]FO40 will generally be raised if the literal is outside the range of values supported by the implementation (other options are available: see Section 4.2 Arithmetic operators on numeric valuesFO for details.)
The XML Schema specification allows implementations to impose a limit (which must not be less than 18 digits) on the size of integer and decimal values. The full range of values of built-in subtypes of xs:integer, such as xs:long and xs:unsignedLong, can be supported only if the limit is 20 digits or higher. Negative numbers such as the minimum value of xs:long (-9223372036854775808) are technically unary expressions rather than literals, but implementations may prefer to ensure that they are expressible.
Here are some examples of numeric literals:
12 denotes the xs:integer value twelve.
1_000_000 denotes the xs:integer value one million.
12.5 denotes the xs:decimal value twelve and one half.
3.14159_26535_89793e0 is an xs:double value representing the mathematical constant π to 15 decimal places.
125E2 denotes the xs:double value twelve thousand, five hundred.
0xffff denotes the xs:integer value 65535.
0b1000_0001 denotes the xs:integer value 129.
The xs:boolean values true and false can be constructed by calls to the system functionsfn:true() and fn:false(), respectively.
Values of other simple types can be constructed by calling the constructor function for the given type. The constructor functions for XML Schema built-in types are defined in Section 21.122.1 Constructor functions for XML Schema built-in atomic typesFO. In general, the name of a constructor function for a given type is the same as the name of the type (including its namespace). For example:
xs:integer("12") returns the integer value twelve.
xs:date("2001-08-25") returns an item whose type is xs:date and whose value represents the date 25th August 2001.
xs:dayTimeDuration("PT5H") returns an item whose type is xs:dayTimeDuration and whose value represents a duration of five hours.
Constructor functions can also be used to create special values that have no literal representation, as in the following examples:
xs:float("NaN") returns the special floating-point value, "Not a Number."
xs:double("INF") returns the special double-precision value, "positive infinity."
Constructor functions are available for all simple types, including union types. For example, if my:dt is a user-defined union type whose member types are xs:date, xs:time, and xs:dateTime, then the expression my:dt("2011-01-10") creates an atomic item of type xs:date. The rules follow XML Schema validation rules for union types: the effect is to choose the first member type that accepts the given string in its lexical space.
It is also possible to construct values of various types by using a cast expression. For example:
9 cast as hatsize returns the atomic item 9 whose type is hatsize.
Functions in XPath 4.0 arise in two ways:
A function definition contains information about a family of functions with the same name and a defined arity range. These functions are in most cases known statically (they appear in the statically known function definitions), but there may be further function definitions that are known only dynamically (appearing in the dynamically known function definitions).
Function items are XDM items that can be called using a dynamic function call. They are values that can be bound to variables, passed as arguments, returned as function results, and generally manipulated in the same way as other XDM values.
The functions defined by a statically known function definition can be invoked using a static function call. Function items corresponding to these definitions can also be obtained, as dynamic values, by evaluating a named function reference. Function items can also be obtained using the fn:function-lookup function: in this case the function name and arity do not need to be known statically, and the function definition need not be present in the static context, so long as it is in the dynamic context.
Static and dynamic function calls are described in the following sections.
[Definition: A dynamic function call consists of a base expression that returns the function and a parenthesized list of zero or more arguments (argument expressions or ArgumentPlaceholders).]
A dynamic function call is evaluated as described in 4.5.3.1 Evaluating Dynamic Function Calls.
The following are examples of some dynamic function calls:
This example calls the function contained in $f, passing the arguments 2 and 3:
$f(2, 3)
This example fetches the second item from sequence $f, treats it as a function and calls it, passing an xs:string argument:
$f[2]("Hi there")This example calls the function $f passing no arguments, and filters the result with a positional predicate:
$f()[2]
Note:
Arguments in a dynamic function call are always supplied positionally.
A dynamic function call can now be applied to a sequence of functions, and in particular to an empty sequence. This makes it easier to chain a sequence of calls. [Issue 1240 ]
This section applies to dynamic function calls whose arguments do not include an ArgumentPlaceholder. For function calls that include a placeholder, see 4.5.4 Partial Function Application.
[Definition: A dynamic function call is an expression that is evaluated by calling a function item, which is typically obtained dynamically.]
When a dynamic function call FC is evaluated, the result is obtained as follows:
The base expression of the function call is evaluated. If this is not of type function(*)* (a sequence of zero or more function items) then a type error is raised [err:XPTY0004].
The result of the dynamic function call is the sequence concatenation of the results of applying each function item individually, retaining order. That is, the result of F(X, Y, ...) is for $FI in F return $FI(X, Y, ...). The result of a dynamic function call applied to a single function item FI is defined by the rules that follow.
[err:XPTY0004]. If the arity of FI does not match the number of arguments in the ArgumentList, a type error is raised [err:XPTY0004].
Argument expressions are evaluated, producing argument values. The order of argument evaluation is implementation-dependent and an argument need not be evaluated if the function body can be evaluated without evaluating that argument.
Each argument value is converted to the corresponding parameter type in FI’s signature by applying the coercion rules, resulting in a converted argument value
If FI is a map, it is evaluated as described in 4.13.1.2 Maps as Functions.
If FI is an array, it is evaluated as described in 4.13.2.2 Arrays as Functions.
If FI’s body is an XPath 4.0 expression (for example, if FI is an anonymous function, or a partial application of such a function):
FI’s body is evaluated. The static context for this evaluation is the static context of the XPath 4.0 expression. The dynamic context for this evaluation is obtained by taking the dynamic context of the InlineFunctionExpr that contains the FunctionBody, and making the following changes:
The focus (context value, context position, and context size) is absentDM.
In the variable values component of the dynamic context, each converted argument value is bound to the corresponding parameter name.
When this is done, the converted argument values retain their dynamic types, even where these are subtypes of the declared parameter types. For example, a function with a parameter $p of type xs:decimal can be called with an argument of type xs:integer, which is derived from xs:decimal. During the processing of this function call, the value of $p inside the body of the function retains its dynamic type of xs:integer.
FI’s nonlocal variable bindings are also added to the variable values. (Note that the names of the nonlocal variables are by definition disjoint from the parameter names, so there can be no conflict.)
The value returned by evaluating the function body is then converted to the declared return type of FI by applying the coercion rules. The result is then the result of evaluating FC.
As with argument values, the value returned by a function retains its dynamic type, which may be a subtype of the declared return type of FI. For example, a function that has a declared return type of xs:decimal may in fact return a value of dynamic type xs:integer.
If the implementation of FI is not an XPath 4.0 expression (for example, if FI is a system function ), the body of the function is evaluated, and the result is converted to the declared return type, in the same way as for a static function call (see 4.5.1.1 Static Function Call Syntax).
Errors may be raised in the same way.
$incr is a nonlocal variable that is available within the function because its variable binding has been added to the variable values of the function.. Even though the parameter and return type of this function are both xs:decimal, the more specific type xs:integer is preserved in both cases.
let $incr := 1
let $f := function($i as xs:decimal) as xs:decimal { $i + $incr }
return $f(5)
The following example will raise a type error [err:XPDY0002]:
let $vat := function() { @vat + @price }
return doc('wares.xml')/shop/article/$vat()Instead, the context value can be used as an argument to the anonymous function:
let $vat := function($art) { $art/@vat + $art/@price }
return doc('wares.xml')/shop/article/$vat(.)Alternatively, the value can be referenced as a nonlocal variable binding:
let $ctx := doc('wares.xml')/shop/article
let $vat := function() { for $a in $ctx return $a/@vat + $a/@price }
return $vat()Finally, a focus function can be used. This binds the value of the argument to the context value within the function body:
let $vat := function { @vat + @price }
return $vat(doc('wares.xml')/shop/article)
Methods are described in 4.5.6.1 Methods, and are commonly used in dynamic function calls such as $rectangle?area(). In this example $rectangle is typically a map, and area is the key of one of the entries in the map, the value of the entry being a method. The lookup expression $rectangle?area returns a function item whose captured context includes the containing map, and the dynamic function call then evaluates the body of this method, which is able to access the containing map as the context item.
Such calls can be chained. For example if $rectangle?resize(2) returns a rectangle that is twice the size of the original, then $rectangle?resize(2)?area() returns the area of the enlarged rectangle.
This kind of chaining extends to the case where a method returns zero or more maps. For example, suppose that rectangles are nested, and that $rectangle?contents() delivers a sequence of zero or more rectangles. Then the expression $rectangle?area() - sum($rectangle?contents()?area()) returns the difference between the area of the containing rectangle and the total area of the contained rectangles. This works because the dynamic function call $rectangle?contents()?area() applies the function area to each of the function items in the sequence returned by the expression $rectangle?contents().
Note:
Keyword arguments are not allowed in a dynamic function call.
[Definition: A static or dynamic function call is a partial function application if one or more arguments is an ArgumentPlaceholder.]
The rules for partial function application in static function calls and dynamic function calls have a great deal in common, but they are stated separately below for clarity.
Partial function application delivers function items, whose arity is equal to the number of placeholders in the call.
A static partial function application always delivers one function item. A dynamic partial function application delivers one function item for each function item in the input.
More specifically, each function item in the result of the partial function application is a partially applied function. [Definition: A partially applied function is a function created by partial function application.]
For static function calls, the result is obtained as follows:
The function definitionFD to be partially applied is determined in the same way as for a static function call without placeholders, as described in 4.5.1.1 Static Function Call Syntax. For this purpose an ArgumentPlaceholder contributes to the count of arguments.
The parameters of FD are classified into three categories:
Parameters that map to a placeholder, referred to as placeholder parameters.
Parameters for which an explicit value is given in the function call (either positionally or by keyword), referred to as explicitly supplied parameters.
Parameters (which are necessarily optional parameters) for which no corresponding argument is supplied, either as a placeholder or with an explicit value. These are referred to as defaulted parameters.
Note:
A partial function application need not have any explicitly supplied parameters. For example, the partial function application fn:string(?) is allowed; it has exactly the same effect as the named function reference fn:string#1.
Explicitly supplied parameters and defaulted parameters are evaluated and converted to the required type using the rules for a static function call. This may result in an error being raised.
A type error is raised if any of the explicitly supplied or defaulted parameters, after applying the coercion rules, does not match the required type of the corresponding parameter.
In addition, a dynamic error may be raised if any of the explicitly supplied or defaulted parameters does not match other constraints on the value of that parameter (for example, if the value supplied for a parameter expecting a regular expression is not a valid regular expression); or if the processor is able to establish that evaluation of the resulting function will fail for any other reason (for example, if an error is raised while evaluating a subexpression in the function body that depends only on explicitly supplied and defaulted parameters).
In all cases the error code is the same as for a static function call supplying the same invalid value(s).
In the particular case where all the supplied arguments are placeholders, the error behavior should be the same as for an equivalent named function reference: for example, fn:id#1 fails if there is no context node, and fn:id(?)should fail likewise.
The result is a partially applied function having the following properties (which are defined in Section 7.18.1 Function ItemsDM):
name: The name of FD if all parameters map to placeholders, that is, if the partial function application is equivalent to the corresponding named function reference. Otherwise, the name is absent.
identity: A new function identity distinct from the identity of any other function item.
Note:
See also 4.5.7 Function Identity.
arity: The number of placeholders in the function call.
signature: The parameters in the returned function are the parameters of FD that have been identified as placeholder parameters, retaining the order in which the placeholders appear in the function call. The result type of the returned function is the same as the result type of FD.
An implementation which can determine a more specific signature (for example, through use of type analysis) is permitted to do so.
annotations: The annotations of FD.
body: The body of FD.
captured context: The static and dynamic context of the function call, augmented, for each explicitly supplied parameter and each defaulted parameter, with a binding of the converted argument value to the corresponding parameter name.
Note:
A partial function application can be used to change the order of parameters, for example fn:contains(substring := ?, value := ?) returns a function item that is equivalent to fn:contains#2, but with the order of arguments reversed.
The following partial function application creates a function item that computes the sum of squares of a sequence.
let $sum-of-squares := fold-right(?, 0, function($a, $b) { $a*$a + $b })
return $sum-of-squares(1 to 3)$sum-of-squares is an anonymous function. It has one parameter, named $seq, which is taken from the corresponding parameter in fn:fold-right (the other two parameters are fixed). The implementation is the implementation of fn:fold-right, which is a context-independent system function. The nonlocal bindings contain the fixed bindings for the second and third parameters of fn:fold-right.
For dynamic function calls, the result is obtained as follows:
The base expression of the function call is evaluated. If this is not of type function(*)* (a sequence of zero or more function items) then a type error is raised.
The result of the dynamic function call is the sequence concatenation of the results of partial applying each function item, retaining order. That is, the result of F(X, Y, ...) is for $FI in F return $FI(X, Y, ...). The result of a dynamic function call applied to a single function item FI is defined by the rules that follow.
An ArgumentPlaceholder contributes to the count of arguments.
The parameters of FI are classified into two categories:
Parameters that map to a placeholder, referred to as placeholder parameters.
Parameters for which an explicit value is given in the function call, referred to as supplied parameters.
Note:
A partial function application need not have any explicitly supplied parameters. For example, if $f is a function with arity 2, then the partial function application $f(?, ?) returns a function that has exactly the same effect as $f.
Arguments corresponding to supplied parameters are evaluated and converted to the required type of the parameter, using the rules for dynamic function calls.
A type error is raised if any of the supplied parameters, after applying the coercion rules, does not match the required type.
In addition, a dynamic error may be raised if any of the supplied parameters does not match other constraints on the value of that parameter (for example, if the value supplied for a parameter expecting a regular expression is not a valid regular expression); or if the processor is able to establish that evaluation of the resulting function will fail for any other reason (for example, if an error is raised while evaluating a subexpression in the function body that depends only on explicitly supplied parameters).
In both cases the error code is the same as for a dynamic function call supplying the same invalid value.
The result of the partial function application is a partially applied function with the following properties (which are defined in Section 7.18.1 Function ItemsDM):
name: Absent.
arity: The number of placeholders in the function call.
signature: The signature of FI, removing the types of supplied parameters. An implementation which can determine a more specific signature (for example, through use of type analysis) is permitted to do so.
annotations: The annotations of FI.
body: The body of FI.
captured context: the captured context of FI, augmented, for each supplied parameter, with a binding of the converted argument value to the corresponding parameter name.
Note:
In a dynamic partial function application, argument keywords are not available, so it is not possible to change the order of parameters.
In the following example, $f is an anonymous function, and $paf is a partially applied function created from $f.
let $f := function($seq, $delim) { fold-left($seq, "", concat(?, $delim, ?)) }
let $paf := $f(?, ".")
return $paf(1 to 5)$paf is also an anonymous function. It has one parameter, named $delim, which is taken from the corresponding parameter in $f (the other parameter is fixed). The implementation of $paf is the implementation of $f, which is fn:fold-left($seq, "", fn:concat(?, $delim, ?)). This implementation is associated with the SC and DC of the original expression in $f. The nonlocal bindings associate the value "." with the parameter $delim.
Partial function application never returns a map or an array. If $f is a map or an array, then $f(?) is a partial function application that returns a function, but the function it returns is neither a map nor an array.
NamedFunctionRef | ::= | EQName "#" IntegerLiteral |
| /* xgc: reserved-function-names */ | ||
EQName | ::= | QName | URIQualifiedName |
IntegerLiteral | ::= | Digits |
| /* ws: explicit */ | ||
Digits | ::= | DecDigit ((DecDigit | "_")* DecDigit)? |
| /* ws: explicit */ | ||
DecDigit | ::= | [0-9] |
| /* ws: explicit */ |
[Definition: A named function reference is an expression (written name#arity) which evaluates to a function item, the details of the function item being based on the properties of a function definition in the static context.]
The name and arity of the required function are known statically.
The EQName is expanded using the default function namespace rule.
The expanded QName and arity must correspond to a function definition present in the static context. More specifically, for a named function reference F#N, there must be a function definition in the statically known function definitions whose name matches F, and whose arity range includes N. Call this function definitionFD.
If FD is context dependent for the given arity, then the returned function item has a captured context comprising the static and dynamic context of the named function reference.
Note:
In practice, it is necessary to retain only those parts of the static and dynamic context that can affect the outcome. These means it is unnecessary to retain parts of the context that no system function depends on (for example, local variables), or parts that are invariant within an execution scope (for example, the implicit timezone).
Consider:
let $f := <foo/>/fn:name#0 return <bar/>/$f()
The function fn:name(), with no arguments, returns the name of the context node. The function item delivered by evaluating the expression fn:name#0 returns the name of the element that was the context node at the point where the function reference was evaluated (that is, the <foo> element). This expression therefore returns "foo", not "bar".
An error is raised if the identified function depends on components of the static or dynamic context that are not present, or that have unsuitable values. For example [err:XPDY0002] is raised for the expression fn:name#0 if the context item is absent, and [err:FODC0001]FO is raised for the call fn:id#1 if the context item is not a node in a tree that is rooted at a document node. The error that is raised is the same as the error that would be raised by the corresponding function if called with the same static and dynamic context.
If the expanded QName and arity in a named function reference do not match the name and arity range of a function definition in the static context, a static error is raised [err:XPST0017].
The value of a NamedFunctionRef is a function itemFI obtained from FD as follows:
name: The name of FD.
identity:
If FD is context dependent for the given arity, then a new function identity distinct from the identity of any other function item.
Note:
In the general case, a function reference to a context-dependent function will produce different results every time it is evaluated, because the resulting function item has a captured context (see Section 7.18.1 Function ItemsDM) that includes the dynamic context of the particular evaluation. Optimizers, however, are allowed to detect cases where the captured context happens to be the same, or where any variations are immaterial, and where it is therefore safe to return the same function item each time. This might be the case, for example, where the only context dependency of a function is on the default collation, and the default collation for both evaluations is known to be the same.
Otherwise, a function identity that is the same as that produced by the evaluation of any other named function reference with the same function name and arity.
This rule applies even across different execution scopesFO: for example if a parameter to a call to fn:transform is set to the result of the expression fn:abs#1, then the function item passed as the parameter value will be identical to that obtained by evaluating the expression fn:abs#1 within the target XSLT stylesheet.
This rule also applies when the target function definition is nondeterministicFO. For example all evaluations of the named function reference map:keys#2 return identical function items, even though two evaluations of map:keys with the same arguments may produce different results.
Note:
See also 4.5.7 Function Identity.
arity: As specified in the named function reference.
signature: Formed from the required types of the first A parameters of FD, and the function result type of FD.
annotations: The annotations of FD.
body: The body of FD.
captured context: Comprises the static and dynamic context of the named function reference, augmented with bindings of the names of parameters of FD beyond the A’th parameter, to their respective default values.
Note:
In practice, it is necessary to retain only the parts of the context that the function actually depends on (if any).
Note:
Consider the system function fn:format-date, which has an arity range of 2 to 5. The named function reference fn:format-date#3 returns a function item whose three parameters correspond to the first three parameters of fn:format-date; the remaining two arguments will take their default values. To obtain an arity-3 function that binds to arguments 1, 2, and 5 of fn:format-date, use the partial function application format-date(?, ?, place := ?).
The following are examples of named function references:
fn:abs#1 references the fn:abs function which takes a single argument.
fn:concat#5 references the fn:concat function which takes 5 arguments.
local:myfunc#2 references a function named local:myfunc which takes 2 arguments.
Note:
Function items, as values in the data model, have a fixed arity, and a dynamic function call always supplies the arguments positionally. In effect, the result of evaluating my:func#3 is the same as the result of evaluating the inline function expression fn($x, $y, $z) { my:func($x, $y, $z) }, except that the returned function has a name (it retains the name my:func).
In inline function expressions, the keyword function may be abbreviated as fn. [Issue 1192 PR 1197 21 May 2024]
New abbreviated syntax is introduced (focus function) for simple inline functions taking a single argument. An example is fn { ../@code } [Issue 503 PR 521 30 May 2023]
An inline function may be annotated as a %method, giving it access to its containing map. [Issues 1800 1845 PRs 1817 1853 4 March 2025]
InlineFunctionExpr | ::= | MethodAnnotation* ("function" | "fn") FunctionSignature? FunctionBody |
MethodAnnotation | ::= | "%method" |
FunctionSignature | ::= | "(" ParamList ")" TypeDeclaration? |
ParamList | ::= | (VarNameAndType ** ",") |
VarNameAndType | ::= | "$" EQNameTypeDeclaration? |
EQName | ::= | QName | URIQualifiedName |
TypeDeclaration | ::= | "as" SequenceType |
SequenceType | ::= | ("empty-sequence" "(" ")") |
FunctionBody | ::= | EnclosedExpr |
EnclosedExpr | ::= | "{" Expr? "}" |
[Definition: An inline function expression, when evaluated, creates an anonymous function defined directly in the inline function expression.] An inline function expression specifies the names and SequenceTypes of the parameters to the function, the SequenceType of the result, and the body of the function.
An inline function expression whose FunctionSignature is omitted is known as a focus function. Focus functions are described in 4.5.6.2 Focus Functions.
[Definition: An anonymous function is a function item with no name. Anonymous functions may be created, for example, by evaluating an inline function expression or by partial function application.]
The keywords function and fn are synonymous.
The syntax allows the names and types of the function argument to be declared, along with the type of the result:
function($x as xs:integer, $y as xs:integer) as xs:integer { $x + $y }The types can be omitted, and the keyword can be abbreviated:
fn($x, $y) { $x + $y }A zero-arity function can be written as, for example, fn() { current-date() }.
If a function parameter is declared using a name but no type, its default type is item()*. If the result type is omitted, its default result type is item()*.
The parameters of an inline function expression are considered to be variables whose scope is the function body. It is a static error [err:XQST0039] for an inline function expression to have more than one parameter with the same name.
The annotation keyword %method is described in 4.5.6.1 Methods.
The static context for the function body is inherited from the location of the inline function expression.
The variables in scope for the function body include all variables representing the function parameters, as well as all variables that are in scope for the inline function expression.
Note:
Function parameter names can mask variables that would otherwise be in scope for the function body.
The result of an inline function expression is a single function item with the following properties (as defined in Section 7.18.1 Function ItemsDM):
name: Absent.
identity: A new function identity distinct from the identity of any other function item.
Note:
See also 4.5.7 Function Identity.
signature: A FunctionType constructed from the SequenceTypes in the InlineFunctionExpr. An implementation which can determine a more specific signature (for example, through use of type analysis of the function’s body) is permitted to do so.
annotations: If the keyword %method is present, then a list containing a single annotation whose name is a QName with local name "method" and namespace "http://www.w3.org/2012/xquery" and whose value is an empty sequence; otherwise an empty set..
body: The FunctionBody of the InlineFunctionExpr.
captured context: the static context is the static context of the inline function expression. The dynamic context has an absent focus, and a set of variable bindings comprising the variable values component of the dynamic context of the InlineFunctionExpr.
The following are examples of some inline function expressions:
This example creates a function that takes no arguments and returns a sequence of the first 6 primes:
function() as xs:integer+ { 2, 3, 5, 7, 11, 13 }This example creates a function that takes two xs:double arguments and returns their product:
fn($a as xs:double, $b as xs:double) as xs:double { $a * $b }This example creates and invokes a function that captures the value of a local variable in its scope:
let $incrementors := (
for $x in 1 to 10
return function($y) as xs:integer { $x + $y }
)
return $incrementors[2](4)The result of this expression is 6
PathExpr | ::= | ("/" RelativePathExpr?) |
| /* xgc: leading-lone-slash */ | ||
RelativePathExpr | ::= | StepExpr (("/" | "//") StepExpr)* |
[Definition: A path expression consists of a series of one or more steps, separated by / or //, and optionally beginning with / or //. A path expression is typically used to locate nodes within trees. ]
Absolute path expressions (those starting with an initial / or //), start their selection from the root node of a tree; relative path expressions (those without a leading / or //) start from the context value.
A path expression consisting of a single step is evaluated as described in 4.6.4 Steps.
StepExpr | ::= | PostfixExpr | AxisStep |
PostfixExpr | ::= | PrimaryExpr | FilterExpr | DynamicFunctionCall | LookupExpr | FilterExprAM |
AxisStep | ::= | (ReverseStep | ForwardStep) Predicate* |
[Definition: A step is a part of a path expression that generates a sequence of items and then filters the sequence by zero or more predicates. The value of the step consists of those items that satisfy the predicates, working from left to right. A step may be either an axis step or a postfix expression.] Postfix expressions are described in 4.3 Postfix Expressions.
[Definition: An axis step returns a sequence of nodes that are reachable from a starting node via a specified axis. Such a step has two parts: an axis, which defines the "direction of movement" for the step, and a node test, which selects nodes based on their kind, name, and/or type annotation .]
If the context value is a sequence of zero or more nodes, an axis step returns a sequence of zero or more nodes; otherwise, a type error is raised [err:XPTY0020].
The step expression S is equivalent to ./S. Thus, if the context value is a sequence containing multiple nodes, the semantics of a step expression are equivalent to a path expression in which the step is always applied to a single node. The following description therefore explains the semantics for the case where the context value is a single node, called the context node.
Note:
The equivalence of a step S to the path expression ./S means that the resulting node sequence is returned in document order.
An axis step may be either a forward step or a reverse step, followed by zero or more predicates.
In the abbreviated syntax for a step, the axis can be omitted and other shorthand notations can be used as described in 4.6.7 Abbreviated Syntax.
The unabbreviated syntax for an axis step consists of the axis name and node test separated by a double colon. The result of the step consists of the nodes reachable from the starting node via the specified axis that have the node kind, name, and/or type annotation specified by the node test. For example, the step child::para selects the para element children of the context node: child is the name of the axis, and para is the name of the element nodes to be selected on this axis. The available axes are described in 4.6.4.1 Axes. The available node tests are described in 4.6.4.2 Node Tests. Examples of steps are provided in 4.6.6 Unabbreviated Syntax and 4.6.7 Abbreviated Syntax.
ForwardAxis | ::= | ("attribute" |
ReverseAxis | ::= | ("ancestor" |
XPath defines a set of axes for traversing documents, but a host language may define a subset of these axes. The following axes are defined:
The child axis contains the children of the context node, which are the nodes returned by the Section 6.7.37.5.3 children AccessorDM.
Note:
Only document nodes and element nodes have children. If the context node is any other kind of node, or if the context node is an empty document or element node, then the child axis is an empty sequence. The children of a document node or element node may be element, processing instruction, comment, or text nodes. Attribute, namespace, and document nodes can never appear as children.
The descendant axis is defined as the transitive closure of the child axis; it contains the descendants of the context node (the children, the children of the children, and so on).
More formally, $node/descendant::node() delivers the result of fn:transitive-closure($node, fn { child::node() }).
The descendant-or-self axis contains the context node and the descendants of the context node.
More formally, $node/descendant-or-self::node() delivers the result of $node/(. | descendant::node()).
The parent axis contains the sequence returned by the Section 6.7.117.5.11 parent AccessorDM, which returns the parent of the context node, or an empty sequence if the context node has no parent.
Note:
An attribute node may have an element node as its parent, even though the attribute node is not a child of the element node.
The ancestor axis is defined as the transitive closure of the parent axis; it contains the ancestors of the context node (the parent, the parent of the parent, and so on).
More formally, $node/ancestor::node() delivers the result of fn:transitive-closure($node, fn { parent::node() }).
Note:
The ancestor axis includes the root node of the tree in which the context node is found, unless the context node is the root node.
The ancestor-or-self axis contains the context node and the ancestors of the context node; thus, the ancestor-or-self axis will always include the root node.
More formally, $node/ancestor-or-self::node() delivers the result of $node/(. | ancestor::node()).
The following-sibling axis contains the context node’s following siblings, that is, those children of the context node’s parent that occur after the context node in document order. If the context node is an attribute or namespace node, the following-sibling axis is empty.
More formally, $node/following-sibling::node() delivers the result of fn:siblings($node)[. >> $node]).
The following-sibling-or-self axis contains the context node, together with the contents of the following-sibling axis.
More formally, $node/following-sibling-or-self::node() delivers the result of fn:siblings($node)[not(. << $node)]
The preceding-sibling axis contains the context node’s preceding siblings, that is, those children of the context node’s parent that occur before the context node in document order. If the context node is an attribute or namespace node, the preceding-sibling axis is empty.
More formally, $node/preceding-sibling::node() delivers the result of fn:siblings($node)[. << $node].
The preceding-sibling-or-self axis contains the context node, together with the contents of the preceding-sibling axis.
More formally, $node/preceding-sibling-or-self::node() delivers the result of fn:siblings($node)[not(. >> $node).
The following axis contains all nodes that are descendants of the root of the tree in which the context node is found, are not descendants of the context node, and occur after the context node in document order.
More formally, $node/following::node() delivers the result of $node/ancestor-or-self::node()/following-sibling::node()/descendant-or-self::node()
The following-or-self axis contains the context node, together with the contents of the following axis.
More formally, $node/following-or-self::node() delivers the result of $node/(. | following::node()).
The preceding axis contains all nodes that are descendants of the root of the tree in which the context node is found, are not ancestors of the context node, and occur before the context node in document order.
More formally, $node/preceding::node() delivers the result of $node/ancestor-or-self::node()/preceding-sibling::node()/descendant-or-self::node().
The preceding-or-self axis contains the context node, together with the contents of the preceding axis.
More formally, $node/preceding-or-self::node() delivers the result of $node/(. | preceding::node()).
The attribute axis contains the attributes of the context node, which are the nodes returned by the Section 6.7.17.5.1 attributes AccessorDM; the axis will be empty unless the context node is an element.
The self axis contains just the context node itself.
The self axis is primarily useful when testing whether the context node satisfies particular conditions, for example if ($x[self::chapter]).
More formally, $node/self::node() delivers the result of $node.
The namespace axis contains the namespace nodes of the context node, which are the nodes returned by the Section 6.7.77.5.7 namespace-nodes AccessorDM; this axis is empty unless the context node is an element node. The namespace axis is deprecated as of XPath 2.0. If XPath 1.0 compatibility mode is true, the namespace axis must be supported. If XPath 1.0 compatibility mode is false, then support for the namespace axis is implementation-defined. An implementation that does not support the namespace axis when XPath 1.0 compatibility mode is false must raise a static error [err:XPST0010] if it is used. Applications needing information about the in-scope namespaces of an element should use the function fn:in-scope-namespaces.
Axes can be categorized as forward axes and reverse axes. An axis that only ever contains the context node or nodes that are after the context node in document order is a forward axis. An axis that only ever contains the context node or nodes that are before the context node in document order is a reverse axis.
The parent, ancestor, ancestor-or-self, preceding, preceding-or-self, preceding-sibling, and preceding-sibling-or-self axes are reverse axes; all other axes are forward axes.
The ancestor, descendant, following, preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document.
[Definition: Every axis has a principal node kind. If an axis can contain elements, then the principal node kind is element; otherwise, it is the kind of nodes that the axis can contain.] Thus:
For the attribute axis, the principal node kind is attribute.
For the namespace axis, the principal node kind is namespace.
For all other axes, the principal node kind is element.
XPath 4.0 supports operators to construct, filter, and combine sequences of items. Sequences are never nested—for example, combining the values 1, (2, 3), and ( ) into a single sequence results in the sequence (1, 2, 3).
UnionExpr | ::= | IntersectExceptExpr (("union" | "|") IntersectExceptExpr)* |
IntersectExceptExpr | ::= | InstanceofExpr (("intersect" | "except") InstanceofExpr)* |
InstanceofExpr | ::= | TreatExpr ("instance" "of" SequenceType)? |
XPath 4.0 provides the following operators for combining sequences of nodesGNodes:
The union and | operators are equivalent. They take two node sequences as operands and return a sequence containing all the nodesGNodes that occur in either of the operands.
The intersect operator takes two nodeGNodes sequences as operands and returns a sequence containing all the nodesGNodes that occur in both operands.
The except operator takes two node sequences as operands and returns a sequence containing all the nodesGNodes that occur in the first operand but not in the second operand.
All these operators eliminate duplicate nodesGNodes from their result sequences based on nodeGNodes identity. The resulting sequence is returned in document order.
If an operand of union, intersect, or except contains an item that is not a nodeGNode, a type error is raised [err:XPTY0004].
If an IntersectExceptExpr contains more than two InstanceofExprsInstanceofExprs, they are grouped from left to right. With a UnionExpr, it makes no difference how operands are grouped, the results are the same.
Here are some examples of expressions that combine sequences. Assume the existence of three element nodes that we will refer to by symbolic names A, B, and C. Assume that the variables $seq1, $seq2 and $seq3 are bound to the following sequences of these nodes:
$seq1 is bound to (A, B)
$seq2 is bound to (A, B)
$seq3 is bound to (B, C)
Then:
$seq1 union $seq2 evaluates to the sequence (A, B).
$seq2 union $seq3 evaluates to the sequence (A, B, C).
$seq1 intersect $seq2 evaluates to the sequence (A, B).
$seq2 intersect $seq3 evaluates to the sequence containing B only.
$seq1 except $seq2 evaluates to the empty sequence.
$seq2 except $seq3 evaluates to the sequence containing A only.
The following example demonstrates the use of the except operator with JNodes:
let $m := JNode($map)
for $e in $m?child::* except $m?child::xx
return ...In addition to the sequence operators described here, see Section 14 Processing sequencesFO for functions defined on sequences.
Comparison expressions allow two values to be compared. XPath 4.0 provides three kinds of comparison expressions, called value comparisons, general comparisons, and nodeGNode comparisons.
ComparisonExpr | ::= | OtherwiseExpr ((ValueComp | GeneralComp | NodeComp) OtherwiseExpr)? |
OtherwiseExpr | ::= | StringConcatExpr ("otherwise" StringConcatExpr)* |
ValueComp | ::= | "eq" | "ne" | "lt" | "le" | "gt" | "ge" |
GeneralComp | ::= | "=" | "!=" | "<" | "<=" | ">" | ">=" |
NodeComp | ::= | "is" | "<<" | ">>" |
Note:
When an XPath expression is written within an XML document, the XML escaping rules for special characters must be followed; thus < must be written as <.
For a summary of the differences between different ways of comparing atomic items in XPath 4.0, see G Atomic Comparisons: An Overview.
NodeGNode comparisons are used to compare two GNodesnodesDM (that is, XNodes or JNodesDM), by their identity or by their document order. The result of a nodeGNode comparison is defined by the following rules:
The operands of a nodeGNode comparison are evaluated in implementation-dependent order.
If either operand is an empty sequence, the result of the comparison is an empty sequence, and the implementation need not evaluate the other operand or apply the operator. However, an implementation may choose to evaluate the other operand in order to determine whether it raises an error.
Each operand must be either a single nodeGNode or an empty sequence; otherwise a type error is raised [err:XPTY0004].
A comparison with the is operator is true if the two operand nodesvalues of two operands are the same nodeGNode; otherwise it is false. See [XQuery and XPath Data Model (XDM) 4.0] for the definition of nodeGNode identity.
A comparison with the << operator returns true if the left operand nodeGNode precedes the right operand nodeGNode in document order; otherwise it returns false.
A comparison with the >> operator returns true if the left operand nodeGNode follows the right operand nodeGNode in document order; otherwise it returns false.
Here are some examples of nodeGNode comparisons:
The following comparison is true only if the left and right sides each evaluate to exactly the same single node:
/books/book[isbn = "1558604820"] is /books/book[call = "QA76.9 C3845"]
The following comparison is true only if the node identified by the left side occurs before the node identified by the right side in document order:
/transactions/purchase[parcel = "28-451"] << /transactions/sale[parcel = "33-870"]
The following comparison is true only if the first integer among the members of an array precedes the first string. This expression compares two JNodes:
let $A := ["Q", 3, "E", "R", "T", 5, "Y"]
return $A ? child::~[xs:integer][1] << $A ? child::~[xs:string][1]Most modern programming languages have support for collections of key/value pairs, which may be called maps, dictionaries, associative arrays, hash tables, keyed lists, or objects (these are not the same thing as objects in object-oriented systems). In XPath 4.0, we call these maps. Most modern programming languages also support ordered lists of values, which may be called arrays, vectors, or sequences. In XPath 4.0, we have both sequences and arrays. Unlike sequences, an array is an item, and can appear as an item in a sequence.
Note:
The XPath 4.0 specification focuses on syntax provided for maps and arrays, especially constructors and lookup.
Some of the functionality typically needed for maps and arrays is provided by functions defined in Section 18 Processing mapsFO and Section 19 Processing arraysFO, including functions used to read JSON to create maps and arrays, serialize maps and arrays to JSON, combine maps to create a new map, remove map entries to create a new map, iterate over the keys of a map, convert an array to create a sequence, combine arrays to form a new array, and iterate over arrays in various ways.
[Definition: A map is a function that associates a set of keys with values, resulting in a collection of key / value pairs.] [Definition: Each key / value pair in a map is called an entry.] [Definition: The value associated with a given key is called the associated value of the key.]
Maps and their properties are defined in the data model: see Section 7.28.2 Map ItemsDM. For an overview of the functions available for processing maps, see Section 18 Processing mapsFO.
Note:
Maps in XPath 4.0 are ordered. The effect of this property is explained in Section 7.28.2 Map ItemsDM. In an ordered map, the order of entries is predictable and depends on the order in which they were added to the map.
The lookup operator ? can now be followed by a string literal, for cases where map keys are strings other than NCNames. It can also be followed by a variable reference.
A deep lookup operator ?? is provided for searching trees of maps and arrays. [Issue 297 PR 837 23 November 2023]
Lookup expressions can now take a modifier (such as keys, values, or pairs) enabling them to return structured results rather than a flattened sequence. [Issues 960 1094 PR 1125 23 April 2024]
An inline function may be annotated as a %method, giving it access to its containing map. [Issues 1800 1845 PRs 1817 1853 4 March 2025]
The key specifier can reference an item type or sequence type, to select values of that type only. This is especially useful when processing trees of maps and arrays, as encountered when processing JSON input. [Issues 1456 1866 PRs 1864 1877]
The "?" operator can now be followed by an axis step, such as child::* or descendant::*. This returns a sequence of JNodes, which allow access to the key used for selection, and to parents and ancestors in the selection tree; it also avoids flattening the result sequence. [Issue 2025 PR 2031 13 June 2025]
XPath 4.0 provides two lookup operators ? and ?? for maps and arrays. These provide a terse syntax for accessing the entries in a map or the members of an array.
The operator "?", known as the shallow lookup operator, returns values found immediately in the operand map or array. The operator "??", known as the deep lookup operator, also searches nested maps and arrays. The effect of the deep lookup operator "??" is explained in 4.13.3.3 Deep Lookup.
LookupExpr | ::= | PostfixExprLookup |
PostfixExpr | ::= | PrimaryExpr | FilterExpr | DynamicFunctionCall | LookupExpr | FilterExprAM |
Lookup | ::= | ShallowLookup | DeepLookup |
LookupShallowLookupLookupShallowLookup | ::= | ("?" | "??") ((".." | "." | ((ModifierJAxis "::")? KeySpecifier)) |
ModifierJAxisModifierJAxis | ::= | "pairsancestor" | "keysancestor-or-self" | "valueschild" | "items"descendant" | "descendant-or-self" | "parent" | "self" |
KeySpecifier | ::= | NCName | IntegerLiteral | StringLiteral | VarRef | ParenthesizedExpr | LookupWildcard | TypeSpecifier |
IntegerLiteral | ::= | Digits |
| /* ws: explicit */ | ||
Digits | ::= | DecDigit ((DecDigit | "_")* DecDigit)? |
| /* ws: explicit */ | ||
DecDigit | ::= | [0-9] |
| /* ws: explicit */ | ||
StringLiteral | ::= | AposStringLiteral | QuotStringLiteral |
| /* ws: explicit */ | ||
VarRef | ::= | "$" EQName |
ParenthesizedExpr | ::= | "(" Expr? ")" |
LookupWildcard | ::= | "*" |
TypeSpecifier | ::= | "~[" SequenceType "]" |
SequenceType | ::= | ("empty-sequence" "(" ")") |
DeepLookup | ::= | "??" KeySpecifier |
A Lookup has two parts: the KeySpecifier determines which entries (in a map) or members (in an array) are selected, and the Modifier determines how they are delivered in the result. The default modifier is items, which delivers the result as a flattened sequence of items.
To take a simple example, given $A as an array [ ("a", "b"), ("c", "d"), ("e", "f"), 42 ], some example Lookup expressions are:
| Expression | Result |
|---|---|
$A?* (or $A?items::*) | ("a", "b", "c", "d", "e", "f", 42) |
$A?pairs::* | ({ "key": 1, "value": ("a", "b") },
{ "key": 2, "value": ("c", "d") },
{ "key": 3, "value": ("e", "f") },
{ "key": 4, "value": 42 }) |
$A?values::* | ([ "a", "b" ], [ "c", "d" ], [ "e", "f" ], [42]) |
$A?keys::* | (1, 2, 3, 4) |
$A?2 (or $A?items::2) | ("c", "d") |
$A?pairs::2 | ({ "key": 2, "value":("c", "d") }) |
$A?values::2 | ([ "c", "d" ]) |
$A?keys::2 | (2) |
$A?(3, 1) (or $A?items::(3, 1)) | ("e", "f", "a", "b") |
$A?pairs::(3, 1) | ({ "key": 3, "value": ("e", "f") },
{ "key": 1, "value": ("a", "b") }) |
$A?values::(3, 1) | ([ "e", "f" ][ "a", "b" ]) |
$A?keys::(3, 1) | (3, 1) |
$A?~[xs:integer] | 42 |
$A?keys::~[xs:integer] | 4 |
$A?keys::~[xs:string+] | (1, 2, 3) |
Similarly, given $M as a map { "X": ("a", "b"), "Y": ("c", "d"), "Z": ("e", "f"), "N": 42 }, some example lookup expressions are as follows.
| Expression | Result |
|---|---|
$M?* (or $M?items::*) | ("a", "b", "c", "d", "e", "f", 42) |
$M?pairs::* | ({ "key": "X", "value": ("a", "b") },
{ "key": "Y", "value": ("c", "d") },
{ "key": "Z", "value": ("e", "f") },
{ "key": "N", "value": 42 }) |
$M?values::* | ([ "a", "b" ], [ "c", "d" ], [ "e", "f" ], [42]) |
$M?keys::* | ("X", "Y", "Z", "N") |
$M?Y (or $M?items::Y) | ("c", "d") |
$M?pairs::Y | ({ "key": "Y", "value":("c", "d") }) |
$M?values::Y | ([ "c", "d" ]) |
$M?keys::Y | ("Y") |
$M?("Z", "X") (or $A?items::("Z", "X")) | ("e", "f", "a", "b") |
$M?pairs::("Z", "X") | ({ "key": "Z", "value": ("e", "f") },
{ "key": "X", "value": ("a", "b") }) |
$M?values::("Z", "X") | ([ "e", "f" ][ "a", "b" ]) |
$M?keys::("Z", "X") | ("Z", "X") |
$M?~[xs:integer] | 42 |
$M?keys::~[xs:integer] | "N" |
$M?keys::~[xs:string+] | ("X", "Y", "Z") |
The semantics of a postfix lookup expression E?pairs::KS are defined as follows. The results with other modifiers can be derived from this result, as explained below.
E is evaluated to produce a value $V.
If $V is not a singleton (that is if count($V) ne 1), then the result (by recursive application of these rules) is the value of for $v in $V return $v?pairs::KS.
If $V is a singleton array item (that is, if $V instance of array(*)) then:
If the KeySpecifierKS is a ParenthesizedExpr, then it is evaluated to produce a value $K and the result is:
data($K) ! { "key": ., "value": array:get($V, .) }Note:
The focus for evaluating the key specifier expression is the same as the focus for the Lookup expression itself.
If the KeySpecifierKS is a VarRef$X, the result is the same as $V?pairs::($X).
If the KeySpecifierKS is an IntegerLiteral with value $i, the result is the same as $V?pairs::($i).
If the KeySpecifierKS is an NCNameor StringLiteral, the expression raises a type error [err:XPTY0004].
If the KeySpecifierKS is a wildcard (*), the result is the same as $V?pairs::(1 to array:size($V)):
Note:
Note that array items are returned in order.
If the KeySpecifierKS is a TypeSpecifier~[T], the result is the same as $V?pairs::*[?value instance of T].
If $V is a singleton map item (that is, if $V instance of map(*)) then:
If the KeySpecifierKS is a ParenthesizedExpr, then it is evaluated to produce a value $K and the result is:
data($K) ! { "key": ., "value": map:get($V, .) }Note:
The focus for evaluating the key specifier expression is the same as the focus for the Lookup expression itself.
If the KeySpecifierKS is a VarRef$X, the result is the same as $V?pairs::($X).
If the KeySpecifierKS is an NCName or a StringLiteral, with value $S, the result is the same as $V?pairs::($S)
If the KeySpecifierKS is an IntegerLiteral with value $N, the result is the same as $V?pairs::($N).
If the KeySpecifierKS is a wildcard (*), the result is the same as $V?pairs::(map:keys($V)).
Note:
The order of entries in the result sequence reflects the entry orderDM of the map.
If the KeySpecifierKS is a TypeSpecifier~T, the result is the same as $V?pairs::*[?value instance of T]. Note that T is in general a sequence type: if there is an occurrence indicator, then it must be written within parentheses, but if it is a plain item type with no occurrence indicator, then the parentheses may be omitted.
Otherwise (that is, if $V is neither a map nor an array) a type error is raised [err:XPTY0004].
For modifiers other than pairs, the resulting key-value pair is post-processed as follows:
A postfix Lookup has three parts: the left hand operand selects maps or arrays to be searched. The JAxis defines the scope of a search for the required data, and the KeySelector defines the search criteria.
First a very simple example: given an array $A of maps:
[ { "John": 3, "Jill": 5}, {"Peter": 8, "Mary": 6} ]$A ? 1 ? John returns 3
$A ? 2 ? Mary returns 6
$A ? * ? * returns (3, 5, 8, 6)
$A ? * ? Peter returns 8
$A ? 2 ? * returns (8, 6)
The above examples do not include a JAxis. For purposes of exposition, however, we will consider cases in the following order:
If the modifier is items (explicitly or by default), and the key specifier is an NCName or StringLiteral, then the result of $V?items::KS is the result of the expression:
for $KVP in $V?pairs::KS let $value := map:get($KVP, 'value') return if ($value instance of %method function(*)) then bind-focus($value, $V) else $value
where bind-focus($F, $V) is a function that takes a function item $F and returns a modified function item whose captured context has the focus set to $V: for more detail see 4.5.6.1 Methods.
Note:
The effect of this is that if any of the selected values is a singletonmethod, the selected function item is modified by binding the context value to the containing map $V. In other cases the result is the sequence concatenation of the value parts.
If the modifier is items (explicitly or by default), and the key specifier is not an NCName nor a StringLiteral, then the result of $V?items::KS is the result of the expression $V?pairs::KS ! map:get(., "value"). This returns the sequence concatenation of the selected values.
If the modifier is values, the result of $V?values::KS is the same as the result of $V?pairs::KS ! array { map:get(., "value") }. This returns each value as an array.
If the modifier is keys, the result of $V?keys::KS is the same as the result of $V?pairs::KS ! map:get(., "key"). This returns the keys (integer indexes in the case of an array) without the values.
Examples:
{ "first" : "Jenna", "last" : "Scott" }?first evaluates to "Jenna"
{ "first name" : "Jenna", "last name" : "Scott" }?"first name" evaluates to "Jenna"
[ 4, 5, 6 ]?2 evaluates to 5.
({ "first": "Tom" }, { "first": "Dick" }, { "first": "Harry" })?first evaluates to the sequence ("Tom", "Dick", "Harry").
([ 1, 2, 3 ], [ 4, 5, 6 ])?2 evaluates to the sequence (2, 5).
([ 1, [ "a", "b" ], [ 4, 5, [ "c", "d"] ])?value::*[. instance of array(xs:string)] evaluates to the sequence ([ "a", "b" ], [ "c", "d" ]).
[ "a", "b" ]?3 raises a dynamic error [err:FOAY0001]FO40
We will start by giving the rules for the case where a JAxis is included.
An expression such as $A ? child::KS (where child might be replaced by any other JAxis) is evaluated as follows:
The expression is expanded to for $a in $A return $a ? child::KS (where $a is some otherwise unused variable name), which is then evaluated using the rules below.
If the value of $a is anything other than a map, an array, or a JNodeDM, then a type error is raised [err:XPTY0004].
If the value of $a is a map or an array, then it is wrapped in a root JNodeDM by applying the fn:JNode function, and the expression is then evaluated using the rules below.
If the value of $a is a JNodeDM, then the JAxis is evaluated, as described below, to deliver a sequence of JNodes.
This sequence of JNodes is filtered and potentially reordered, retaining only those JNodes that satisfy the KeySpecifierKS, as described in 4.13.3.1.2 Key Specifiers.
A JAxis is essentially a function that takes a JNode as its input and returns a sequence of JNodes. The rules for each JAxis follow:
The selfJAxis, applied to a JNode N, returns N.
The parentJAxis, applied to a JNode N, returns the ¶parent property of N, or an empty sequence if the property is absent.
The ancestorJAxis, applied to a JNode N, returns the transitive closure of the parentJAxis, that is, it returns the ¶parent, then the ¶parent of the ¶parent, and so on until the root JNode (which has no parent) is reached. The result is in reverse document order.
The ancestor-or-selfJAxis, applied to a JNode N, returns N followed by the content of the ancestorJAxis. The result is in reverse document order.
The childJAxis, applied to a JNode N, returns the result of the j-childrenDM accessor function defined in the data model. The effect of this accessor applied to a JNode $N is repeated here for convenience, assuming the existence of a function dm:JNode that returns a JNode with given values for its ¶parent, ¶position, ¶selector, and ¶value properties:
for $item at $pos in JNode-value($N)
return
if ($item instance of array(*))
then for member $member at $index in $item
return dm:JNode(parent := $P,
position := $pos,
selector := $index,
value := $member)
else if ($item instance of map(*))
then for key $key value $value in $item
return dm:JNode(parent := $P,
position := $pos,
selector := $key
value := $value)
else ()The order of the result reflects the order of members in an array or of entries within a map, and this is in document order by definition.
The descendantJAxis, applied to a JNode N, returns the transitive closure of the childJAxis. The result is in document order.
More specifically, the descendant axis returns the result of the recursive XQuery function:
function descendants($root as JNode()) as JNode()* {
for $c in $root ? child::*
return ($c, if ($c instance of JNode(array(*)|map(*)))
then descendants($c)
else ())
}The descendant-or-selfJAxis, applied to a JNode N, returns N followed by the content of the descendantJAxis. The result is in document order.
The KeySpecifier filters and potentially reorders the sequence of JNodes selected by the axis (referred to below as the raw result), as follows:
If the KeySpecifier is a LookupWildcard (that is, *) then the raw result is returned unchanged.
If the KeySpecifier is a TypeSpecifier, then the raw result is filtered to retain only those JNodes whose ¶value property is an instance of the sequence type defined by the KeySpecifier.
For example, the expression $array?child::~[array(*)] selects only those members of an array that are themselves arrays.
If the KeySpecifier is an NCName, then the raw result is filtered to retain only those JNodes whose ¶selector property is equal to the NCName under the rules of the atomic-equal function.
For example, the expression $map?child::status returns a JNode that wraps the map entry having the key "status", if such an entry exists.
Note:
There may be more than one such JNode in the case of a JAxis other than child.
In all other cases the KeySpecifer is treated as an expression. It is evaluated in the current context: call the result $K.
The final result is then obtained from the raw result $RR by evaluating the expression:
for $k in data($K)
return $RR[atomic-equal($k, JNode-selector(.))]Note:
This expression will never raise a type error, nor will it raise an error if an array index is out of bounds.
For example, the expression:
let $M := {1:10, 2:20, 3:30} return $M?child::(3, 1, 82)returns JNodes representing the entries 3:30 and 1:10 in that order; the index 82 selects nothing.
Note:
In a practical implementation one might reasonably expect that the entry for a particular key in a map can be accessed without a sequential search of all entries, as this pseudo-code would suggest.
The lookup operator ? (unlike the path operator /) does not force the results into document order, nor does it eliminate duplicates. If this is required, it can be achieved by a call on the distinct-ordered-nodes function (or more concisely, with the union operator: $X|() has the same effect as distinct-ordered-nodes($X)).
The expression $A ? .. is an abbreviation for $A ? parent::*. If $A is a root JNode (a JNode with no parent), or if it is a map or array (which is implicitly wrapped in a root JNode) then the expression returns an empty sequence; otherwise it returns the ¶parent property of $A. If $A is a sequence with multiple items, it returns the sequence concatenation of the results.
The expression $A ? . is an abbreviation for $A ? self::*. If $A is a JNode, it returns that JNode unchanged; otherwise, it returns the result of fn:JNode($A). If $A is a sequence containing multiple items, it returns the sequence concatenation of the result.
In the absence of an JAxis, a shallow lookup expression $C ? KS is evaluated as follows:
for $c in $C
return if ($c instance of JNode())
then $c ? child::KS
else JNode($c) ? child::KS =!> JNode-value()The effect of this is that when the left-hand operand of the expression contains a JNode, the result will be a sequence of JNodes. But when the left-hand operand is a simple map or array, the result will contain simple values, flattened by sequence concatenation.
For example, if $m is the map { "a":1, "b": 2 }, then $m?b returns the xs:integer value 2. In contrast, JNode($m)?b returns a JNode having ¶parent=JNode($m), ¶selector="b", ¶position=1, and ¶value=2.
Note:
The effect of returning a simple value as the result is to lose information. This happens in two ways. Firsly, no information is retained about the parent map or array, or about the key that was used to make the selection, which means it is not possible to navigate around the containing tree to obtain properties of other related JNodes. Secondly, the results are flattened into a single sequence: for example, the result of the lookup expression [ 1, 2, (), 4 ] ? * is the sequence ( 1, 2, 4 ), losing any information about the third array member.
This effect can be avoided in a number of ways:
Use of an explicit call on fn:JNode on the left-hand side, for example JNode($A) ? K
Use of an explicit axis, for example $A ? child::K
Implicit wrapping using the step ., for example $A ? . ? K. This is equivalent to $A ? self::* ? K, which (because it uses an explicit axis) ensures that the left-hand operand of the second ? operator is a JNode, and therefore the result of the final expression is also a JNode.
The reason for the design choice is primarily for backwards compatibility; but it also has the benefit of keeping common cases simple.
The following examples use the ? operator without a JAxis. Given $A as an array [ ("a", "b"), ("c", "d"), ("e", "f"), 42 ], some example Lookup expressions are:
| Expression | Result |
|---|---|
$A ? * | ("a", "b", "c", "d", "e", "f", 42) |
$A ? 2 | ("c", "d") |
$A ? (3, 1) | ("e", "f", "a", "b") |
$A ? 82 | ()Note: There is no array bound checking. |
$A ? xyz | ()Note: No type error is raised. |
$A ? ~[xs:integer] | 42 |
Similarly, given $M as a map { "X": ("a", "b"), "Y": ("c", "d"), "Z": ("e", "f"), "N": 42 }, some example lookup expressions are as follows.
| Expression | Result |
|---|---|
$M ? * | ("a", "b", "c", "d", "e", "f", 42) |
$M ? Y | ("c", "d") |
$M ? ("Z", "X") | ("e", "f", "a", "b") |
$M ? ~[xs:integer] | 42 |
The following examples illustrate lookup expressions on an array of maps, where some of the maps themselves contain arrays. The input array $A is:
[ {"a":1, "b":"banana"}, {"a":2, "b":"bread", "c":[10, 20, 30] } ]| Expression | Result |
|---|---|
$A ? * ? a | (1, 2) |
$A ? * ? c ? 2 | 20 |
data($A ? descendant::c ? parent::* ? a) | 2 |
data($A ? descendant::c ? .. ? a) | 2 |
$A ? ~[record(a, b, c)] ? c ? 2 | 20 |
UnaryLookup | ::= | Lookup |
Lookup | ::= | ShallowLookup | DeepLookup |
LookupShallowLookupLookupShallowLookup | ::= | ("?" | "??") ((".." | "." | ((ModifierJAxis "::")? KeySpecifier)) |
ModifierJAxisModifierJAxis | ::= | "pairsancestor" | "keysancestor-or-self" | "valueschild" | "items"descendant" | "descendant-or-self" | "parent" | "self" |
KeySpecifier | ::= | NCName | IntegerLiteral | StringLiteral | VarRef | ParenthesizedExpr | LookupWildcard | TypeSpecifier |
IntegerLiteral | ::= | Digits |
| /* ws: explicit */ | ||
Digits | ::= | DecDigit ((DecDigit | "_")* DecDigit)? |
| /* ws: explicit */ | ||
DecDigit | ::= | [0-9] |
| /* ws: explicit */ | ||
StringLiteral | ::= | AposStringLiteral | QuotStringLiteral |
| /* ws: explicit */ | ||
VarRef | ::= | "$" EQName |
ParenthesizedExpr | ::= | "(" Expr? ")" |
LookupWildcard | ::= | "*" |
TypeSpecifier | ::= | "~[" SequenceType "]" |
SequenceType | ::= | ("empty-sequence" "(" ")") |
DeepLookup | ::= | "??" KeySpecifier |
Unary lookup is most commonly used in predicates (for example, $map[?name = 'Mike']) or with the simple map operator (for example, avg($maps ! (?price - ?discount))).
The unary lookup expression ?modifieraxis::KS is defined to be equivalent to the postfix lookup expression .?modifieraxis::KS which has the context value (.) as the implicit first operand. See 4.13.3.1 Postfix Lookup Expressions for the postfix lookup operator.
Similarly, ?. is equivalent to .?., ?.. is equivalent to .?.., and ?KS (where KS is some KeySpecifier) is equivalent to .?KS.
Examples:
?name is equivalent to .("name"), an appropriate lookup for a map.
?2 is equivalent to .(2), an appropriate lookup for an array or an integer-valued map.
If the context item is the result of parsing the JSON input:
{
"name": "John Smith",
"address": { "street": "18 Acacia Avenue", "postcode": "MK12 2EX" },
"previous-address": { "street": "12 Seaview Road", "postcode": "EX8 9AA" }
}then ?*[. instance of ~[record(street, postcode)] ? postcode returns ("MK12 2EX", "EX8 9AA") (reflecting the entry orderDM of the map).
Note:
Writing ? * ? postcode would raise a type error, because the result of the initial step ? * includes an item (the string "John Smith") that is neither a map nor an array. Adding the TypeSpecifier avoids this problem.
?"first name" is equivalent to .("first name")
?($a)and ?$a are equivalent to for $k in $a return .($k), allowing keys for an array or map to be passed using a variable.
?(2 to 4) is equivalent to for $k in (2, 3, 4) return .($k), a convenient way to return a range of values from an array.
?(3.5) raises a type error if the context value is an array because the parameter must be an integer.
?(3.0e0) returns the same result as ?3, because xs:double(3.0e0) and xs:integer(3) compare equal under the rules of the atomic-equal function.
([ 1, 2, 3 ], [ 1, 2, 5 ], [ 1, 2 ])[?3 = 5] raises an error because ?3 on one of the items in the sequence fails.
If $m is bound to the weekdays map described in 4.13.1 Maps, then $m?* returns the values ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"), in implementation-dependent order.
[ 1, 2, 5, 7 ]?* evaluates to (1, 2, 5, 7).
[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]?* evaluates to ([ 1, 2, 3 ], [ 4, 5, 6 ])
[ [ 1, 2, 3 ], 4, 5 ]?*[. instance of array(xs:integer)] evaluates to ([ 1, 2, 3 ])
[ [ 1, 2, 3 ], [ 4, 5, 6 ], 7 ]?*[. instance of array(*)]?2 evaluates to (2, 5)
[ [ 1, 2, 3 ], 4, 5 ]?*[. instance of xs:integer] evaluates to (4, 5).
The deep lookup operator ?? has both unary and postfix forms. The unary formNo explicit JAxis ??modifier::KS (where KS is anyallowed, KeySpecifierbecause) has the same effect as the binary form the .??modifier::KSdescendant. JAxis is implicit.
The semantics are defined as follows.
First we define the recursive content of an item as follows:
declare function immediate-content($item as item()) as record(key, value)* {
if ($item instance of map(*)) {
map:pairs($item)
} else if ($item instance of array(*)) {
for member $m at $p in $item
return { "key": $p, "value": $m }
}
};
declare function recursive-content($item as item()) as record(key, value)* {
immediate-content($item) ! (., ?items::value ! recursive-content(.))
};Note:
Explanation: the immediate content of a map is obtained by splitting it into a sequence of key-value pairs, each representing one entry. The immediate content of an array is obtained by constructing a sequence of key-value pairs, one for each array member, where the key is the array index and the value is the corresponding member. Each key-value pair is of type record(key as xs:anyAtomicType, value as item()*). The recursive content of an item contains the key-value pairs in its immediate content, each followed by the recursive content obtained by expanding any maps or arrays in the immediate content.
It is then useful to represent the recursive content as a sequence of single-entry-mapsDM: so each pair { "key": $K, "value": $V } is converted to the form { $K: $V }. This can be achieved using the expression recursive-content($V) ! { ?key: ?value }.
In addition we define the function array-or-map as follows:
declare function array-or-map($item as item()) {
typeswitch ($item) {
case array(*) | map(*) return $item
default return error( #err:XPTY0004 )
}
}The result of the expression E??pairs::KS, where E is any expression and KS is any KeySpecifier, is then:
(E ! array-or-map(.) -> recursive-content(.) ! { ?key: ?value }) ?pairs::KS
DeepLookup | ::= | "??" KeySpecifier |
KeySpecifier | ::= | NCName | IntegerLiteral | StringLiteral | VarRef | ParenthesizedExpr | LookupWildcard | TypeSpecifier |
IntegerLiteral | ::= | Digits |
| /* ws: explicit */ | ||
Digits | ::= | DecDigit ((DecDigit | "_")* DecDigit)? |
| /* ws: explicit */ | ||
DecDigit | ::= | [0-9] |
| /* ws: explicit */ | ||
StringLiteral | ::= | AposStringLiteral | QuotStringLiteral |
| /* ws: explicit */ | ||
VarRef | ::= | "$" EQName |
ParenthesizedExpr | ::= | "(" Expr? ")" |
LookupWildcard | ::= | "*" |
TypeSpecifier | ::= | "~[" SequenceType "]" |
SequenceType | ::= | ("empty-sequence" "(" ")") |
A deep lookup expression is evaluated as follows:
$C ?? KS is equivalent to $C ? descendant::KS
Unlike the shallow lookup operator ?, the deep lookup operator always returns a sequence of JNodes.
The unary form is also available: the expression ?? KS is equivalent to . ?? KS.
Note:
This is best explained by considering examples.
Consider the expressionLet let $V :=be the array [ { "first": "John", "last": "Smith" }, { "first": "Mary", "last": "Evans" } ].
The recursive contentresult of this array $V ?? * => JNode-value() is thea sequence of six mapsvalues:
{ "first": "John", "last": "Smith" }
"John"
"Smith"
{ "key": "last", "value": "Smith" }
"Mary"
"Evans"
The associated selectors can be included in the result by forming a sequence of key-value pair records, using the expression $V ?? * ! { "key": JNode-selector(.), "value": JNode-value(.) }, which returns:
{ "key": 1, "value": { "first": "John", "last": "Smith" } }
{ "key": 2, "value": { "first": "Mary", "last": "Evans" } }
{ "key": "first", "value": "John" }
{ "key": "firstlast", "value": "JohnSmith" }
{ "key": "last"2, "value": { "Smith" }first": "Mary", "last": "Evans" } }
{ "key": "first", "value": "Mary" }
{ "key": "last", "value": "Evans" }
The expression $V??pairs::* returns this sequence.
With some other KeySpecifierKS, $V??pairs::KS returns selected items from this sequence that match KS. Formally this is achieved by converting the key-value pairs to single-entry mapsDM, applying the KeySpecifier to the sequence of single-entry maps, and then converting the result back into a sequence of key-value pairs.
For example, given the expression $V??pairs::first, the selection from the converted sequence will include the two single entry mapsDM{ "first" : "John" } and { "first" : "Mary" }, which will be delivered in key-value pair form as { "key": "first", "value": "John" }, { "key": "first", "value": "Mary" }.
The surnames of all people with first name "John" can be found using the expression:
$V ?? first [. = "John"] ? .. ? last => string()The call on string() here achieves the same effect as a call on JNode-value, because the string function, when applied to a JNode, extracts the ¶value property and converts it to a string. Indeed the predicate [. = "John"] is also implicitly extracting the ¶value property of a JNode: this happens automatically when a JNode is atomized, as required by the = operator.
The effect of using modifiers other than pairs is the same as with shallow lookup expressions:
If the modifier is items (explicitly or by default), the result of $V??items::KS is the same as the result of $V??pairs::KS ! map:get(., "value"); that is, it is the sequence concatenation of the value parts.
If the modifier is values, the result of $V??values::KS is the same as the result of $V??pairs::KS ! array { map:get(., "value") }.
If the modifier is keys, the result of $V??keys::KS is the same as the result of $V??pairs::KS ! map:get(., "key").
Note:
This means that with the example given earlier:
The expression $V ?? first returns the sequence "John", "Mary".
The expression $V ?? last returns the sequence "Smith", "Evans".
The expression $V ?? 1 returns the sequence { "first": "John", "last": "Smith" }.
The expression $V ?? ~[record(first, last)] ! `{ ?first } { ?last }` returns the sequence "John Smith", "Mary Evans". This expression selects all values of type record(first, last) at any level in the tree.
Note:
The effect of evaluating all shallow lookups on maps rather than arrays is that no error arises if an array subscript is out of bounds. In the above example, $value??3 would return an empty sequence, it would not raise an error.
This also affects the way an xs:untypedAtomic key value is handled. Given the shallow lookup expression $A?$x, if $A is an array and $x (after atomization) is xs:untypedAtomic then the value of $x is converted to an integer (by virtue of the coercion rules applying to a call on array:get). With a deep lookup expression $A??$x, by contrast, the semantics are defined in terms of a map lookup, in which xs:untypedAtomic items are always treated as strings.
Note:
The definition of the recursive-content function is such that items in the top-level value that are not maps or arrays are ignored, whereas items that are not themselves maps or arrays, but which appear in the content of a map or array at the top level, are included. This means that E??X mirrors the behavior of E//X, in that it includes all items that are one-or-more levels deep in the tree.
Note:
An expression involving multiple deep lookup operators may return duplicates. For example, the result of the expression [ [ [ "a" ], [ "b" ] ], [ [ "c" ], [ "d" ] ] ] ?? 1 ?? 1 is ([ "a" ], "a", "b", "a", "c"). This is because the first ?? operator selects members in position 1 at all three levels, that is it selects the arrays [ [ "a" ], [ "b" ] ], [ "a" ], and [ "c" ] as well as each of the four string values. The second ?? operator selects members in position 1 within each of these values, which results in the string "a" being selected twice.
Note:
A type error is raised if the value of the left-hand expression includes an item that is neither a map nor an array.
Consider the tree $tree of maps and arrays that results from applying the fn:parse-json function to the following JSON input:
{
"desc" : "Distances between several cities, in kilometers.",
"updated" : "2014-02-04T18:50:45",
"uptodate": true,
"author" : null,
"cities" : {
"Brussels": [
{ "to": "London", "distance": 322 },
{ "to": "Paris", "distance": 265 },
{ "to": "Amsterdam", "distance": 173 }
],
"London": [
{ "to": "Brussels", "distance": 322 },
{ "to": "Paris", "distance": 344 },
{ "to": "Amsterdam", "distance": 358 }
],
"Paris": [
{ "to": "Brussels", "distance": 265 },
{ "to": "London", "distance": 344 },
{ "to": "Amsterdam", "distance": 431 }
],
"Amsterdam": [
{ "to": "Brussels", "distance": 173 },
{ "to": "London", "distance": 358 },
{ "to": "Paris", "distance": 431 }
]
}
}Given two variables $from and $to containing the names of two cities that are present in this table, the distance between the two cities can be obtained with the expression:
$tree ?? $from ?? ~[record(to, distance)][?to = $to] ? distance$tree ?? $from ?? ~[record(to, distance)] [?to = $to] ? distanceThe names of all pairs of cities whose distance is represented in the data can be obtained with the expression:
$tree ?? $cities
=> map:for-each(fn($key, $val) { $val ?? to ! ($key || "-" || .) })$tree ?? "to" ! `{JNode-selector(?..?..)} to {.}`The JNodes returned by the initial lookup $tree ?? "to wrap values such as "London". The first ?.. selection from such a value returns a JNode wrapping a value such as the map { "to": "London", "distance": 322 }. The second ?.. selection returns the containing array. The selector for this array is a key in the containing map, such as "Brussels".
Note:
The quotation marks around the key "to" are not needed, but are included here to improve readability.
An alternative way to deliver the same result would be:
for key $from value $array in $tree ? cities
for $to in $array ?? "to"
return `{$from} to {$to}`
This example provides XPath equivalents to some examples given in the JSONPath specification. [TODO: add a reference].
The examples query the result of parsing the following JSON value, representing a store whose stock consists of four books and a bicycle:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 399
}
}
}The following table illustrates some queries on this data, expressed both in JSONPath and in XPath 4.0.
| Query | JSONPath | XPath 4.0 |
|---|---|---|
| The authors of all books in the store | $.store.book[*].author$.store.book[*].author | ?store?book??author$m?store?book??author |
| All authors | $..author$..author | ??author$m??author |
| All things in store (four books and a red bicycle) | $.store.* $.store.* | ?store?*$m?store?* |
| The prices of everything in the store | $.store..price$.store..price | ?store??price$m?store??price |
| The third book | $..book[2] $..book[2] | ??book?3$m??book?3 |
| The third book's author | $..book[2].author$..book[2].author | ??book?3?author$m??book?3?author |
| The third book's publisher (empty result) | $..book[2].publisher$..book[2].publisher | ??book?3?publisher$m??book?3?publisher |
| The last book (in order) | $..book[-1]$..book[-1] | ??book?*[last()]$m??book => array:foot()?*[last()] |
| The first two books | $..book[0,1]$..book[0,1] | ??book?(1,2)$m??book?(1, 21,2) |
| All books with an ISBN | $..book[?@.isbn]$..book[?@.isbn] | ??book[?isbn]$m??book[?isbn] |
| All books cheaper than 10 | $..book[?@.price<10]$..book[?@.price<10] | ??book[?price lt 10]$m??book[?price lt 10] |
| All member values and array elements contained in the input value | $..*$..* | ??*$m??* |
Unlike navigation within node trees derived from XML, navigation within a tree of maps and arrays derived from JSON is normally “downwards only”: there is no equivalent of the parent or ancestor axis. This means, for example, that having selected leav nodes in the tree with an expression such as ??name, there is no way of navigating from the items in the result to any related items. Pinned maps and arrays provide a solution to this problem; if a map or array is pinned (by calling the fn:pin function), then values found by navigating within the map or array are labeled, which provides supplementary information about their location within the containing tree structure.
For further information about pinned and labeled values see Section 3.3 Labeled ItemsDM.
More specifically, if a map $M or an array $A is pinned, then any value returned by map:get($M, $key) or array:get($A, $index) will be a sequence of labeled items. The label can be obtained by calling the function fn:label, and the result will be a map having the following properties:
pinned: set to true. This means that any further selections from this value (if it is itself a map or array) will also deliver labeled items.
parent: the containing map ($M) or array ($A).
key: the key ($key) or index ($index) that was used to select the item.
position: in the general case the value returned by map:get or array:get is a sequence, and each item in the sequence is labeled with its 1-based position in that sequence.
ancestors: a zero-arity function that delivers the item’s parent (its containing map or array), that item’s parent, and so on, recursively, up to the map or array that was the root of the selection. The sequence is in upwards navigation order (the immediate parent comes first).
path: a zero-arity function that delivers the sequence of keys (in the case of maps) or integer indexes (in the case of arrays) by which the item was reached. The sequence is in downwards navigation order (the immediate key or index of the item comes last).
The formal model for the fn:pin is that it returns a deep copy of the supplied map or array in which all items in the recursive content have been labeled. This is a useful model because it avoids the need to specify the effect of each individual function and operator on the structure. For example, the rule has the consequence that the result of pin([ 11, 12, 13, 14 ]) => array:remove(3) => array:for-each(fn { label(.)?key }) is [ 1, 2, 4 ]. In a practical implementation, however, it is likely that labels will be attached to items lazily, at the time they are retrieved. Such an implementation will need to recognize pinned maps and arrays and treat them specially when operations such as array:get, array:remove, array:for-each, array:subarray, and their map counterparts, are evaluated.
Because maps and arrays selected from a pinned map or array are themselves pinned, deep lookup operations (whether conducted using the deep lookup operator ??, or the map:find function, or by user-written recursive code) will deliver a labeled value whose parent or ancestor properties can be used to navigate back up through the tree.
For example, given the example map shown in 4.13.1.1 Map Constructors, the expression $map??last[. = "Suciu"] selects the map entry with key "last" and value "Suciu", but by itself gives no information about where this entry was found. By first pinning the map, this extra information can be made available through the label on the result. For example you can select all co-authors of "Suciu" by writing:
pin($map)??last[. = "Suciu"] => label()?ancestors()?author??lastNote:
When an entry in a map, or a member of an array, has the empty sequence as its value, the value contains no items and is therefore unchanged in the pinned version of the containing structure. In addition, the lookup operators ? and ?? flatten their result to a single sequence, so any empty values are effectively discarded from the result. For this reason, pinned arrays and maps work best when all values in arrays and maps are singleton items. An option is therefore provided on the fn:parse-json and fn:json-doc functions to change the representation of JSON null values (whose default is an empty sequence, ()) to a user-supplied value.
| Editorial note | |
| That note is anticipating a proposal in a separate PR. | |
The instance of, cast, castable, and treat expressions are used to test whether a value conforms to a given type or to convert it to an instance of a given type.
CastExpr | ::= | PipelineExpr ("cast" "as" CastTarget "?"?)? |
PipelineExpr | ::= | ArrowExpr ("->" ArrowExpr)* |
CastTarget | ::= | TypeName | ChoiceItemType | EnumerationType |
TypeName | ::= | EQName |
EQName | ::= | QName | URIQualifiedName |
ChoiceItemType | ::= | "(" (ItemType ++ "|") ")" |
ItemType | ::= | AnyItemTest | TypeName | KindTest | GNodeType | JNodeType | FunctionType | MapType | ArrayType | RecordType | EnumerationType | ChoiceItemType |
EnumerationType | ::= | "enum" "(" (StringLiteral ++ ",") ")" |
Sometimes it is necessary to convert a value to a specific datatype. For this purpose, XPath 4.0 provides a cast expression that creates a new value of a specific type based on an existing value. A cast expression takes two operands: an input expression and a target type. The type of the atomized value of the input expression is called the input type. The target type must be a generalized atomic type. In practice this means it may be any of:
The name of an named item type defined in the static context, which in turn must refer to an item type in one of the following categories.
The name of a type defined in the in-scope schema types, which must be a simple type (of variety atomic, list or union) [err:XQST0052] . In addition, the target type cannot be xs:NOTATION, xs:anySimpleType, or xs:anyAtomicType
A ChoiceItemType representing a generalized atomic type (such as (xs:date | xs:dateTime)).
An EnumerationType such as enum("red", "green", "blue").
Otherwise, a static error is raised [err:XPST0080].
The optional occurrence indicator ? denotes that an empty sequence is permitted.
Casting a node to xs:QName can cause surprises because it uses the static context of the cast expression to provide the namespace bindings for this operation. Instead of casting to xs:QName, it is generally preferable to use the fn:QName function, which allows the namespace context to be taken from the document containing the QName.
The semantics of the cast expression are as follows:
The input expression is evaluated.
The result of the first step is atomized.
If the result of atomization is a sequence of more than one atomic item, a type error is raised [err:XPTY0004].
If the result of atomization is an empty sequence:
If ? is specified after the target type, the result of the cast expression is an empty sequence.
If ? is not specified after the target type, a type error is raised [err:XPTY0004].
If the result of atomization is a single atomic item, the result of the cast expression is determined by casting to the target type as described in Section 2223 CastingFO. When casting, an implementation may need to determine whether one type is derived by restriction from another. An implementation can determine this either by examining the in-scope schema definitions or by using an alternative, implementation-dependent mechanism such as a data dictionary. The result of a cast expression is one of the following:
A value of the target type (or, in the case of list types, a sequence of values that are instances of the item type of the list type).
A type error, if casting from the source type to the target type is not supported (for example attempting to convert an integer to a date).
A dynamic error, if the particular input value cannot be converted to the target type (for example, attempting to convert the string "three" to an integer).
Note:
Casting to an enumeration type relies on the fact that an enumeration type is a generalized atomic type. So cast $x as enum("red") is equivalent to casting to an anonymous atomic type derived from xs:string whose enumeration facet restricts the value space to the single string "red", while cast $x as enum("red", "green") is equivalent to casting to (enum("red") | enum("green")).
CastableExpr | ::= | CastExpr ("castable" "as" CastTarget "?"?)? |
CastExpr | ::= | PipelineExpr ("cast" "as" CastTarget "?"?)? |
CastTarget | ::= | TypeName | ChoiceItemType | EnumerationType |
TypeName | ::= | EQName |
EQName | ::= | QName | URIQualifiedName |
ChoiceItemType | ::= | "(" (ItemType ++ "|") ")" |
ItemType | ::= | AnyItemTest | TypeName | KindTest | GNodeType | JNodeType | FunctionType | MapType | ArrayType | RecordType | EnumerationType | ChoiceItemType |
EnumerationType | ::= | "enum" "(" (StringLiteral ++ ",") ")" |
XPath 4.0 provides an expression that tests whether a given value is castable into a given target type. The target type is subject to the same rules as the target type of a cast expression.
The expression E castable as T returns true if the result of evaluating E can be successfully cast into the target type T by using a cast expression; otherwise it returns false. If evaluation of E fails with a dynamic error or if the value of E cannot be atomized, the castable expression as a whole fails.
The castable expression can be used as a predicate to avoid errors at evaluation time. It can also be used to select an appropriate type for processing of a given value, as illustrated in the following example:
if ($x castable as hatsize) then $x cast as hatsize else if ($x castable as IQ) then $x cast as IQ else $x cast as xs:string
Note:
The expression $x castable as enum("red", "green", "blue") is for most practical purposes equivalent to $x = ("red", "green", "blue"); the main difference is that it uses the Unicode codepoint collation for comparing strings, not the default collation from the static context.
For every simple type in the in-scope schema types (except xs:NOTATION and xs:anyAtomicType, and xs:anySimpleType, which are not instantiable), a constructor function is implicitly defined. In each case, the name of the constructor function is the same as the name of its target type (including namespace). The signature of the constructor function for a given type depends on the type that is being constructed, and can be found in Section 2122 Constructor functionsFO.
There is also a constructor function for every named item type in the static context that expands either to a generalized atomic typeor to a RecordType.
All such constructor functions are classified as system functions.
Note:
The constructor function is present in the static context if and only if the corresponding type is present in the static context.
For XSLT, this means that a constructor function corresponding to an imported schema type is private to the stylesheet package, and a constructor function corresponding to an xsl:item-type declaration has the same visibility as the xsl:item-type declaration.
For XQuery, this means that a constructor function corresponding to an imported schema type is private to the query module, and a constructor function corresponding to a named item type declaration is %public or %private according to the annotations on the item type declaration.
[Definition: The constructor function for a given simple type is used to convert instances of other simple types into the given type. The semantics of the constructor function call T($arg) are defined to be equivalent to the expression $arg cast as T?.]
The following examples illustrate the use of constructor functions:
This example is equivalent to "2000-01-01" cast as xs:date?.
xs:date("2000-01-01")This example is equivalent to ($floatvalue * 0.2E-5) cast as xs:decimal?.
xs:decimal($floatvalue * 0.2E-5)
This example returns an xs:dayTimeDuration value equal to 21 days. It is equivalent to "P21D" cast as xs:dayTimeDuration?.
xs:dayTimeDuration("P21D")If usa:zipcode is a user-defined atomic type in the in-scope schema types, then the following expression is equivalent to the expression ("12345" cast as usa:zipcode?).
usa:zipcode("12345")If my:chrono is a named item type that expands to (xs:date | xs:time | xs:dateTime), then the result of my:chrono("12:00:00Z") is the xs:time value 12:00:00Z.
If my:location is a named item type that expands to record(latitude as xs:double, longitude as xs:double), then the result of my:location(50.52, -3.02) is the map { 'latitude': 50.52e0, 'longitude': -3.02e0 }.
Note:
An instance of an atomic type whose name is in no namespace can be constructed by using a URIQualifiedName in either a cast expression or a constructor function call. Examples:
17 cast as Q{}appleQ{}apple(17)In either context, using an unqualified NCName might not work: in a cast expression, an unqualified name is it is interpreted according to the default namespace for elements and types, while an unqualified name in a constructor function call is resolved using the default function namespace which will often be inappropriate.
The grammar of XPath 4.0 uses the same simple Extended Backus-Naur Form (EBNF) notation as [XML 1.0] with the following differences.
The notation XYZ ** "," indicates a sequence of zero or more occurrences of XYZ, with a single comma between adjacent occurrences.
The notation XYZ ++ "," indicates a sequence of one or more occurrences of XYZ, with a single comma between adjacent occurrences.
All named symbols have a name that begins with an uppercase letter.
It adds a notation for referring to productions in external specifications.
Comments or extra-grammatical constraints on grammar productions are between '/*' and '*/' symbols.
A 'xgc:' prefix is an extra-grammatical constraint, the details of which are explained in A.1.2 Extra-grammatical Constraints
A 'ws:' prefix explains the whitespace rules for the production, the details of which are explained in A.3.5 Whitespace Rules
A 'gn:' prefix means a 'Grammar Note', and is meant as a clarification for parsing rules, and is explained in A.1.3 Grammar Notes. These notes are not normative.
The terminal symbols for this grammar include the quoted strings used in the production rules below, and the terminal symbols defined in section A.3.1 Terminal Symbols. The grammar is a little unusual in that parsing and tokenization are somewhat intertwined: for more details see A.3 Lexical structure.
The EBNF notation is described in more detail in A.1.1 Notation.
This section describes how an XPath 4.0 text is tokenized prior to parsing.
All keywords are case sensitive. Keywords are not reserved—that is, any lexical QName may duplicate a keyword except as noted in A.4 Reserved Function Names.
Tokenizing an input string is a process that follows the following rules:
[Definition: An ordinary production rule is a production rule in A.1 EBNF that is not annotated ws:explicit.]
[Definition: A literal terminal is a token appearing as a string in quotation marks on the right-hand side of an ordinary production rule.]
Note:
Strings that appear in other production rules do not qualify. For example, BracedURILiteral does not quality because it appears only in URIQualifiedName, and "0x" does not qualify because it appears only in HexIntegerLiteral.
The literal terminals in XPath 4.0 are: !!=#$()*+,...///::::=<<<<===!>=>>>=>>????[@[]{|||}~[×÷%method-->ancestorancestor-or-selfandarrayasatattributecastcastablechildcommentdescendantdescendant-or-selfdivdocument-nodeelementelseempty-sequenceenumeqeveryexceptfnfollowingfollowing-or-selffollowing-siblingfollowing-sibling-or-selfforfunctiongeGNodegtidivifininstanceintersectisitemitemsJNodekeykeysleletltmapmembermodnamespacenamespace-nodenenodeoforotherwisepairsparentprecedingpreceding-or-selfpreceding-siblingpreceding-sibling-or-selfprocessing-instructionrecordreturnsatisfiesschema-attributeschema-elementselfsometextthentotreatunionvaluevalues
[Definition: A variable terminal is an instance of a production rule that is not itself an ordinary production rule but that is named (directly) on the right-hand side of an ordinary production rule.]
The variable terminals in XPath 4.0 are: BinaryIntegerLiteralDecimalLiteralDoubleLiteralHexIntegerLiteralIntegerLiteralNCNameQNameStringLiteralStringTemplateURIQualifiedNameWildcard
[Definition: A complex terminal is a variable terminal whose production rule references, directly or indirectly, an ordinary production rule.]
The complex terminals in XPath 4.0 are: StringTemplate
Note:
The significance of complex terminals is that at one level, a complex terminal is treated as a single token, but internally it may contain arbitrary expressions that must be parsed using the full EBNF grammar.
Tokenization is the process of splitting the supplied input string into a sequence of terminals, where each terminal is either a literal terminal or a variable terminal (which may itself be a complex terminal). Tokenization is done by repeating the following steps:
Starting at the current position, skip any whitespace and comments.
If the current position is not the end of the input, then return the longest literal terminal or variable terminal that can be matched starting at the current position, regardless whether this terminal is valid at this point in the grammar. If no such terminal can be identified starting at the current position, or if the terminal that is identified is not a valid continuation of the grammar rules, then a syntax error is reported.
Note:
Here are some examples showing the effect of the longest token rule:
The expression map{a:b} is a syntax error. Although there is a tokenization of this string that satisfies the grammar (by treating a and b as separate expressions), this tokenization does not satisfy the longest token rule, which requires that a:b is interpreted as a single QName.
The expression 10 div3 is a syntax error. The longest token rule requires that this be interpreted as two tokens ("10" and "div3") even though it would be a valid expression if treated as three tokens ("10", "div", and "3").
The expression $x-$y is a syntax error. This is interpreted as four tokens, ("$", "x-", "$", and "y").
Note:
The lexical production rules for variable terminals have been designed so that there is minimal need for backtracking. For example, if the next terminal starts with "0x", then it can only be either a HexIntegerLiteral or an error; if it starts with "`" (and not with "```") then it can only be a StringTemplate or an error.
This convention, together with the rules for whitespace separation of tokens (see A.3.2 Terminal Delimitation) means that the longest-token rule does not normally result in any need for backtracking. For example, suppose that a variable terminal has been identified as a StringTemplate by examining its first few characters. If the construct turns out not to be a valid StringTemplate, an error can be reported without first considering whether there is some shorter token that might be returned instead.
Tokenization unambiguously identifies the boundaries of the terminals in the input, and this can be achieved without backtracking or lookahead. However, tokenization does not unambiguously classify each terminal. For example, it might identify the string "div" as a terminal, but it does not resolve whether this is the operator symbol div, or an NCName or QName used as a node test or as a variable or function name. Classification of terminals generally requires information about the grammatical context, and in some cases requires lookahead.
Note:
Operationally, classification of terminals may be done either in the tokenizer or the parser, or in some combination of the two. For example, according to the EBNF, the expression "parent::x" is made up of three tokens, "parent", "::", and "x". The name "parent" can be classified as an axis name as soon as the following token "::" is recognized, and this might be done either in the tokenizer or in the parser. (Note that whitespace and comments are allowed both before and after "::".)
In the case of a complex terminal, identifying the end of the complex terminal typically involves invoking the parser to process any embedded expressions. Tokenization, as described here, is therefore a recursive process. But other implementations are possible.
Note:
Previous versions of this specification included the statement: When tokenizing, the longest possible match that is consistent with the EBNF is used.
Different processors are known to have interpreted this in different ways. One interpretation, for example, was that the expression 10 div-3 should be split into four tokens (10, div, -, 3) on the grounds that any other tokenization would give a result that was inconsistent with the EBNF grammar. Other processors report a syntax error on this example.
This rule has therefore been rewritten in version 4.0. Tokenization is now entirely insensitive to the grammatical context; div-3 is recognized as a single token even though this results in a syntax error. For some implementations this may mean that expressions that were accepted in earlier releases are no longer accepted in 4.0.
XPath 4.0 expressions consist of terminal symbols and symbol separators.
Literal and variable terminal symbols are of two kinds: delimiting and non-delimiting.
[Definition: The delimiting terminal symbols are: !!=#$%method()**:+,-->...///::*:::=<<<<===!>=>>>=>>????[@[]```{{{|||}}}~[×÷AposStringLiteralBracedURILiteralQuotStringLiteralStringLiteral ]
[Definition: The non-delimiting terminal symbols are: ancestorancestor-or-selfandarrayasatattributecastcastablechildcommentdescendantdescendant-or-selfdivdocument-nodeelementelseempty-sequenceenumeqeveryexceptfnfollowingfollowing-or-selffollowing-siblingfollowing-sibling-or-selfforfunctiongeGNodegtidivifininstanceintersectisitemitemsJNodekeykeysleletltmapmembermodnamespacenamespace-nodenenodeoforotherwisepairsparentprecedingpreceding-or-selfpreceding-siblingpreceding-sibling-or-selfprocessing-instructionrecordreturnsatisfiesschema-attributeschema-elementselfsometextthentotreatunionvaluevaluesBinaryIntegerLiteralDecimalLiteralDoubleLiteralHexIntegerLiteralIntegerLiteralNCNameQNameURIQualifiedName ]
[Definition: Whitespace and Comments function as symbol separators. For the most part, they are not mentioned in the grammar, and may occur between any two terminal symbols mentioned in the grammar, except where that is forbidden by the /* ws: explicit */ annotation in the EBNF, or by the /* xgc: xml-version */ annotation.]
As a consequence of the longest token rule (see A.3 Lexical structure), one or more symbol separators are required between two consecutive terminal symbols T and U (where T precedes U) when any of the following is true:
T and U are both non-delimiting terminal symbols.
T is a QName or an NCName and U is "." or "-".
T is a numeric literal and U is ".", or vice versa.
An anonymous function is a function item with no name. Anonymous functions may be created, for example, by evaluating an inline function expression or by partial function application.
Application functions are function definitions written in a host language such as XQuery or XSLT whose syntax and semantics are defined in this family of specifications. Their behavior (including the rules determining the static and dynamic context) follows the rules for such functions in the relevant host language specification.
An argument to a function call is either an argument expression or an ArgumentPlaceholder (?); in both cases it may either be supplied positionally, or identified by a name (called a keyword).
A function definition has an arity range, which is a range of consecutive non-negative integers. If the function definition has M required parameters and N optional parameters, then its arity range is from M to M+N inclusive.
An array is a function item that associates a set of positions, represented as positive integer keys, with values.
The value associated with a given key is called the associated value of the key.
An atomic item is a value in the value space of an atomic type, as defined in [XML Schema 1.0] or [XML Schema 1.1].
An atomic type is a simple schema type whose {variety}XS11-1 is atomic.
Atomization of a sequence is defined as the result of invoking the fn:data function, as defined in Section 2.1.4 fn:dataFO.
Available binary resources. This is a mapping of strings to binary resources. Each string represents the absolute URI of a resource. The resource is returned by the fn:unparsed-binary function when applied to that URI.
Available documents. This is a mapping of strings to document nodes. Each string represents the absolute URI of a resource. The document node is the root of a tree that represents that resource using the data model. The document node is returned by the fn:doc function when applied to that URI.
Available collections. This is a mapping of strings to sequences of items. Each string represents the absolute URI of a resource. The sequence of items represents the result of the fn:collection function when that URI is supplied as the argument.
Available text resources. This is a mapping of strings to text resources. Each string represents the absolute URI of a resource. The resource is returned by the fn:unparsed-text function when applied to that URI.
Available URI collections. This is a mapping of strings to sequences of URIs. The string represents the absolute URI of a resource which can be interpreted as an aggregation of a number of individual resources each of which has its own URI. The sequence of URIs represents the result of the fn:uri-collection function when that URI is supplied as the argument.
An axis step returns a sequence of nodes that are reachable from a starting node via a specified axis. Such a step has two parts: an axis, which defines the "direction of movement" for the step, and a node test, which selects nodes based on their kind, name, and/or type annotation .
The result of evaluating the binding expression in a for expression is called the binding collection
A choice item type defines an item type that is the union of a number of alternatives. For example the type (xs:hexBinary | xs:base64Binary) defines the union of these two primitive atomic types, while the type (map(*) | array(*)) matches any item that is either a map or an array.
The coercion rules are rules used to convert a supplied value to a required type, for example when converting an argument of a function call to the declared type of the function parameter.
A collation is a specification of the manner in which strings and URIs are compared and, by extension, ordered. For a more complete definition of collation, see Section 5.3 Comparison of stringsFO.
One way to construct a sequence is by using the comma operator, which evaluates each of its operands and concatenates the resulting sequences, in order, into a single result sequence.
A complex terminal is a variable terminal whose production rule references, directly or indirectly, an ordinary production rule.
The constructor function for a given simple type is used to convert instances of other simple types into the given type. The semantics of the constructor function call T($arg) are defined to be equivalent to the expression $arg cast as T?.
In an enclosed expression, the optional expression enclosed in curly brackets is called the content expression.
A function definition is said to be context dependent if its result depends on the static or dynamic context of its caller. A function definition may be context-dependent for some arities in its arity range, and context-independent for others: for example fn:name#0 is context-dependent while fn:name#1 is context-independent.
When the context value is a single item, it can also be referred to as the context item; when it is a single node, it can also be referred to as the context node.
The context position is the position of the context value within the series of values currently being processed.
The context size is the number of values in the series of values currently being processed.
The context value is the value currently being processed.
Current dateTime. This information represents an implementation-dependent point in time during the processing of an expression, and includes an explicit timezone. It can be retrieved by the fn:current-dateTime function. If called multiple times during the execution of an expression, this function always returns the same result.
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) 4.0].
decimal-separator(M, R) is used to separate the integer part of the number from the fractional part. The default value for both the marker and the rendition is U+002E (FULL STOP, PERIOD, .) .
Default calendar. This is the calendar used when formatting dates in human-readable output (for example, by the functions fn:format-date and fn:format-dateTime) if no other calendar is requested. The value is a string.
Default collation. This identifies one of the collations in statically known collations as the collation to be used by functions and operators for comparing and ordering values of type xs:string and xs:anyURI (and types derived from them) when no explicit collation is specified.
Default collection. This is the sequence of items that would result from calling the fn:collection function with no arguments.
When an unprefixed lexical QName is expanded using the default element namespace rule, then it uses the default namespace for elements and types. If this is absent, or if it takes the special value ##any, then the no-namespace rule is used.
Default function namespace. This is either a namespace URI, or absentDM. The namespace URI, if present, is used for any unprefixed QName appearing in a position where a function name is expected.
When an unprefixed lexical QName is expanded using the default function namespace rule, it uses the default function namespace from the static context.
The default in-scope namespace of an element node
Default language. This is the natural language used when creating human-readable output (for example, by the functions fn:format-date and fn:format-integer) if no other language is requested. The value is a language code as defined by the type xs:language.
Default namespace for elements and types. This is either a namespace URI, or the special value "##any", or absentDM. This indicates how unprefixed QNames are interpreted when they appear in a position where an element name or type name is expected.
Default place. This is a geographical location used to identify the place where events happened (or will happen) when processing dates and times using functions such as fn:format-date, fn:format-dateTime, and fn:civil-timezone, if no other place is specified. It is used when translating timezone offsets to civil timezone names, and when using calendars where the translation from ISO dates/times to a local representation is dependent on geographical location. Possible representations of this information are an ISO country code or an Olson timezone name, but implementations are free to use other representations from which the above information can be derived. The only requirement is that it should uniquely identify a civil timezone, which means that country codes for countries with multiple timezones, such as the United States, are inadequate.
When an unprefixed lexical QName is expanded using the default type namespace rule, it uses the default namespace for elements and types. If this is absent, the no-namespace rule is used. If the default namespace for elements and types has the special value ##any, then the lexical QName refers to a name in the namespace http://www.w3.org/2001/XMLSchema.
Default URI collection. This is the sequence of URIs that would result from calling the fn:uri-collection function with no arguments.
The delimiting terminal symbols are: !!=#$%method()**:+,-->...///::*:::=<<<<===!>=>>>=>>????[@[]```{{{|||}}}~[×÷AposStringLiteralBracedURILiteralQuotStringLiteralStringLiteral
A schema typeS1 is said to derive fromschema typeS2 if any of the following conditions is true:
S1 is the same type as S2.
S2 is the base type of S1.
S2 is a pure union type of which S1 is a member type.
There is a schema typeM such that S1derives fromM and Mderives fromS2.
digit(M) is a character used in the picture string to represent an optional digit; the default value is U+0023 (NUMBER SIGN, #) .
Informally, document order is the order in which nodes appear in the XML serialization of a document.
Dynamically known function definitions. This is a set of function definitions. It includes the statically known function definitions as a subset, but may include other function definitions that are not known statically.
The dynamic context of an expression is defined as information that is needed for the dynamic evaluation of an expression, beyond any information that is needed from the static context.
A dynamic error is an error that must be detected during the dynamic evaluation phase and may be detected during the static analysis phase.
The dynamic evaluation phase is the phase during which the value of an expression is computed.
A dynamic function call consists of a base expression that returns the function and a parenthesized list of zero or more arguments (argument expressions or ArgumentPlaceholders).
A dynamic function call is an expression that is evaluated by calling a function item, which is typically obtained dynamically.
Every value matches one or more sequence types. A value is said to have a dynamic typeT if it matches (or is an instance of) the sequence type T.
The effective boolean value of a value is defined as the result of applying the fn:boolean function to the value, as defined in Section 8.3.1 fn:booleanFO.
When an unprefixed lexical QName is expanded using the element name matching rule rule, then it uses the default namespace for elements and types. If this is absent, then it uses the no-namespace rule. But if it takes the special value ##any, then the name is taken as matching any expanded QName with the corresponding local part, regardless of namespace: that is, the unprefixed name local is interpreted as *:local.
A sequence containing zero items is called an empty sequence.
An enclosed expression is an instance of the EnclosedExpr production, which allows an optional expression within curly brackets.
Each key / value pair in a map is called an entry.
An EnumerationType accepts a fixed set of string values.
Environment variables. This is a mapping from names to values. Both the names and the values are strings. The names are compared using an implementation-defined collation, and are unique under this collation. The set of environment variables is implementation-defined and may be empty.
In addition to its identifying QName, a dynamic error may also carry a descriptive string and one or more additional values called error values.
Executable Base URI. This is an absolute URI used to resolve relative URIs during the evaluation of expressions; it is used, for example, to resolve a relative URI supplied to the fn:doc or fn:unparsed-text functions.
An expanded QName is a triple: its components are a prefix, a local name, and a namespace URI. In the case of a name in no namespace, the namespace URI and prefix are both absent. In the case of a name in the default namespace, the prefix is absent.
exponent-separator(M, R) is used to separate the mantissa from the exponent in scientific notation. The default value for both the marker and the rendition is U+0065 (LATIN SMALL LETTER E, e) .
The expression context for a given expression consists of all the information that can affect the result of the expression.
External functions can be characterized as functions that are neither part of the processor implementation, nor written in a language whose semantics are under the control of this family of specifications. The semantics of external functions, including any context dependencies, are entirely implementation-defined. In XSLT, external functions are called Section 24.1 Extension Functions XT30.
A filter expression is an expression in the form E1[E2]: its effect is to return those items from the value of E1 that satisfy the predicate in E2.
A fixed focus is a focus for an expression that is evaluated once, rather than being applied to a series of values; in a fixed focus, the context value is set to one specific value, the context position is 1, and the context size is 1.
The first three components of the dynamic context (context value, context position, and context size) are called the focus of the expression.
A focus function is an inline function expression in which the function signature is implicit: the function takes a single argument of type item()* (that is, any value), and binds this to the context value when evaluating the function body, which returns a result of type item()*.
Function coercion wraps a function item in a new function whose signature is the same as the expected type. This effectively delays the checking of the argument and return types until the function is called.
A function definition contains information used to evaluate a static function call, including the name, parameters, and return type of the function.
A function item is an item that can be called using a dynamic function call.
A generalized atomic type is an item type whose instances are all atomic items. Generalized atomic types include (a) atomic types, either built-in (for example xs:integer) or imported from a schema, (b) pure union types, either built-in (xs:numeric and xs:error) or imported from a schema, (c) choice item types if their alternatives are all generalized atomic types, and (d) enumeration types.
grouping-separator(M, R) is used to separate groups of digits (for example as a thousands separator). The default value for both the marker and the rendition is U+002C (COMMA, ,) .
An expression E is said to be guarded by some governing condition C if evaluation of E is not allowed to fail with a dynamic error except when C applies.
A host language for XPath is any environment that provides capabilities for XPath expressions to be defined and evaluated, and that supplies a static and dynamic context for their evaluation.
Ignorable whitespace consists of any whitespace characters that may occur between terminals, unless these characters occur in the context of a production marked with a ws:explicit annotation, in which case they can occur only where explicitly specified (see A.3.5.2 Explicit Whitespace Handling).
Certain expressions, while not erroneous, are classified as being implausible, because they achieve no useful effect.
Implementation-defined indicates an aspect that may differ between implementations, but must be specified by the implementer for each particular implementation.
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 implementer for any particular implementation.
Implicit timezone. This is the timezone to be used when a date, time, or dateTime value that does not have a timezone is used in a comparison or arithmetic operation. The implicit timezone is an implementation-defined value of type xs:dayTimeDuration. See Section 3.2.7.3 Timezones XS1-2 or Section 3.3.7 dateTime XS11-2 for the range of valid values of a timezone.
infinity(R) is the string used to represent the double value infinity (INF); the default value is the string "Infinity"
An inline function expression, when evaluated, creates an anonymous function defined directly in the inline function expression.
In-scope attribute declarations. Each attribute declaration is identified either by an expanded QName (for a top-level attribute declaration) or by an implementation-dependent attribute identifier (for a local attribute declaration).
In-scope element declarations. Each element declaration is identified either by an expanded QName (for a top-level element declaration) or by an implementation-dependent element identifier (for a local element declaration).
In-scope named item types. This is a mapping from expanded QNames to named item types.
The in-scope namespaces property of an element node is a set of namespace bindings, each of which associates a namespace prefix with a URI.
In-scope schema definitions is a generic term for all the element declarations, attribute declarations, and schema type definitions that are in scope during static analysis of an expression.
In-scope schema types. Each schema type definition is identified either by an expanded QName (for a named type) or by an implementation-dependent type identifier (for an anonymous type). The in-scope schema types include the predefined schema types described in 3.5 Schema Types.
In-scope variables. This is a mapping from expanded QNames to sequence types. It defines the set of variables that are available for reference within an expression. The expanded QName is the name of the variable, and the type is the static type of the variable.
An item is either an atomic item, a node, or a function item.
An item type is a type that can be expressed using the ItemType syntax, which forms part of the SequenceType syntax. Item types match individual items.
An item type designator is a syntactic construct conforming to the grammar rule ItemType. An item type designator is said to designate an item type.
An alternative form of a node test called a kind test can select nodes based on their kind, name, and type annotation.
A lexical QName is a name that conforms to the syntax of the QName production
A literal is a direct syntactic representation of an atomic item.
A literal terminal is a token appearing as a string in quotation marks on the right-hand side of an ordinary production rule.
A map is a function that associates a set of keys with values, resulting in a collection of key / value pairs.
The mapping arrow operator=!> applies a function to each item in a sequence.
MAY means that an item is truly optional.
The values of an array are called its members.
A method is a function item that has the annotation %method.
minus-sign(R) is the string used to mark negative numbers; the default value is U+002D (HYPHEN-MINUS, -) .
MUST means that the item is an absolute requirement of the specification.
MUST NOT means that the item is an absolute prohibition of the specification.
A named function reference is an expression (written name#arity) which evaluates to a function item, the details of the function item being based on the properties of a function definition in the static context.
A named item type is an ItemType identified by an expanded QName.
A namespace binding is a pair comprising a namespace prefix (which is either an xs:NCName or empty), and a namespace URI.
The namespace-sensitive types are xs:QName, xs:NOTATION, types derived by restriction from xs:QName or xs:NOTATION, list types that have a namespace-sensitive item type, and union types with a namespace-sensitive type in their transitive membership.
A node test that consists only of an EQName or a Wildcard is called a name test.
NaN(R) is the string used to represent the double value NaN (not a number); the default value is the string "NaN"
A node is an instance of one of the node kinds defined in [TITLE OF DM40 SPEC, TITLE OF Node SECTION]DM40.
Except where the context indicates otherwise, the term node is used as a synonym for XNode.
A node test is a condition on the name, kind (element, attribute, text, document, comment, or processing instruction), and/or type annotation of a node. A node test determines which nodes contained by an axis are selected by a step.
When an unprefixed lexical QName is expanded using the no-namespace rule, it is interpreted as having an absent namespace URI.
The non-delimiting terminal symbols are: ancestorancestor-or-selfandarrayasatattributecastcastablechildcommentdescendantdescendant-or-selfdivdocument-nodeelementelseempty-sequenceenumeqeveryexceptfnfollowingfollowing-or-selffollowing-siblingfollowing-sibling-or-selfforfunctiongeGNodegtidivifininstanceintersectisitemitemsJNodekeykeysleletltmapmembermodnamespacenamespace-nodenenodeoforotherwisepairsparentprecedingpreceding-or-selfpreceding-siblingpreceding-sibling-or-selfprocessing-instructionrecordreturnsatisfiesschema-attributeschema-elementselfsometextthentotreatunionvaluevaluesBinaryIntegerLiteralDecimalLiteralDoubleLiteralHexIntegerLiteralIntegerLiteralNCNameQNameURIQualifiedName
The type xs:numeric is defined as a union type with member types xs:double, xs:float, and xs:decimal. An item that is an instance of any of these types is referred to as a numeric value, and a type that is a subtype of xs:numeric is referred to as a numeric type.
An ordinary production rule is a production rule in A.1 EBNF that is not annotated ws:explicit.
A static or dynamic function call is a partial function application if one or more arguments is an ArgumentPlaceholder.
A partially applied function is a function created by partial function application.
A path expression consists of a series of one or more steps, separated by / or //, and optionally beginning with / or //. A path expression is typically used to locate nodes within trees.
pattern-separator(M) is a character used to separate positive and negative sub-pictures in a picture string; the default value is U+003B (SEMICOLON, ;) .
percent(M, R) is used to indicate that the number is written as a per-hundred fraction; the default value for both the marker and the rendition is U+0025 (PERCENT SIGN, %) .
per-mille(M, R) is used to indicate that the number is written as a per-thousand fraction; the default value for both the marker and the rendition is U+2030 (PER MILLE SIGN, ‰) .
The pipeline operator-> evaluates an expression and binds the result to the context value before evaluating another expression.
The predicate truth value of a value $V is the result of the expression if ($V instance of xs:numeric+) then ($V = position()) else fn:boolean($V).
Primary expressions are the basic primitives of the language. They include literals, variable references, context value references, and function calls. A primary expression may also be created by enclosing any expression in parentheses, which is sometimes helpful in controlling the precedence of operators.
Every axis has a principal node kind. If an axis can contain elements, then the principal node kind is element; otherwise, it is the kind of nodes that the axis can contain.
A pure union type is a simple type that satisfies the following constraints: (a) {variety}XS11-1 is union, (b) the {facets}XS11-1 property is empty, (c) no type in the transitive membership of the union type has {variety}XS11-1list, and (d) no type in the transitive membership of the union type is a type with {variety}XS11-1union having a non-empty {facets}XS11-1 property
To resolve a relative URI$rel against a base URI $base is to expand it to an absolute URI, as if by calling the function fn:resolve-uri($rel, $base).
The node ordering that is the reverse of document order is called reverse document order.
Two atomic items K1 and K2 have the same key value if fn:atomic-equal(K1, K2) returns true, as specified in Section 14.2.1 fn:atomic-equalFO
A schema type is a complex type or simple type as defined in the [XML Schema 1.0] or [XML Schema 1.1] specifications, including built-in types as well as user-defined types.
A sequence is an ordered collection of zero or more items.
The sequence arrow operator=> applies a function to a supplied sequence.
The sequence concatenation of a number of sequences S1, S2, ... Sn is defined to be the sequence formed from the items of S1, followed by the items from S2, and so on, retaining order.
A sequence type is a type that can be expressed using the SequenceType syntax. Sequence types are used whenever it is necessary to refer to a type in an XPath 4.0 expression. Since all values are sequences, every value matches one or more sequence types.
A sequence type designator is a syntactic construct conforming to the grammar rule SequenceType. A sequence type designator is said to designate a sequence type.
SequenceType matching compares a value with an expected sequence type.
Serialization is the process of converting an XDM instance to a sequence of octets (step DM4 in Figure 1.), as described in [XSLT and XQuery Serialization 4.0].
A sequence containing exactly one item is called a singleton.
An enumeration type with a single enumerated value (such as enum("red")) is an anonymous atomic type derived from xs:string by restriction using an enumeration facet that permits only the value "red". This is referred to as a singleton enumeration type.
A singleton focus is a fixed focus in which the context value is a singleton item.
Document order is stable, which means that the relative order of two nodes will not change during the processing of a given expression, even if this order is implementation-dependent.
Statically known collations. This is an implementation-defined mapping from URI to collation. It defines the names of the collations that are available for use in processing expressions.
Statically known decimal formats. This is a mapping from QNames to decimal formats, with one default format that has no visible name, referred to as the unnamed decimal format. Each format is available for use when formatting numbers using the fn:format-number function.
Statically known function definitions. This is a set of function definitions.
Statically known namespaces. This is a mapping from prefix to namespace URI that defines all the namespaces that are known during static processing of a given expression.
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).
Static Base URI. This is an absolute URI, used to resolve relative URIs during static analysis.
The static context of an expression is the information that is available during static analysis of the expression, prior to its evaluation.
An error that can be detected during the static analysis phase, and is not a type error, is a static error.
A static function call consists of an EQName followed by a parenthesized list of zero or more arguments.
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.
A step is a part of a path expression that generates a sequence of items and then filters the sequence by zero or more predicates. The value of the step consists of those items that satisfy the predicates, working from left to right. A step may be either an axis step or a postfix expression.
The string value of a node is a string and can be extracted by applying the Section 2.1.3 fn:stringFO function to the node.
Two sequence types are deemed to be substantively disjoint if (a) neither is a subtype of the other (see 3.3.1 Subtypes of Sequence Types) and (b) the only values that are instances of both types are one or more of the following:
The empty sequence, ().
The empty mapDM, {}.
The empty arrayDM, [].
Substitution groups are defined in Section 2.2.2.2 Element Substitution Group XS1-1 and Section 2.2.2.2 Element Substitution Group XS11-1. Informally, the substitution group headed by a given element (called the head element) consists of the set of elements that can be substituted for the head element without affecting the outcome of schema validation.
Given two sequence types or item types, the rules in this section determine if one is a subtype of the other. If a type A is a subtype of type B, it follows that every value matched by A is also matched by B.
The use of a value that has a dynamic type that is a subtype of the expected type is known as subtype substitution.
Each rule in the grammar defines one symbol, using the following format:
symbol ::= expression
Whitespace and Comments function as symbol separators. For the most part, they are not mentioned in the grammar, and may occur between any two terminal symbols mentioned in the grammar, except where that is forbidden by the /* ws: explicit */ annotation in the EBNF, or by the /* xgc: xml-version */ annotation.
System functions include the functions defined in [XQuery and XPath Functions and Operators 4.0], functions defined by the specifications of a host language, constructor functions for atomic types, and any additional functions provided by the implementation. System functions are sometimes called built-in functions.
A terminal is a symbol or string or pattern that can appear in the right-hand side of a rule, but never appears on the left-hand side in the main grammar, although it may appear on the left-hand side of a rule in the grammar for terminals.
Each element node and attribute node in an XDM instance has a type annotation (described in Section 4.1 Schema InformationDM). The type annotation of a node is a reference to a schema type.
The typed value of a node is a sequence of atomic items and can be extracted by applying the Section 2.1.4 fn:dataFO function to the node.
A type error may be raised during the static analysis phase or the dynamic evaluation phase. During the static analysis phase, a type error occurs when the static type of an expression does not match the expected type of the context in which the expression occurs. During the dynamic evaluation phase, a type error occurs when the dynamic type of a value does not match the expected type of the context in which the value occurs.
Within this specification, the term URI refers to a Universal Resource Identifier as defined in [RFC3986] and extended in [RFC3987] with the new name IRI.
In the data model, a value is always a sequence.
A variable reference is an EQName preceded by a $-sign.
A variable terminal is an instance of a production rule that is not itself an ordinary production rule but that is named (directly) on the right-hand side of an ordinary production rule.
Variable values. This is a mapping from expanded QNames to values. It contains the same expanded QNames as the in-scope variables in the static context for the expression. The expanded QName is the name of the variable and the value is the dynamic value of the variable, which includes its dynamic type.
In addition to static errors, dynamic errors, and type errors, an XPath 4.0 implementation may raise warnings, either during the static analysis phase or the dynamic evaluation phase. The circumstances in which warnings are raised, and the ways in which warnings are handled, are implementation-defined.
A whitespace character is any of the characters defined by [http://www.w3.org/TR/REC-xml/#NT-S].
In these rules, if MU and NU are NameTestUnions, then MUwildcard-matchesNU is true if every name that matches MU also matches NU.
The term XDM instance is used, synonymously with the term value, to denote an unconstrained sequence of items.
An XNode is an instance of one of the node kinds defined in Section 7.1 XML NodesDM.
XPath 1.0 compatibility mode.This value is true if rules for backward compatibility with XPath Version 1.0 are in effect; otherwise it is false.
xs:anyAtomicType is an atomic type that includes all atomic items (and no values that are not atomic). Its base type is xs:anySimpleType from which all simple types, including atomic, list, and union types, are derived. All primitive atomic types, such as xs:decimal and xs:string, have xs:anyAtomicType as their base type.
xs:dayTimeDuration is derived by restriction from xs:duration. The lexical representation of xs:dayTimeDuration is restricted to contain only day, hour, minute, and second components.
xs:error is a simple type with no value space. It is defined in Section 3.16.7.3 xs:error XS11-1 and can be used in the 3.1 Sequence Types to raise errors.
xs:untyped is used as the type annotation of an element node that has not been validated, or has been validated in skip mode.
xs:untypedAtomic is an atomic type that is used to denote untyped atomic data, such as text that has not been assigned a more specific type.
xs:yearMonthDuration is derived by restriction from xs:duration. The lexical representation of xs:yearMonthDuration is restricted to contain only year and month components.
zero-digit(M) is the character used in the picture string to represent the digit zero; the default value is U+0030 (DIGIT ZERO, 0) . This character must be a digit (category Nd in the Unicode property database), and it must have the numeric value zero. This property implicitly defines the ten Unicode characters that are used to represent the values 0 to 9 in the function output: Unicode is organized so that each set of decimal digits forms a contiguous block of characters in numerical sequence. Within the picture string any of these ten character can be used (interchangeably) as a place-holder for a mandatory digit. Within the final result string, these ten characters are used to represent the digits zero to nine.
Use the arrows to browse significant changes since the 3.1 version of this specification.
See 1 Introduction
Sections with significant changes are marked Δ in the table of contents.
See 1 Introduction
Setting the default namespace for elements and types to the special value ##any causes an unprefixed element name to act as a wildcard, matching by local name regardless of namespace.
The terms FunctionType, ArrayType, MapType, and RecordType replace FunctionTest, ArrayTest, MapTest, and RecordTest, with no change in meaning.
Record types are added as a new kind of ItemType, constraining the value space of maps.
Function coercion now allows a function with arity N to be supplied where a function of arity greater than N is expected. For example this allows the function true#0 to be supplied where a predicate function is required.
PR 1817 1853
An inline function may be annotated as a %method, giving it access to its containing map.
See 4.5.6 Inline Function Expressions
See 4.5.6.1 Methods
The symbols × and ÷ can be used for multiplication and division.
The rules for value comparisons when comparing values of different types (for example, decimal and double) have changed to be transitive. A decimal value is no longer converted to double, instead the double is converted to a decimal without loss of precision. This may affect compatibility in edge cases involving comparison of values that are numerically very close.
Operators such as < and > can use the full-width forms < and > to avoid the need for XML escaping.
The lookup operator ? can now be followed by a string literal, for cases where map keys are strings other than NCNames. It can also be followed by a variable reference.
PR 1864 1877
The key specifier can reference an item type or sequence type, to select values of that type only. This is especially useful when processing trees of maps and arrays, as encountered when processing JSON input.
PR 1763 1830
The syntax on the right-hand side of an arrow operator has been relaxed; a dynamic function call no longer needs to start with a variable reference or a parenthesized expression, it can also be (for example) an inline function expression or a map or array constructor.
The arrow operator => is now complemented by a “mapping arrow” operator =!> which applies the supplied function to each item in the input sequence independently.
PR 1023 1128
It has been clarified that function coercion applies even when the supplied function item matches the required function type. This is to ensure that arguments supplied when calling the function are checked against the signature of the required function type, which might be stricter than the signature of the supplied function item.
A dynamic function call can now be applied to a sequence of functions, and in particular to an empty sequence. This makes it easier to chain a sequence of calls.
The syntax document-node(N), where N is a NameTestUnion, is introduced as an abbreviation for document-node(element(N)). For example, document-node(*) matches any well-formed XML document (as distinct from a document fragment).
See 3.2.7 Node Types
QName literals are new in 4.0.
PR 28
Multiple for and let clauses can be combined in an expression without an intervening return keyword.
PR 159
Keyword arguments are allowed on static function calls, as well as positional arguments.
PR 202
The presentation of the rules for the subtype relationship between sequence types and item types has been substantially rewritten to improve clarity; no change to the semantics is intended.
PR 230
The rules for “errors and optimization” have been tightened up to disallow many cases of optimizations that alter error behavior. In particular there are restrictions on reordering the operands of and and or, and of predicates in filter expressions, in a way that might allow the processor to raise dynamic errors that the author intended to prevent.
PR 254
The term "function conversion rules" used in 3.1 has been replaced by the term "coercion rules".
The coercion rules allow “relabeling” of a supplied atomic item where the required type is a derived atomic type: for example, it is now permitted to supply the value 3 when calling a function that expects an instance of xs:positiveInteger.
PR 284
Alternative syntax for conditional expressions is available: if (condition) { X }.
PR 286
Element and attribute tests can include alternative names: element(chapter|section), attribute(role|class).
See 3.2.7 Node Types
The NodeTest in an AxisStep now allows alternatives: ancestor::(section|appendix)
See 3.2.7 Node Types
Element and attribute tests of the form element(N) and attribute(N) now allow N to be any NameTest, including a wildcard.
PR 324
String templates provide a new way of constructing strings: for example `{$greeting}, {$planet}!` is equivalent to $greeting || ', ' || $planet || '!'
PR 326
Support for higher-order functions is now a mandatory feature (in 3.1 it was optional).
See 5 Conformance
PR 344
A for member clause is added to FLWOR expressions to allow iteration over an array.
PR 368
The concept of the context item has been generalized, so it is now a context value. That is, it is no longer constrained to be a single item.
PR 433
Numeric literals can now be written in hexadecimal or binary notation; and underscores can be included for readability.
PR 519
The rules for tokenization have been largely rewritten. In some cases the revised specification may affect edge cases that were handled in different ways by different 3.1 processors, which could lead to incompatible behavior.
PR 521
New abbreviated syntax is introduced (focus function) for simple inline functions taking a single argument. An example is fn { ../@code }
PR 603
The rules for reporting type errors during static analysis have been changed so that a processor has more freedom to report errors in respect of constructs that are evidently wrong, such as @price/@value, even though dynamic evaluation is defined to return an empty sequence rather than an error.
PR 606
Element and attribute tests of the form element(A|B) and attribute(A|B) are now allowed.
PR 691
Enumeration types are added as a new kind of ItemType, constraining the value space of strings.
PR 728
The syntax record(*) is allowed; it matches any map.
PR 815
The coercion rules now allow conversion in either direction between xs:hexBinary and xs:base64Binary.
PR 837
A deep lookup operator ?? is provided for searching trees of maps and arrays.
PR 911
The coercion rules now allow any numeric type to be implicitly converted to any other, for example an xs:double is accepted where the required type is xs:decimal.
PR 996
The value of a predicate in a filter expression can now be a sequence of integers.
PR 1031
An otherwise operator is introduced: A otherwise B returns the value of A, unless it is an empty sequence, in which case it returns the value of B.
PR 1071
In map constructors, the keyword map is now optional, so map { 0: false(), 1: true() } can now be written { 0: false(), 1: true() }, provided it is used in a context where this creates no ambiguity.
PR 1125
Lookup expressions can now take a modifier (such as keys, values, or pairs) enabling them to return structured results rather than a flattened sequence.
PR 1131
A positional variable can be defined in a for expression.
The type of a variable used in a for expression can be declared.
The type of a variable used in a let expression can be declared.
PR 1132
Choice item types (an item type allowing a set of alternative item types) are introduced.
PR 1163
Filter expressions for maps and arrays are introduced.
PR 1181
The default namespace for elements and types can be set to the value ##any, allowing unprefixed names in axis steps to match elements with a given local name in any namespace.
If the default namespace for elements and types has the special value ##any, then an unprefixed name in a NameTest acts as a wildcard, matching names in any namespace or none.
PR 1197
The keyword fn is allowed as a synonym for function in function types, to align with changes to inline function declarations.
In inline function expressions, the keyword function may be abbreviated as fn.
PR 1212
XPath 3.0 included empty-sequence and item as reserved function names, and XPath 3.1 added map and array. This was unnecessary since these names never appear followed by a left parenthesis at the start of an expression. They have therefore been removed from the list. New keywords introducing item types, such as record and enum, have not been included in the list.
PR 1217
Predicates in filter expressions for maps and arrays can now be numeric.
PR 1249
A for key/value clause is added to FLWOR expressions to allow iteration over maps.
PR 1250
Several decimal format properties, including minus sign, exponent separator, percent, and per-mille, can now be rendered as arbitrary strings rather than being confined to a single character.
PR 1265
The rules regarding the document-uri property of nodes returned by the fn:collection function have been relaxed.
PR 1344
Parts of the static context that were there purely to assist in static typing, such as the statically known documents, were no longer referenced and have therefore been dropped.
The static typing option has been dropped.
The static typing feature has been dropped.
See 5 Conformance
PR 1361
The term atomic value has been replaced by atomic item.
See 2.1.2 Values
PR 1384
If a type declaration is present, the supplied values in the input sequence are now coerced to the required type. Type declarations are now permitted in XPath as well as XQuery.
PR 1496
The context value static type, which was there purely to assist in static typing, has been dropped.
PR 1498
The EBNF operators ++ and ** have been introduced, for more concise representation of sequences using a character such as "," as a separator. The notation is borrowed from Invisible XML.
See 2.1 Terminology
The EBNF notation has been extended to allow the constructs (A ++ ",") (one or more occurrences of A, comma-separated, and (A ** ",") (zero or more occurrences of A, comma-separated.
The EBNF operators ++ and ** have been introduced, for more concise representation of sequences using a character such as "," as a separator. The notation is borrowed from Invisible XML.
See A.1 EBNF
See A.1.1 Notation
PR 1501
The coercion rules now apply recursively to the members of an array and the entries in a map.
PR 1532
Four new axes have been defined: preceding-or-self, preceding-sibling-or-self, following-or-self, and following-sibling-or-self.
See 4.6.4.1 Axes
PR 1577
The syntax record() is allowed; the only thing it matches is an empty map.
PR 1686
With the pipeline operator ->, the result of an expression can be bound to the context value before evaluating another expression.
PR 1696
Parameter names may be included in a function signature; they are purely documentary.
PR 1703
Ordered maps are introduced.
See 4.13.1 Maps
The order of key-value pairs in the map constructor is now retained in the constructed map.
PR 1874
The coercion rules now reorder the entries in a map when the required type is a record type.
PR 1898
The rules for subtyping of document node types have been refined.
PR 1991
Named record types used in the signatures of built-in functions are now available as standard in the static context.
PR 2026
The module feature is no longer an optional feature; processing of library modules is now required.
See 5 Conformance
PR 2031
The terms XNode and JNode are introduced; the existing term node remains in use as a synonym for XNode where the context does not specify otherwise.
See 2.1.2 Values
JNodes are introduced
See 3.2.9 Generalized Node Types
The "?" operator can now be followed by an axis step, such as child::* or descendant::*. This returns a sequence of JNodes, which allow access to the key used for selection, and to parents and ancestors in the selection tree; it also avoids flattening the result sequence.
PR 2055
Sequences, arrays, and maps can be destructured in a let expression to extract their components into multiple variables.