View Old View New View Both View Only Previous Next

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

W3C

XPath and XQuery Functions and Operators 4.0

W3C Editor's Draft 23 February 2026

This version:
https://qt4cg.org/specifications/xpath-functions-40/
Latest version of XPath and XQuery Functions and Operators 4.0:
https://qt4cg.org/specifications/xpath-functions-40/
Most recent Recommendation of XPath and XQuery Functions and Operators:
https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/
Editor:
Michael Kay, Saxonica <http://www.saxonica.com/>

This document is also available in these non-normative formats: Specification in XML format and XML function catalog.


Abstract

This document defines constructor functions, operators, and functions on the datatypes defined in [XML Schema Part 2: Datatypes Second Edition] and the datatypes defined in [XQuery and XPath Data Model (XDM) 4.0]. It also defines functions and operators on nodes and node sequences as defined in the [XQuery and XPath Data Model (XDM) 4.0]. These functions and operators are defined for use in [XML Path Language (XPath) 4.0] and [XQuery 4.0: An XML Query Language] and [XSL Transformations (XSLT) Version 4.0] and other related XML standards. The signatures and summaries of functions defined in this document are available at: http://www.w3.org/2005/xpath-functions/.

A summary of changes since version 3.1 is provided at H Changes since 3.1.

Status of this Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document.

This document is a working draft developed and maintained by a W3C Community Group, the XQuery and XSLT Extensions Community Group unofficially known as QT4CG (where "QT" denotes Query and Transformation). This draft is work in progress and should not be considered either stable or complete. Standard W3C copyright and patent conditions apply.

The community group welcomes comments on the specification. Comments are best submitted as issues on the group's GitHub repository.

The community group maintains two extensive test suites, one oriented to XQuery and XPath, the other to XSLT. These can be found at qt4tests and xslt40-test respectively. New tests, or suggestions for correcting existing tests, are welcome. The test suites include extensive metadata describing the conditions for applicability of each test case as well as the expected results. They do not include any test drivers for executing the tests: each implementation is expected to provide its own test driver.

Dedication

The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).


15 Processing arrays

Arrays were introduced as a new datatype in XDM 3.1. This section describes functions that operate on arrays.

An array is an additional kind of item. An array of size N is a mapping from the integers (1 to N) to a set of values, called the members of the array, each of which is an arbitrary sequence. Because an array is an item, and therefore a sequence, arrays can be nested.

An array acts as a function from integer positions to associated values, so the function call $array($index) can be used to retrieve the array member at a given position. The function corresponding to the array has the signature function($index as xs:integer) as item()*. The fact that an array is a function item allows it to be passed as an argument to higher-order functions that expect a function item as one of their arguments.

15.2 Functions that operate on arrays

The functions defined in this section use a conventional namespace prefix array, which is assumed to be bound to the namespace URI http://www.w3.org/2005/xpath-functions/array.

As with all other values, arrays are treated as immutable. For example, the array:reverse function returns an array that differs from the supplied array in the order of its members, but the supplied array is not changed by the operation. Two calls on array:reverse with the same argument will return arrays that are indistinguishable from each other; there is no way of asking whether these are “the same array”. Like sequences, arrays have no identity.

All functionality on arrays is defined in terms of two primitives:

  • The function array:members decomposes an array to a sequence of value records.

  • The function array:of-members composes an array from a sequence of value records.

A value record here is an item that encapsulates an arbitrary value; the representation chosen for a value record is record(value as item()*), that is, a map containing a single entry whose key is the string "value" and whose value is the encapsulated sequence.

Note that when the required type of an argument to a function such as array:build is an array type, then the coercion rules ensure that a JNode can be supplied in the function call: if the ·content· property of the JNode is an array, then the array is automatically extracted as if by the jnode-content function.

