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: Specification in XML format and XML function catalog.
Copyright © 2000 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and document use rules apply.
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) 3.1]. It also defines functions and operators on nodes and node sequences as defined in the [XQuery and XPath Data Model (XDM) 3.1]. 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 G Changes since version 3.1.
This version of the specification is work in progress. It is produced by the QT4 Working Group, officially the W3C XSLT 4.0 Extensions Community Group. Individual functions specified in the document may be at different stages of review, reflected in their History notes. Comments are invited, in the form of GitHub issues at https://github.com/qt4cg/qtspecs.
The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).
The following functions take function items as an argument.
| Function | Meaning |
|---|---|
fn:apply | Makes a dynamic call on a function with an argument list supplied in the form of an array. |
fn:chain | Applies a sequence of functions starting with an initial input. |
fn:do-until | Processes a supplied value repeatedly, continuing when some condition is false, and returning the value that satisfies the condition. |
fn:every | Returns true if every item in the input sequence matches a supplied predicate. |
fn:filter | Returns those items from the sequence $input for which the supplied function $predicate returns true. |
fn:fold-left | Processes the supplied sequence from left to right, applying the supplied function repeatedly to each item in turn, together with an accumulated result value. |
fn:fold-right | Processes the supplied sequence from right to left, applying the supplied function repeatedly to each item in turn, together with an accumulated result value. |
fn:for-each | Applies the function item $action to every item from the sequence $input in turn, returning the concatenation of the resulting sequences in order. |
fn:for-each-pair | Applies the function item $action to successive pairs of items taken one from $input1 and one from $input2, returning the concatenation of the resulting sequences in order. |
fn:highest | Returns those items from a supplied sequence that have the highest value of a sort key, where the sort key can be computed using a caller-supplied function. |
fn:index-where | Returns the positions in an input sequence of items that match a supplied predicate. |
fn:lowest | Returns those items from a supplied sequence that have the lowest value of a sort key, where the sort key can be computed using a caller-supplied function. |
fn:partial-apply | Performs partial application of a function item by binding values to selected arguments. |
fn:partition | Partitions a sequence of items into a sequence of non-empty arrays containing the same items, starting a new partition when a supplied condition is true. |
fn:scan-left | Produces the complete (ordered) sequence of all partial results from every new value the accumulator is assigned to during the evaluation of fn:fold-left. |
fn:scan-right | Produces the complete (ordered) sequence of all partial results from every new value the accumulator is assigned to during the evaluation of fn:fold-right. |
fn:some | Returns true if at least one item in the input sequence matches a supplied predicate. |
fn:sort | Sorts a supplied sequence, based on the value of a number of sort keys supplied as functions. |
fn:sort-with | Sorts a supplied sequence, according to the order induced by the supplied comparator functions. |
fn:subsequence-where | Returns a contiguous sequence of items from $input, with the start and end points located by applying predicates. |
fn:take-while | Returns items from the input sequence prior to the first one that fails to match a supplied predicate. |
fn:transitive-closure | Returns all the nodes reachable from a given start node by applying a supplied function repeatedly. |
fn:while-do | Processes a supplied value repeatedly, continuing while some condition remains true, and returning the first value that does not satisfy the condition. |
With all these functions, if the caller-supplied function fails with a dynamic error, this error is propagated as an error from the higher-order function itself.
Performs partial application of a function item by binding values to selected arguments.
fn:partial-apply( | ||
$function | as , | |
$arguments | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
The result is a function obtained by binding values to selected arguments of the function item $function. The arguments to be bound are represented by entries in the $arguments map: an entry with key $i and value $v causes the argument at position $i (1-based) to be bound to $v.
Any entries in $arguments whose keys are greater than the arity of $function are ignored.
If $arguments is an empty map then the function returns $function unchanged.
For example, the effect of calling fn:partial-apply($f, {2: $x}) is the same as the effect of the partial appplication $f(?, $x, ?, ?, ....). The coercion rules are applied to the supplied arguments in the usual way.
Unlike a partial application using place-holder arguments:
The arity of $function need not be statically known.
It is possible to bind all the arguments of $function: the effect is to return a zero-arity function.
The result is a partially applied functionXP having the following properties (which are defined in Section 2.9.4 Function ItemsDM):
name: absent.
identity: A new function identity distinct from the identity of any other function item.
Note:
See also Section 4.5.2.7 Function IdentityXP.
arity: The arity of $function minus the number of parameters in $function that map to supplied arguments in $arguments.
parameter names: The names of the parameters of $function that do not map to supplied arguments in $arguments.
signature: The parameters in the returned function are the parameters of $function that do not map to supplied arguments in $arguments, retaining order. The result type of the returned function is the same as the result type of $function.
An implementation that can determine a more specific signature (for example, through use of type analysis) is permitted to do so.
body: The body of $function.
captured context: The static and dynamic context of $function, augmented, for each supplied argument, with a binding of the converted argument value to the corresponding parameter name.
A type error is raised if any of the supplied arguments, after applying the coercion rules, does not match the required type of the corresponding function parameter.
In addition, a dynamic error may be raised if any of the supplied arguments does not match other constraints on the value of that argument (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).
See also Section 4.5.2.3 Partial Function ApplicationXP.
The function is useful where the arity of a function item is not known statically, or where all arguments in a function are to be bound, returning a zero-arity function.
| Expression: | let $f := partial-apply(dateTime#2, {2: xs:time('00:00:00')})
return $f(xs:date('2025-03-01')) |
|---|---|
| Result: | xs:dateTime('2025-03-01T00:00:00') |
Partitions a sequence of items into a sequence of non-empty arrays containing the same items, starting a new partition when a supplied condition is true.
fn:partition( | ||
$input | as , | |
$split-when | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
Informally, the function starts by creating a partition containing the first item in the input sequence, if any. For each remaining item J in the input sequence, other than the first, it calls the supplied $split-when function with three arguments: the contents of the current partition, the item J, and the current position in the input sequence.
Each partition is a sequence of items; the function result wraps each partition as an array, and returns the sequence of arrays.
If the $split-when function returns true, the current partition is wrapped as an array and added to the result, and a new current partition is created, initially containing the item J only. If the $split-when function returns false or (), the item J is added to the current partition.
The effect of the function is equivalent to the result of the following XPath expression.
fold-left($input, (), fn($partitions, $next, $pos) {
if (empty($partitions) or $split-when(foot($partitions)?*, $next, $pos))
then ($partitions, [ $next ])
else (trunk($partitions), array { foot($partitions)?*, $next })
})The function enables a variety of positional grouping problems to be solved. For example:
partition($input, fn($a, $b) { count($a) eq 3 } partitions a sequence into fixed size groups of length 3.
partition($input, fn($a, $b) { boolean($b/self::h1) } starts a new group whenever an h1 element is encountered.
partition($input, fn($a, $b) { $b lt foot($a) } starts a new group whenever an item is encountered whose value is less than the value of the previous item.
The callback function is not called to process the first item in the input sequence, because this will always start a new partition. The first argument to the callback function (the current partition) is always a non-empty sequence.
| Expression: | partition(
("Anita", "Anne", "Barbara", "Catherine", "Christine"),
fn($partition, $next) {
substring(head($partition), 1, 1) ne substring($next, 1, 1)
}
) |
|---|---|
| Result: | [ "Anita", "Anne" ], [ "Barbara" ], [ "Catherine", "Christine" ] |
| Expression: | partition(
(1, 2, 3, 4, 5, 6, 7),
fn($partition, $next) {
count($partition) eq 2
}
) |
| Result: | [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] |
| Expression: | partition(
(1, 4, 6, 3, 1, 1),
fn($partition, $next) {
sum($partition) ge 5
}
) |
| Result: | [ 1, 4 ], [ 6 ], [ 3, 1, 1 ] |
| Expression: | partition(
tokenize("In the beginning was the word"),
fn($partition, $next) {
sum(($partition, $next) ! string-length()) gt 10
}
) |
| Result: | [ "In", "the" ], [ "beginning" ], [ "was", "the", "word" ] |
| Expression: | partition(
(1, 2, 3, 6, 7, 9, 10),
fn($partition, $next) { $next != foot($partition) + 1 }
) |
| Result: | [ 1, 2, 3 ], [ 6, 7 ], [ 9, 10 ] |
| Expression: | partition(
('a', 'b', 'c', 'd', 'e'),
fn($all, $next, $p) { $p mod 2 = 1 }
) |
| Result: | [ "a", "b" ], [ "c", "d" ], [ "e" ] |
Produces the complete (ordered) sequence of all partial results from every new value the accumulator is assigned to during the evaluation of fn:fold-left.
fn:scan-left( | ||
$input | as , | |
$zero | as , | |
$action | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
The function is equivalent to the following implementation in XPath (return clause added in comments for completeness):
let $scan-left-inner := fn(
$input as item()*,
$zero as item()*,
$action as fn(item()*, item()) as item()*,
$self as fn(*)
) as array(*)* {
let $result := [$zero]
return if (empty($input)) then (
$result
) else (
$result, $self(tail($input), $action($zero, head($input)), $action, $self)
)
}
let $scan-left := fn(
$input as item()*,
$zero as item()*,
$action as fn(item()*, item()) as item()*
) as array(*)* {
$scan-left-inner($input, $zero, $action, $scan-left-inner)
}
(: return $scan-left(1 to 10, 0, op('+')) :)As a consequence of the function signature and the function calling rules, a type error occurs if the supplied function $action cannot be applied to two arguments, where the first argument is either the value of $zero or the result of a previous application of $action, and the second is any single item from the sequence $input.
Note that each intermediate result is placed in a separate single-member arrayDM. This is necessary because we cannot represent a sequence of results, some or all of which are a sequence - that is "sequence of sequences" as just a single sequence.
| Expression: | scan-left(1 to 5, 0, op('+')) |
|---|---|
| Result: | [ 0 ], [ 1 ], [ 3 ], [ 6 ], [ 10 ], [ 15 ] |
| Expression: | scan-left(1 to 3, 0, op('-')) |
| Result: | [ 0 ], [ -1 ], [ -3 ], [ -6 ] |
Produce the intermediate results of mapping each number in a sequence to its doubled value. This example shows the necessity to place each intermediate result (sequence) into a single-member arrayDM - otherwise the sequence of sequences (intermediate results) would not be possible to express as a single sequence without losing completely the intermediate results. | |
| Expression: | let $double := fn($x) { 2 * $x }
return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) }) |
| Result: | [ () ], [ 2 ], [ (2, 4) ], [ (2, 4, 6) ] ] |
Produce the factorials of all numbers from 0 to 5 | |
| Expression: | scan-left(1 to 5, 1, op('*')) |
| Result: | [ 1 ], [ 1 ], [ 2 ], [ 6 ], [ 24 ], [ 120 ] |
Produces the complete (ordered) sequence of all partial results from every new value the accumulator is assigned to during the evaluation of fn:fold-right.
fn:scan-right( | ||
$input | as , | |
$zero | as , | |
$action | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
The function is equivalent to the following implementation in XPath (return clause in comments added for completeness):
let $scan-right-inner := fn(
$input as item()*,
$zero as item()*,
$action as fn(item()*, item()) as item()*, $self as fn(*)
) as array(*)* {
if (empty($input)) then (
[ $zero ]
) else (
let $rightResult := $self(tail($input), $zero, $action, $self)
return ([ $action(head($input), head($rightResult)) ], $rightResult)
)
}
let $scan-right := function(
$input as item()*,
$zero as item()*,
$f as fn(item()*, item()) as item()*
) as array(*)* {
$scan-right-inner($input, $zero, $f, $scan-right-inner)
}
(: return $scan-right(1 to 10, 0, op('+')) :)As a consequence of the function signature and the function calling rules, a type error occurs if the supplied function $action cannot be applied to two arguments, where the first argument is any item in the sequence $input, and the second is either the value of $zero or the result of a previous application of $action.
Note that each intermediate result is placed in a separate single-member arrayDM. This is necessary because we cannot represent a sequence of results, some or all of which are a sequence - that is "sequence of sequences" as just a single sequence.
| Expression: | scan-right(1 to 10, 0, op('+')) |
|---|---|
| Result: | [ 55 ], [ 54 ], [ 52 ], [ 49 ], [ 45 ], [ 40 ], [ 34 ], [ 27 ], [ 19 ], [ 10 ], [ 0 ] |
| Expression: | scan-right(1 to 3, 0, op('-')) |
| Result: | [ 2 ], [ -1 ], [ 3 ], [ 0 ] |
Returns true if at least one item in the input sequence matches a supplied predicate.
fn:some( | ||
$input | as , | |
$predicate | as | := fn:boolean#1 |
) as | ||
This function is deterministic, context-independent, and focus-independent.
The function returns true if (and only if) there is an item $item at position $pos in the input sequence such that $predicate($item, $pos) returns true.
The effect of the function is equivalent to the result of the following XPath expression.
fold-left($input, false(), fn($result, $item, $pos) {
$result or $predicate($item, $pos)
})An error is raised if the $predicate function raises an error. In particular, when the default predicate fn:boolean#1 is used, an error is raised if an item has no effective boolean value.
If the second argument is omitted or an empty sequence, the predicate defaults to fn:boolean#1, which takes the effective boolean value of each item.
It is possible for the supplied $predicate to be a function whose arity is less than two. The coercion rules mean that the additional parameters are effectively ignored. Frequently a predicate function will only consider the item itself, and disregard its position in the sequence.
The predicate is required to return either true, false, or an empty sequence (which is treated as false). A predicate such as fn{self::h1} results in a type error because it returns a node, not a boolean.
The implementation may deliver a result as soon as one item is found for which the predicate returns true; it is not required to evaluate the predicate for every item, nor is it required to examine items sequentially from left to right.
| Expression: |
|
|---|---|
| Result: | false() |
| Expression: |
|
| Result: | true() |
| Expression: |
|
| Result: | false() |
| Expression: |
|
| Result: | true() |
| Expression: |
|
| Result: | true() |
| Expression: | some(
("January", "February", "March", "April",
"September", "October", "November", "December"),
contains(?, "z")
) |
| Result: | false() |
| Expression: | some(
("January", "February", "March", "April",
"September", "October", "November", "December")
=!> contains("r")
) |
| Result: | true() |
| Expression: |
|
| Result: | false() (The effective boolean value in each case is false.) |
| Expression: |
|
| Result: | true() |
Sorts a supplied sequence, based on the value of a number of sort keys supplied as functions.
fn:sort( | ||
$input | as , | |
$collations | as | := fn:default-collation(), |
$keys | as | := fn:data#1, |
$orders | as | := 'ascending' |
) as | ||
This function is deterministic, context-dependent, and focus-independent. It depends on collations.
The result of the function is a sequence that contains all the items from $input, typically in a different order, the order being defined by the supplied sort key definitions.
A sort key definition has three parts:
A sort key function, which is applied to each item in the input sequence to determine a sort key value.
A collation, which is used when comparing sort key values that are of type xs:string or xs:untypedAtomic.
An order direction, which is ascending or descending.
The number of sort key definitions is determined by the number of function items supplied in the $keys argument. If the argument is absent or empty, the default is a single sort key definition using the function data#1.
The $nth sort key definition (where $n counts from one (1)) is established as follows:
The sort key function is $keys[$n] otherwise data#1.
The collation is $collations[$n] otherwise $collations[last()] otherwise default-collation(). That is, it is the collation supplied in the corresponding item of the supplied $collations argument; or in its absence, the last entry in $collations; or if $collations is absent or empty, the default collation from the static context of the caller.
The order direction is $orders[$n] otherwise $orders[last()] otherwise "ascending". That is, it is "ascending" or "descending" according to the value of the corresponding item in the supplied $orders argument; or in its absence, the last entry in $orders; or if $orders is absent or empty, then "ascending".
When comparing values of types other than xs:string or xs:untypedAtomic, the corresponding collation is ignored, and no error is reported if the supplied value is not a known or valid collation name. If it is necessary to supply such an ignored value (for example, in the case where a non-string sort key is followed by another sort key that requires a collation) the empty string can be supplied.
The result of the function is obtained as follows:
The result sequence contains the same items as the input sequence $input, but generally in a different order.
The sort key definitions are established as described above. The sort key definitions are in major-to-minor order. That is, the position of two items $A and $B in the result sequence is determined first by the relative magnitude of their primary sort key values, which are computed by evaluating the sort key function in the first sort key definition. If those two sort key values are equal, then the position is determined by the relative magnitude of their secondary sort key values, computed by evaluating the sort key function in the second sort key definition, and so on.
When a pair of corresponding sort key values of $A and $B are found to be not equal, then $A precedes $B in the result sequence if both the following conditions are true, or if both conditions are false:
The sort key value for $A is less than the sort key value for $B, as defined below.
The order direction in the corresponding sort key definition is "ascending".
If all the sort key values for $A and $B are pairwise equal, then $A precedes $B in the result sequence if and only if $A precedes $B in the input sequence.
Note:
That is, the sort is stable.
Each sort key value for a given item is obtained by applying the sort key function of the corresponding sort key definition to that item. The result of this function is in the general case a sequence of atomic items. Two sort key values $a and $b are compared as follows:
Let $C be the collation in the corresponding sort key definition.
Let $REL be the result of evaluating op:lexicographic-compare($key($A), $key($B), $C) where op:lexicographic-compare($a, $b, $C) is defined as follows:
if (empty($a) and empty($b)) then 0
else if (empty($a)) then -1
else if (empty($b)) then +1
else let $rel = op:simple-compare(head($a), head($b), $C)
return if ($rel eq 0)
then op:lexicographic-compare(tail($a), tail($b), $C)
else $relHere op:simple-compare($k1, $k2) is defined as follows:
if ($k1 instance of (xs:string | xs:anyURI | xs:untypedAtomic)
and $k2 instance of (xs:string | xs:anyURI | xs:untypedAtomic))
then compare($k1, $k2, $C)
else if ($k1 instance of xs:numeric and $k2 instance of xs:numeric)
then compare($k1, $k2)
else if ($k1 eq $k2) then 0
else if ($k2 lt $k2) then -1
else +1Note:
This raises an error if two keys are not comparable, for example if one is a string and the other is a number, or if both belong to a non-ordered type such as xs:QName.
If $REL is zero, then the two sort key values are deemed equal; if $REL is -1 then $a is deemed less than $b, and if $REL is +1 then $a is deemed greater than $b
If the set of computed sort keys contains values that are not comparable using the lt operator then the sort operation will fail with a type error ([err:XPTY0004]XP).
XSLT and XQuery both provide native sorting capability, but earlier releases of XPath provided no sorting functionality for use in standalone environments.
In addition there are cases where this function may be more flexible than the built-in sorting capability for XQuery or XSLT, for example when the sort key or collation is chosen dynamically, or when the sort key is a sequence of items rather than a single item.
The results are compatible with the results of XSLT sorting (using xsl:sort) in the case where the sort key evaluates to a sequence of length zero or one, given the options stable="yes".
The results are compatible with the results of XQuery sorting (using the order by clause) in the case where the sort key evaluates to a sequence of length zero or one, given the options stable, ascending, and empty least.
The function has been enhanced in 4.0 to allow multiple sort keys to be defined, each potentially with a different collation, and to allow sorting in descending order.
The effect of the XQuery option empty least|greatest, which controls whether the empty sequence is sorted before or after all other values, can be achieved by adding an extra sort key definition that evaluates whether or not the actual sort key is empty (when sorting boolean values, false precedes true).
Supplying too many items in the $collations and/or $orders arguments is not an error; the excess values are ignored except for type-checking against the function signature.
| Expression: |
|
|---|---|
| Result: | 1, 3, 4, 5, 6 |
| Expression: |
|
| Result: | 6, 5, 4, 4e0, 3, 1 |
| Expression: |
|
| Result: | 1, -2, 5, 8, 10, -10, 10 |
To sort a set of strings | |
let $SWEDISH := collation({ 'lang': 'se' })
return sort($in, $SWEDISH) | |
To sort a sequence of employees by last name as the major sort key and first name as the minor sort key, using the default collation: | |
sort($employees, (), fn { name ! (last, first) }) | |
To sort a sequence of employees first by increasing last name (using Swedish collation order) and then by decreasing salary: | |
sort(
$employees,
collation({ 'lang': 'se' }),
(fn { name/last }, fn { xs:decimal(salary) }),
("ascending", "descending")
) | |
Sorts a supplied sequence, according to the order induced by the supplied comparator functions.
fn:sort-with( | ||
$input | as , | |
$comparators | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
Informally, the items of the supplied $input are compared against each other, using the supplied $comparators. The result is a sorted sequence.
Each comparator function takes two items and returns an xs:integer that defines the relationship between these items, in accordance with the fn:compare function:
The comparators are evaluated one after the other, either completely or until the result is an integer other than 0.
If the last integer returned is negative or 0, the first item is returned before the second.
Otherwise, the second item is returned first.
Users are responsible for supplying transitive comparators; otherwise, the result might not be correctly sorted. An example for a non-transitive and thus unsuitable comparator is fn($a, $b) { if ($a mod 2 = 1) then 1 else -1 }, as it considers odd numbers to be greater than even numbers.
Sorting is stable, which means that the relative order of the input items is maintained.
More formally, assuming that the comparators raise no errors and are transitive, the effect of the function is equivalent to the following implementation in XQuery:
declare function sort-with(
$input as item()*,
$comparators as (fn(item(), item()) as xs:integer)*
) as item()* {
if (count($input) < 2) then (
$input
) else (
let $head := head($input), $sorted-tail := sort-with(tail($input), $comparators)
let $diff := fold-left($comparators, 0, fn($df, $cmp) {
if ($df != 0) then $df else $cmp($head, head($sorted-tail))
})
return if ($diff <= 0) then (
$head, $sorted-tail
) else (
head($sorted-tail), sort-with(($head, tail($sorted-tail)), $comparators)
)
)
};An implementation is free to choose any sorting algorithm as long as the result returned is stable and matches the result of the implementation in XQuery.
| Expression: |
|
|---|---|
| Result: | 1, 3, 4, 5, 6 |
| Expression: |
|
| Result: | 1, 3, 4, 5, 6 |
| Expression: |
|
| Result: | 6, 5, 4, 4e0, 3, 1 |
| Expression: | sort-with(
(1, -2, 5, 10, -12, 8),
fn($a, $b) { abs($a) - abs($b) }
) |
| Result: | 1, -2, 5, 8, 10, -12 |
| Expression: | let $persons := <persons>
<person name='Josipa' age='8'/>
<person name='Jade' age='6'/>
<person name='Jie' age='8'/>
</persons>
return sort-with($persons/person, (
fn($a, $b) { compare($a/@age, $b/@age) },
fn($a, $b) { compare($a/@name, $b/@name) }
)) |
| Result: | <person name="Jade" age="6"/> <person name="Jie" age="8"/> <person name="Josipa" age="8"/> |
Returns a contiguous sequence of items from $input, with the start and end points located by applying predicates.
fn:subsequence-where( | ||
$input | as , | |
$from | as | := true#0, |
$to | as | := false#0 |
) as | ||
This function is deterministic, context-independent, and focus-independent.
Informally, the function returns the subsequence of $input starting with the first item that matches the $from predicate, and ending with the first subsequent item that matches the $to predicate. If $from is not supplied, it defaults to the start of $input; if $to is not supplied, it defaults to the end of $input. If $from does not match any items in $input, the result is the empty sequence; if $to does not match any items, all items up to the last are included in the result.
The effect of the function is equivalent to the result of the following XPath expression.
let $start := index-where($input, $from)[1] otherwise count($input) + 1 let $end := index-where($input, $to)[. ge $start][1] otherwise count($input) + 1 return slice($input, $start, $end)
The result includes both the item that matches the $from condition and the item that matches the $to condition. To select a subsequence that starts after the $from item, apply the fn:tail function to the result. To select a subsequence that ends before the $to item, apply the fn:trunk function to the result.
The predicate functions supplied to the $from and $to parameters can include an integer position argument as well as the item itself. This position will always be 1-based, relative to the start of $input. This means it is possible to select items based on their absolute position in the $input sequence, but there is no mechanism to select an end position relative to the start position. If this is needed, the function can be combined with others: for example, to select a subsequence of four items starting with "Barbara", use $input => subsequence-where(fn {. eq "Barbara" }) => slice(end := 4).
If the requirement is to select all elements stopping before the first h2 element if it exists, or up to the end of the sequence otherwise, the simplest solution is perhaps to write:
slice($input, end:=index-where($input, fn { boolean(self::h2) })[1])A return value of () from the $from or $to predicate is treated as false.
| Variables | |
|---|---|
let $names := ("Anna", "Barbara", "Catherine", "Delia", "Eliza", "Freda",
"Gertrude", "Hilda") | |
| Expression: |
|
|---|---|
| Result: | "Eliza", "Freda", "Gertrude", "Hilda" |
| Expression: |
|
| Result: | "Anna", "Barbara", "Catherine", "Delia" |
| Expression: |
|
| Result: | "Anna", "Barbara", "Catherine" |
| Expression: |
|
| Result: | "Eliza", "Freda", "Gertrude" |
| Expression: | subsequence-where(
$names,
starts-with(?, "D"), fn { string-length(.) gt 5 }
) |
| Result: | "Delia", "Eliza", "Freda", "Gertrude" |
| Expression: |
|
| Result: | () |
| Expression: |
|
| Result: | "Gertrude", "Hilda" |
| Expression: |
|
| Result: | "Anna", "Barbara", "Catherine", "Delia", "Eliza", "Freda", "Gertrude", "Hilda" |
| Expression: | subsequence-where(
$names,
fn($it, $pos) { ends-with($it, "a") and $pos gt 5 }
) |
| Result: | "Freda", "Gertrude", "Hilda" |
| Expression: | subsequence-where(
$names,
to := fn($it, $pos) { ends-with($it, "a") and $pos ge 5 }
) |
| Result: | "Anna", "Barbara", "Catherine", "Delia", "Eliza" |
Returns items from the input sequence prior to the first one that fails to match a supplied predicate.
fn:take-while( | ||
$input | as , | |
$predicate | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
The function returns all items in the sequence prior to the first one where the result of calling the supplied $predicate function, with the current item and its position as arguments, returns the value false or ().
If every item in the sequence satisfies the predicate, then $input is returned in its entirety.
The effect of the function is equivalent to the result of the following XQuery expression.
for $item at $pos in $input while $predicate($item, $pos) return $item
There is no analogous drop-while or skip-while function, as found in some functional programming languages. The effect of drop-while($input, $predicate) can be achieved by calling fn:subsequence-where($input, fn { not($predicate(.)) }).
| Expression: |
|
|---|---|
| Result: | 10, 11, 12 |
| Expression: |
|
| Result: | 10 to 20 |
| Expression: |
|
| Result: | () |
| Expression: | take-while(
("A", "B", "C", " ", "E"),
fn { boolean(normalize-space()) }
) |
| Result: | "A", "B", "C" |
| Expression: | parse-xml("<doc><p/><p/><h2/><img/><p/></doc>")/doc/*
=> take-while(fn { boolean(self::p) })
=> count() |
| Result: | 2 |
| Expression: | ("Aardvark", "Antelope", "Bison", "Buffalo", "Camel", "Dingo")
=> take-while(starts-with(?, "A")) |
| Result: | "Aardvark", "Antelope" |
| Expression: | take-while(10 to 20, fn($num, $pos) { $num lt 18 and $pos lt 4 }) |
| Result: | 10, 11, 12 |
| Expression: | take-while(
characters("ABCD-123"),
fn($ch, $pos) { $pos lt 4 and $ch ne '-' }
) => string-join() |
| Result: | "ABC" |
| Expression: | take-while(
("A", "a", "B", "b", "C", "D", "d"),
fn($ch, $pos) {
matches($ch, if ($pos mod 2 eq 1) then "\p{Lu}" else "\p{Ll}")
}
) |
| Result: | "A", "a", "B", "b", "C" |
Returns all the nodes reachable from a given start node by applying a supplied function repeatedly.
fn:transitive-closure( | ||
$node | as , | |
$step | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
The value of $node is a node from which navigation starts. If $node is an empty sequence, the function returns an empty sequence.
The value of $step is a function that takes a single node as input, and returns a set of nodes as its result.
The result of the fn:transitive-closure function is the set of nodes that are reachable from $node by applying the $step function one or more times.
Although $step may return any sequence of nodes, the result is treated as a set: the order of nodes in the sequence is ignored, and duplicates are ignored. The result of of the transitive-closure function will always be a sequence of nodes in document order with no duplicates.
The function delivers the same result as the following XQuery implementation.
declare %private function tc-inclusive(
$nodes as node()*,
$step as fn(node()) as node()*
) as node()* {
let $nextStep := $nodes/$step(.)
let $newNodes := $nextStep except $nodes
return if (exists($newNodes))
then $nodes union tc-inclusive($newNodes, $step)
else $nodes
};
declare function transitive-closure (
$node as node(),
$step as fn(node()) as node()*
) as node()* {
tc-inclusive($node/$step(.), $step)
};
(: Explanation:
The private helper function tc-inclusive takes a set of nodes as input,
and calls the $step function on each one of those nodes; if the result
includes nodes that are not already present in the input, then it makes
a recursive call to find nodes reachable from these new nodes, and returns
the union of the supplied nodes and the nodes returned from the recursive
call (which will always include the new nodes selected in the first step).
If there are no new nodes, the recursion ends, returning the nodes that
have been found up to this point.
The main function fn:transitive-closure finds the nodes that are reachable
from the start node in a single step, and then invokes the helper function
tc-inclusive to add nodes that are reachable in multiple steps.
:)Cycles in the data are not a problem; the function stops searching when it finds no new nodes.
The function may fail to terminate if the supplied $step function constructs and returns new nodes. A processor may detect this condition but is not required to do so.
The $node node is not included in the result, unless it is reachable by applying the $step function one or more times. If a result is required that does include $node, it can be readily added to the result using the union operator: $node | transitive-closure($node, $step).
| Variables | |
|---|---|
let $data := document { <doc>
<person id="0"/>
<person id="1" manager="0"/>
<person id="2" manager="0"/>
<person id="3" manager="2"/>
<person id="4" manager="2"/>
<person id="5" manager="1"/>
<person id="6" manager="3"/>
<person id="7" manager="6"/>
<person id="8" manager="6"/>
</doc> } | |
let $direct-reports := fn($p as element(person)) as element(person)* {
$p/../person[@manager = $p/@id]
} | |
| Expression: | transitive-closure( $data//person[@id = "2"], $direct-reports )/string(@id) |
|---|---|
| Result: | "3", "4", "6", "7", "8" |
| Expression: | transitive-closure(
$data,
function { child::* }
)/@id ! string() |
| Result: | "0", "1", "2", "3", "4", "5", "6", "7", "8" |
The following example, given | |
transitive-closure($root, fn { document(//(xsl:import|xsl:include)/@href) })
=!> document-uri() | |
This example uses the XSLT-defined | |
Processes a supplied value repeatedly, continuing while some condition remains true, and returning the first value that does not satisfy the condition.
fn:while-do( | ||
$input | as , | |
$predicate | as , | |
$action | as | |
) as | ||
This function is deterministic, context-independent, and focus-independent.
Informally, the function behaves as follows:
$pos is initially set to 1.
$predicate($input, $pos) is evaluated. If the result is false or (), the function returns the value of $input.
Otherwise, $action($input, $pos) is evaluated, the resulting value is used as a new $input, and the process repeats from step 2 with $pos incremented by 1.
The function delivers the same result as the following XQuery implementation.
declare %private function while-do-helper(
$input as item()*,
$predicate as fn(item()*, xs:integer) as xs:boolean?,
$action as fn(item()*, xs:integer) as item()*,
$pos as xs:integer
) as item()* {
if ($predicate($input, $pos))
then while-do-helper($action($input, $pos), $predicate, $action, $pos + 1)
else $input
};
declare function while-do(
$input as item()*,
$predicate as fn(item()*, xs:integer) as xs:boolean?,
$action as fn(item()*, xs:integer) as item()*
) as item()* {
while-do-helper($input, $predicate, $action, 1)
};While-do loops are very common in procedural programming languages, and this function provides a way to write functionally clean and interruptible iterations without side-effects. As long as a given condition is met, an new value is computed and tested again. Depending on the use case, the value can be a simple atomic item or an arbitrarily complex data structure.
The function fn:do-until can be used to perform the action before the first predicate test.
Note that, just as when writing recursive functions, it is easy to construct infinite loops.
| Expression: | while-do(2, fn { . <= 100 }, fn { . * . }) |
|---|---|
| Result: | 256 (The loop is interrupted as soon as the computed product is greater than 100.) |
| Expression: | while-do(
1,
fn($num, $pos) { $pos <= 10 },
fn($num, $pos) { $num * $pos }
) |
| Result: | 3628800 (This returns the factorial of 10, i.e., the product of all integers from 1 to 10.) |
| Expression: | let $input := (0 to 4, 6 to 10)
return while-do(
0,
fn($n) { $n = $input },
fn($n) { $n + 1 }
) |
| Result: | 5 (This returns the first positive number missing in a sequence.) |
| Expression: | while-do(
1 to 9,
fn($value) { head($value) < 5 },
fn($value) { tail($value) }
) |
| Result: | 5, 6, 7, 8, 9 (The first number of a sequence is removed as long as it is smaller than 5.) |
| Expression: | let $input := 3936256
return while-do(
$input,
fn($result) { abs($result * $result - $input) >= 0.0000000001 },
fn($guess) { ($guess + $input div $guess) div 2 }
) => round(5) |
| Result: | 1984 (This computes the square root of a number.) |
The following example generates random doubles. It is interrupted once a number exceeds a given limit: | |
let $result := while-do(
random-number-generator(),
fn($random) {
$random?number < 0.8
},
fn($random) {
map:put($random?next(), 'numbers', ($random?numbers, $random?number))
}
)
return $result?numbers | |
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. New functions introduced in this version are marked ➕ in the table of contents.
See 1 Introduction
PR 1547 1551
New in 4.0
PR 629 803
New in 4.0
See 3.2.2 fn:message
PR 1260 1275
A third argument has been added, providing control over the rounding mode.
See 4.4.4 fn:round
New in 4.0
See 4.4.7 fn:is-NaN
PR 1049 1151
Decimal format parameters can now be supplied directly as a map in the third argument, rather than referencing a format defined in the static context.
PR 1205 1230
New in 4.0
See 4.8.2 math:e
See 4.8.16 math:sinh
See 4.8.17 math:cosh
See 4.8.18 math:tanh
The 3.1 specification suggested that every value in the result range should have the same chance of being chosen. This has been corrected to say that the distribution should be arithmetically uniform (because there are as many xs:double values between 0.01 and 0.1 as there are between 0.1 and 1.0).
PR 261 306 993
New in 4.0
See 5.4.1 fn:char
New in 4.0
PR 937 995 1190
New in 4.0
See 5.4.13 fn:hash
The $action argument is new in 4.0.
See 5.6.4 fn:replace
New in 4.0
PR 1423 1413
New in 4.0
New in 4.0
Reformulated in 4.0 in terms of the new fn:in-scope-namespaces function; the semantics are unchanged.
Reformulated in 4.0 in terms of the new fn:in-scope-namespaces function; the semantics are unchanged.
New in 4.0
New in 4.0
See 13.1.12 fn:slice
New in 4.0. The function is identical to the internal op:same-key function in 3.1
PR 1120 1150
A callback function can be supplied for comparing individual items.
Changed in 4.0 to use transitive equality comparisons for numeric values.
New in 4.0. Originally proposed under the name fn:uniform
New in 4.0. Originally proposed under the name fn:unique
PR 1117 1279
The $options parameter has been added.
A new function is available for processing input data in HTML format.
PR 259 956
New in 4.0
An option is provided to control how JSON numbers should be formatted.
Additional options are available, as defined by fn:parse-json.
New in 4.0
New in 4.0
New in 4.0
New in 4.0
See 16.2.4 fn:every
New in 4.0
New in 4.0
New in 4.0
New in 4.0
New in 4.0
PR 521 761
New in 4.0
New in 4.0
A third argument is added, allowing user control of how absent keys should be handled.
See 17.4.9 map:get
New in 4.0
PR 478 515
New in 4.0
New in 4.0
New in 4.0
See 17.4.15 map:pair
New in 4.0
New in 4.0
New in 4.0.
New in 4.0
A third argument is added, allowing user control of how index-out-of-bounds conditions should be handled.
PR 968 1295
New in 4.0
PR 476 1087
New in 4.0
PR 360 476
New in 4.0
New in 4.0
New in 4.0
New in 4.0
New in 4.0
Supplying an empty sequence as the value of an optional argument is equivalent to omitting the argument.
New functions are provided to obtain information about built-in types and types defined in an imported schema.
Options are added to customize the form of the output.
See 2.2.6 fn:path
PR 533 719 834
New functions are available for processing input data in CSV (comma separated values) format.
PR 734 1233
New in 4.0
See 16.2.2 fn:chain
A new function fn:elements-to-maps is provided for converting XDM trees to maps suitable for serialization as JSON. Unlike the fn:xml-to-json function retained from 3.1, this can handle arbitrary XML as input.
New in 4.0
New in 4.0.
The default for the escape option has been changed to false. The 3.1 specification gave the default value as true, but this appears to have been an error, since it was inconsistent with examples given in the specification and with tests in the test suite.
The spec has been corrected to note that the function depends on the implicit timezone.
In 3.1, given a mixed input sequence such as (1, 3, 4.2e0), the specification was unclear whether it was permitted to add the first two integer items using integer arithmetic, rather than converting all items to doubles before performing any arithmetic. The 4.0 specification is clear that this is permitted; but since the items can be reordered before being added, this is not required.
See 13.4.2 fn:avg
See 13.4.5 fn:sum
It is explicitly stated that the limits for $precision are implementation-defined.
See 4.4.4 fn:round
For consistency with the new functions map:build and map:of-pairs, the handling of duplicates may now be controlled by the combine option as an alternative to the existing duplicates option.
New in 4.0
PR 173
New in 4.0
See 16.3.4 fn:op
PR 203
New in 4.0
See 17.4.1 map:build
PR 207
New in 4.0
PR 222
New in 4.0
See 13.2.7 fn:starts-with-subsequence
PR 250
New in 4.0
See 13.1.3 fn:foot
See 13.1.15 fn:trunk
PR 258
New in 4.0
PR 313
The second argument can now be a sequence of integers.
See 13.1.8 fn:remove
PR 314
New in 4.0
PR 326
Higher-order functions are no longer an optional feature.
See 1.2 Conformance
PR 419
New in 4.0
PR 434
New in 4.0
The function has been extended to allow output in a radix other than 10, for example in hexadecimal.
PR 482
Deleted an inaccurate statement concerning the behavior of NaN.
PR 507
New in 4.0
PR 546
The rules regarding use of non-XML characters in JSON texts have been relaxed.
PR 614
New in 4.0
PR 623
Substantially revised to allow multiple sort key definitions.
PR 631
New in 4.0
PR 662
Constructor functions now have a zero-arity form; the first argument defaults to the context item.
PR 680
The case-insensitive collation is now defined normatively within this specification, rather than by reference to the HTML "living specification", which is subject to change. The collation can now be used for ordering comparisons as well as equality comparisons.
PR 702
The function can now take any number of arguments (previously it had to be two or more), and the arguments can be sequences of strings rather than single strings.
See 5.4.4 fn:concat
PR 710
Changes the function to return a sequence of key-value pairs rather than a map.
PR 727
It has been clarified that loading a module has no effect on the static or dynamic context of the caller.
PR 795
New in 4.0
PR 828
The $predicate callback function accepts an optional position argument.
See 16.2.5 fn:filter
The $action callback function accepts an optional position argument.
The $predicate callback function now accepts an optional position argument.
The $action callback function now accepts an optional position argument.
PR 881
The way that fn:min and fn:max compare numeric values of different types has changed. The most noticeable effect is that when these functions are applied to a sequence of xs:integer or xs:decimal values, the result is an xs:integer or xs:decimal, rather than the result of converting this to an xs:double
See 13.4.3 fn:max
See 13.4.4 fn:min
PR 901
All three arguments are now optional, and each argument can be set to an empty sequence. Previously if $description was supplied, it could not be empty.
See 3.1.1 fn:error
The $label argument can now be set to an empty sequence. Previously if $label was supplied, it could not be empty.
See 3.2.1 fn:trace
The third argument can now be supplied as an empty sequence.
The second argument can now be an empty sequence.
The optional second argument can now be supplied as an empty sequence.
The 3rd, 4th, and 5th arguments are now optional; previously the function required either 2 or 5 arguments.
The optional third argument can now be supplied as an empty sequence.
PR 905
The rule that multiple calls on fn:doc supplying the same absolute URI must return the same document node has been clarified; in particular the rule does not apply if the dynamic context for the two calls requires different processing of the documents (such as schema validation or whitespace stripping).
See 13.6.1 fn:doc
PR 909
The function has been expanded in scope to handle comparison of values other than strings.
PR 924
Rules have been added clarifying that users should not be allowed to change the schema for the fn namespace.
See C Schemas
PR 925
The decimal format name can now be supplied as a value of type xs:QName, as an alternative to supplying a lexical QName as an instance of xs:string.
PR 932
The specification now prescribes a minimum precision and range for durations.
PR 933
When comments and processing instructions are ignored, any text nodes either side of the comment or processing instruction are now merged prior to comparison.
PR 940
New in 4.0
PR 953
Constructor functions for named record types have been introduced.
PR 962
New in 4.0
PR 969
New in 4.0
See 17.4.3 map:empty
PR 984
New in 4.0
See 8.4.1 fn:seconds
PR 987
The order of results is now prescribed; it was previously implementation-dependent.
PR 988
New in 4.0
See 14.3.8 fn:pin
See 14.3.9 fn:label
PR 1022
Regular expressions can include comments (starting and ending with #) if the c flag is set.
See 5.6.1 Regular expression syntax
See 5.6.2 Flags
PR 1028
An option is provided to control how the JSON null value should be handled.
PR 1032
New in 4.0
See 13.1.17 fn:void
PR 1046
New in 4.0
PR 1059
Use of an option keyword that is not defined in the specification and is not known to the implementation now results in a dynamic error; previously it was ignored.
See 1.7 Options
PR 1068
New in 4.0
PR 1072
The return type is now specified more precisely.
PR 1090
When casting from a string to a duration or time or dateTime, it is now specified that when there are more digits in the fractional seconds than the implementation is able to retain, excess digits are truncated. Rounding upwards (which could affect the number of minutes or hours in the value) is not permitted.
PR 1093
New in 4.0
PR 1117
The $options parameter has been added.
PR 1182
The $predicate callback function may return an empty sequence (meaning false).
See 16.2.4 fn:every
See 16.2.5 fn:filter
See 16.2.2016.2.21 fn:take-while
PR 1191
New in 4.0
See 2.3.1 fn:distinct-ordered-nodes
The $options parameter has been added, absorbing the $collation parameter.
PR 1250
For selected properties including percent and exponent-separator, it is now possible to specify a single-character marker to be used in the picture string, together with a multi-character rendition to be used in the formatted output.
PR 1257
The $options parameter has been added.
PR 1262
New in 4.0
PR 1265
The constraints on the result of the function have been relaxed.
PR 1280
As a result of changes to the coercion rules, the number of supplied arguments can be greater than the number required: extra arguments are ignored.
See 16.2.1 fn:apply
PR 1288
Additional error conditions have been defined.
PR 1296
New in 4.0
PR 1333
A new option is provided to allow the content of the loaded module to be supplied as a string.
PR 1353
An option has been added to suppress the escaping of the solidus (forwards slash) character.
PR 1358
New in 4.0
PR 1361
The term atomic value has been replaced by atomic item.
See 1.9 Terminology
PR 1393
Changes the function to return a sequence of key-value pairs rather than a map.
PR 1409
This section now uses the term primitive type strictly to refer to the 20 atomic types that are not derived by restriction from another atomic type: that is, the 19 primitive atomic types defined in XSD, plus xs:untypedAtomic. The three types xs:integer, xs:dayTimeDuration, and xs:yearMonthDuration, which have custom casting rules but are not strictly-speaking primitive, are now handled in other subsections.
See 21.1 Casting from primitive types to primitive types
The rules for conversion of dates and times to strings are now defined entirely in terms of XSD 1.1 canonical mappings, since these deliver exactly the same result as the XPath 3.1 rules.
See 21.1.2.2 Casting date/time values to xs:string
The rules for conversion of durations to strings are now defined entirely in terms of XSD 1.1 canonical mappings, since the XSD 1.1 rules deliver exactly the same result as the XPath 3.1 rules.
PR 1455
Numbers now retain their original lexical form, except for any changes needed to satisfy JSON syntax rules (for example, stripping leading zero digits).
PR 1481
The function has been extended to handle other Gregorian types such as xs:gYearMonth.
See 9.5.1 fn:year-from-dateTime
See 9.5.2 fn:month-from-dateTime
The function has been extended to handle other Gregorian types such as xs:gMonthDay.
See 9.5.3 fn:day-from-dateTime
The function has been extended to handle other types including xs:time.
See 9.5.4 fn:hours-from-dateTime
See 9.5.5 fn:minutes-from-dateTime
The function has been extended to handle other types such as xs:gYearMonth.
PR 1504
New in 4.0
Optional $separator added.
PR 1523
New in 4.0
PR 1545
New in 4.0
PR 1570
New in 4.0
PR 1703
The order of entries in maps is retained.
Ordered maps are introduced.
Enhanced to allow for ordered maps.
See 17.4.7 map:find
See 17.4.17 map:put