FunctionMeaning
array:appendReturns an array containing all the members of a supplied array, plus one additional member at the end.
array:buildReturns an array obtained by evaluating the supplied function once for each item in the input sequence.
array:emptyReturns true if the supplied array contains no members.
array:filterReturns an array containing those members of the $array for which $predicate returns true. A return value of () is treated as false.
array:flattenReplaces any array appearing in a supplied sequence with the members of the array, recursively.
array:fold-leftEvaluates the supplied function cumulatively on successive members of the supplied array.
array:fold-rightEvaluates the supplied function cumulatively on successive values of the supplied array.
array:footReturns the last member of an array.
array:for-eachReturns an array whose size is the same as array:size($array), in which each member is computed by applying $action to the corresponding member of $array.
array:for-each-pairReturns an array obtained by evaluating the supplied function once for each pair of members at the same position in the two supplied arrays.
array:getReturns the value at the specified position in the supplied array (counting from 1).
array:headReturns the first member of an array, that is $array(1).
array:index-ofReturns a sequence of positive integers giving the positions within the array $array of members that are equal to $target.
array:index-whereReturns the positions in an input array of members that match a supplied predicate.
array:insert-beforeReturns an array containing all the members of the supplied array, with one additional member at a specified position.
array:itemsReturns the sequence concatenation of the members of an array.
array:joinConcatenates the contents of several arrays into a single array, with an optional separator between adjacent members.
array:membersDelivers the contents of an array as a sequence of value records.
array:of-membersConstructs an array from the contents of a sequence of value records.
array:putReturns an array containing all the members of a supplied array, except for one member which is replaced with a new value.
array:removeReturns an array containing all the members of the supplied array, except for the members at specified positions.
array:reverseReturns an array containing all the members of a supplied array, but in reverse order.
array:sizeReturns the number of members in the supplied array.
array:sliceReturns an array containing selected members of a supplied input array based on their position.
array:sortSorts a supplied array, based on the value of a sort key supplied as a function.
array:sort-bySorts a supplied array, based on the value of a number of sort keys supplied as functions.
array:sort-withSorts a supplied array, according to the order induced by the supplied comparator functions.
array:splitDelivers the contents of an array as a sequence of single-member arrays.
array:subarrayReturns an array containing all members from a supplied array starting at a supplied position, up to a specified length.
array:tailReturns an array containing all members except the first from a supplied array.
array:trunkReturns an array containing all members except the last from a supplied array.

15.2.9 array:for-each

Changes in 4.0  

  1. The $action callback function now accepts an optional position argument.  [Issue 516 PR 828 14 November 2023]

Summary

Returns an array whose size is the same as array:size($array), in which each member is computed by applying $action to the corresponding member of $array.

Signature
array:for-each(
$arrayas array(*),
$actionas fn(item()*, xs:integer) as item()*
) as array(*)
Properties

This function is deterministic, context-independent, and focus-independent.

Rules

Informally, the function returns an array whose members are obtained by applying the supplied $action function to each member of the input array in turn.

The $action function is called with two arguments: the first is the array member (which in general is an arbitrary sequence), and the second is the 1-based integer position.

Formal Equivalent

The effect of the function is equivalent to the result of the following XPath expression.

array:of-members(
  for-each(array:members($array), 
  fn($member, $pos) {
    { 'value': $action($member, $pos) }
  })
)
array:of-members(
  for-each(array:members($array), 
           fn($member, $pos) {
              { 'value': $action(map:get($member, 'value'), $pos) }
           })
)
Examples
Expression:
array:for-each(
  [ "A", "B", 1, 2 ],
  fn($z) { $z instance of xs:integer }
)
Result:
[false(), false(), true(), true()]
Expression:
array:for-each(
  [ "the cat", "sat", "on the mat" ],
  tokenize#1
)
Result:
[ ("the", "cat"), "sat", ("on", "the", "mat") ]
Expression:
array:for-each(
  [ [ "the", "cat" ], [ "sat" ], [ "on", "the", "mat" ] ],
  array:flatten#1
)
Result:
[ ("the", "cat"), "sat", ("on", "the", "mat") ]
Expression:
array:for-each(
  [ 'one', 'two', 'three' ],
  fn($member, $pos) { $pos || '. ' || $member }
)
Result:
[ "1. one", "2. two", "3. three" ]