Main Page » XQuery » Functions » Standard Functions

Standard Functions

This page presents all functions of XQuery in the standard function namespace. Functions that have recently been added or changed in XQuery 4.0 are marked as such in their descriptions.

Conventions

All functions and errors are in the http://www.w3.org/2005/xpath-functions namespace, to which the fn prefix is statically bound. Some functions require permissions: fn:available-environment-variables, fn:environment-variable, fn:load-xquery-module and fn:put require admin permissions. Functions that retrieve external resources, such as fn:doc, fn:collection, fn:csv-doc, fn:html-doc, fn:json-doc, fn:transform and fn:unparsed-text, require create permissions, or read permissions if the resource is a database.

Sequences

General Functions

fn:empty

Signature
fn:empty(  $input  as item()*) as xs:boolean
SummaryReturns true if $input is an empty sequence.
Examples
empty(())
Result: true()

fn:exists

Signature
fn:exists(  $input  as item()*) as xs:boolean
SummaryReturns true if $input contains at least one item.
Examples
exists(1 to 10)
Result: true()

fn:foot

Signature
fn:foot(  $input  as item()*) as item()?
SummaryReturns the last item of $input. Equivalent to $input[last()].
Examples
foot(reverse(1 to 100))
Result: 1

fn:head

Signature
fn:head(  $input  as item()*) as item()?
SummaryReturns the first item of $input. Equivalent to $input[1].
Examples
head(1 to 100)
Result: 1

fn:identity

Signature
fn:identity(  $input  as item()*) as item()*
SummaryReturns $input unchanged. This function isn’t useful on its own, but can be used as an argument to other higher-order functions.
Examples
let $sort := sort(?, (), identity#1)
let $reverse-sort := sort(?, (), fn($x) { -$x })
return string-join((
  $sort((1, 5, 3, 2, 4)),
  '|',
  $reverse-sort((1, 5, 3, 2, 4))
))
Result: '12345|54321'
map:for-each({ 1: 'one', 2: 'two' }, identity#1)
Result: 1, 2. map:for-each invokes the supplied function with the key and value of every entry of the supplied map. As identity#1 takes only the first argument (the second argument is ignored, see calling higher-order functions) the result is the sequence of keys of the map.

fn:insert-before

Signature
fn:insert-before(  $input     as item()*,  $position  as xs:integer,  $insert    as item()*) as item()*
SummaryInserts $insert into $input before the item at the given $position. Positions before the first and after the last item are adjusted to the respective end of the sequence.
Examples
insert-before(1 to 3, 2, 'x')
Result: 1, 'x', 2, 3

fn:insert-separator

Updated: Renamed (before: fn:sequence-join).

Signature
fn:insert-separator(  $input      as item()*,  $separator  as item()*) as item()*
SummaryInserts a $separator between each item of $input. Equivalent to:
head($input), tail($input) ! ($separator, .)
Examples
(1 to 3)
=> insert-separator('|')
=> string-join()
Result: '1|2|3'
insert-separator((<_>1</_>, <_>2</_>, <_>3</_>), '; ')
Inserts semicolon strings between the three input items.

fn:items-at

Signature
fn:items-at(  $input  as item()*,  $at     as xs:integer*) as item()*
SummaryReturns the items from $input at the positions specified with $at in the given order. Equivalent to:
for $pos in $at
return $input[$pos]
Examples
items-at(reverse(1 to 5), 1)
Result: 5
items-at(('one', 'two'), (2, 1))
Result: 'two', 'one'
items-at(('a', 'b'), 0)
Result: ()

fn:remove

Signature
fn:remove(  $input      as item()*,  $positions  as xs:integer*) as item()*
SummaryReturns a new version of $input that excludes the items at the specified $positions.
Examples
remove(1 to 3, 2)
Result: 1, 3
remove(1 to 5, (5, 3, 1))
Result: 2, 4

fn:replicate

Signature
fn:replicate(  $input  as item()*,  $count  as xs:nonNegativeInteger) as item()*
SummaryEvaluates $input and returns the result $count times.
Examples
replicate('A', 3)
Result: 'A', 'A', 'A'
let $nodes := replicate(<node/>, 2)
return $nodes[1] is $nodes[2]
true is returned, as two instances of the same node are returned.

fn:reverse

Signature
fn:reverse(  $input  as item()*) as item()*
SummaryReturns the items of $input in reverse order.
Examples
reverse(1 to 5)
Result: 5, 4, 3, 2, 1

fn:slice

Signature
fn:slice(  $input  as item()*,  $start  as xs:integer?  := (),  $end    as xs:integer?  := (),  $step   as xs:integer?  := ()) as item()*
SummaryReturns a new version of $input starting from $start and ending at $end, using the specified $step:
  • If no start is specified, the sequence will start with the first item.
  • If no end is specified, all remaining items are returned.
  • If end is smaller than start, the items are returned in reverse order.
  • If a negative start or end is specified, the counter starts from the end of the sequence.
Examples
slice(1 to 5, 3)
Result: 3, 4, 5
slice(1 to 5, 3, 4)
Result: 3, 4
slice(1 to 10, -3)
Result: 8, 9, 10
slice(1 to 5, 4, 2)
Result: 4, 3, 2
slice(1 to 5, step := 2)
Result: 1, 3, 5

fn:subsequence

Signature
fn:subsequence(  $input   as item()*,  $start   as xs:numeric,  $length  as xs:numeric?  := ()) as item()*
SummaryReturns the items of $input starting at the position $start, and at most $length items. Positions outside the range of the sequence are ignored. If $length is omitted or empty, all remaining items are returned.
Examples
subsequence(1 to 10, 8)
Result: 8, 9, 10
subsequence(1 to 10, 3, 2)
Result: 3, 4
subsequence(1 to 10, -1, 4)
Result: 1, 2. Requested positions -1 to 2, of which only 1 and 2 exist.

fn:tail

Signature
fn:tail(  $input  as item()*) as item()*
SummaryReturns all items of $input except for the first one. Equivalent to $input[position() > 1].
Examples
tail(1 to 4)
Result: 2, 3, 4

fn:trunk

Signature
fn:trunk(  $input  as item()*) as item()*
SummaryReturns all items of $input except for the last one. Equivalent to $input[position() < last()].
Examples
trunk(reverse(1 to 4))
Result: 4, 3, 2

fn:unordered

Signature
fn:unordered(  $input  as item()*) as item()*
SummaryReturns the items of $input in an implementation-dependent order. BaseX returns the sequence unchanged.
Examples
unordered(1 to 3)
Result: 1, 2, 3

fn:void

Signature
fn:void(  $input  as item()*  := ()) as empty-sequence()
SummaryAbsorbs $input and returns an empty sequence. This function is helpful if some (often nondeterministic or side-effecting) code needs to be evaluated but the resulting value is not required.
Examples
void(fetch:binary('http://my.rest.service'))
Performs an HTTP request and ignores the result.

Comparisons

fn:atomic-equal

Signature
fn:atomic-equal(  $value1  as xs:anyAtomicType,  $value2  as xs:anyAtomicType) as xs:boolean
SummaryDetermines whether the atomic values $value1 and $value2 are equal.
Examples
atomic-equal(1, 1.0)
Result: true()
atomic-equal('a', xs:anyURI('a'))
Result: true()
atomic-equal(xs:double('NaN'), xs:double('NaN'))
Result: true()
atomic-equal(1, '1')
Result: false()

fn:compare

Signature
fn:compare(  $value1     as xs:anyAtomicType?,  $value2     as xs:anyAtomicType?,  $collation  as xs:string?  := fn:default-collation()) as xs:integer?
SummaryReturns -1, 0, or 1, depending on whether $value1 is less than, equal to, or greater than $value2, and using the specified $collation for strings.
Examples
compare(1, 1.0)
Result: 0
compare(xs:double('NaN'), 0)
Result: -1
compare('a', 'A')
Result: 1
compare(
  'Strasse',
  'Straße',
  collation({ 'lang': 'de', 'strength': 'primary' })
)
Result: 0
compare(xs:hexBinary('41'), xs:base64Binary('QQ=='))
Result: 0

fn:contains-subsequence

Signature
fn:contains-subsequence(  $input        as item()*,  $subsequence  as item()*,  $compare      as (fn($a, $b) as xs:boolean)?  := fn:deep-equal#2) as xs:boolean
SummaryDetermines whether $input contains $subsequence, using the $compare function to compare items.
Examples
contains-subsequence(1 to 10, 4 to 6)
Result: true()
contains-subsequence(
  ('anna', 'berta', 'clara', 'dora'),
  ('CLARA', 'DORA'),
  fn($a, $b) { $a = lower-case($b) }
)
Result: true()

fn:deep-equal

Updated: Diagnostics via the debug option.

Signature
fn:deep-equal(  $input1   as item()*,  $input2   as item()*,  $options  as (xs:string | map(*))?  := { 'collation': default-collation() }) as xs:boolean
SummaryDetermines if $input1 and $input2 are deep-equal. The $options can be either a string, denoting a collation, or an options map:
optiondefaultdescription
base-urifalse()Consider base-uri of nodes.
collationdefault-collation()Collation to be used.
commentsfalse()Consider comments.
debugfalse()If the result is false(), output the items that were found to be different (see Debugging).
id-propertyfalse()Consider id property of elements and attributes.
idrefs-propertyfalse()Consider idrefs property of elements and attributes.
ignore-empty-entriesfalse()Ignore map entries and array members whose value is an empty sequence.
in-scope-namespacesfalse()Consider in-scope namespaces.
items-equalvoid#0Custom function to compare items. If an empty sequence is returned, the standard comparison is applied.
map-orderfalse()Consider the order of map entries.
namespace-prefixesfalse()Consider prefixes in QNames.
nilled-propertyfalse()Consider nilled property of elements and attributes.
normalization-form()Applies Unicode normalization to strings. Allowed values are NFC, NFD, NFKC, NFKD and FULLY-NORMALIZED.
orderedtrue()Considers the top-level order of the input sequences.
processing-instructionsfalse()Consider processing instructions.
timezonesfalse()Consider timezones in time/date values.
unordered-elements()A list of QNames of elements considered whose child elements may appear in any order.
whitespacepreserveHandling of whitespace:
  • preserve: Compare strings unchanged.
  • strip: Ignore whitespace-only text nodes.
  • normalize: Normalize whitespace; ignore whitespace-only text nodes.

As BaseX is not schema-aware, all nodes have the same type annotations, and typed values are equal to string values. The options typed-values, type-annotations and type-variety are accepted and ignored.

Examples
deep-equal((), ())
Result: true()
deep-equal(1, 1.0)
Result: true()
deep-equal(
  <name sex='f' id='name1'>Sunita</name>,
  <name id="name1" sex="f">Sunita</name>
)
Result: true(). Attributes have no order, different quotes make no difference.
deep-equal((1, 2, 3), (3, 2, 1), { 'ordered': false() })
Result: true()
deep-equal('X', ' X ', { 'whitespace': 'normalize' })
Result: true()
deep-equal(sum#1, sum#2, {
  'items-equal': fn($a, $b) {
    if(($a, $b) instance of fn(*)*) {
      function-name($a) = function-name($b)
    }
  }
})
Result: true(). When comparing functions, only the function name is considered, but not the arity.

fn:distinct-values

Signature
fn:distinct-values(  $values     as xs:anyAtomicType*,  $collation  as xs:string?  := fn:default-collation()) as xs:anyAtomicType*
SummaryReturns the atomic items of $values with duplicates removed. Strings are compared with the specified $collation, and NaN is equal to itself. The first occurrence of each value is retained: unlike in XQuery 3.1, the order of the result is prescribed.
Examples
distinct-values((1, 2, 2, 3, 1))
Result: 1, 2, 3
distinct-values((2, 2.0e0, 2.0))
Result: 2. Numeric values of different types are compared by value.

fn:duplicate-values

Signature
fn:duplicate-values(  $values     as xs:anyAtomicType*,  $collation  as xs:string?  := fn:default-collation()) as xs:anyAtomicType*
SummaryReturns all values that appear for the second time in $values. If no $collation is specified, the function is similar to:
for $group in $values
group by $value := $group
where count($group) > 1
return $value
Examples
duplicate-values((1, 2, 3, 1.0, 1e0))
Result: 1
duplicate-values(1 to 100)
Result: ()
let $ids := duplicate-values(//@id)
where exists($ids)
return error((), 'Duplicate IDs found: ' || string-join($ids, ', '))
Raises an error for duplicates in a sequence.

fn:ends-with-subsequence

Signature
fn:ends-with-subsequence(  $input        as item()*,  $subsequence  as item()*,  $compare      as (fn($a, $b) as xs:boolean)?  := fn:deep-equal#2) as xs:boolean
SummaryDetermines whether $input ends with $subsequence, using the $compare function to compare items.
Examples
ends-with-subsequence(1 to 10, 8 to 10)
Result: true()
ends-with-subsequence(
  ('one', 'two', 'three'),
  ('t', 't'),
  starts-with#2
)
Result: true()

fn:index-of

Signature
fn:index-of(  $input      as xs:anyAtomicType*,  $target     as xs:anyAtomicType,  $collation  as xs:string?  := fn:default-collation()) as xs:integer*
SummaryReturns the positions of all items of $input that are equal to $target. Strings are compared with the specified $collation, and NaN is equal to itself.
Examples
index-of((10, 20, 30, 20), 20)
Result: 2, 4

fn:starts-with-subsequence

Signature
fn:starts-with-subsequence(  $input        as item()*,  $subsequence  as item()*,  $compare      as (fn($a, $b) as xs:boolean)?  := fn:deep-equal#2) as xs:boolean
SummaryDetermines whether $input starts with $subsequence, using the $compare function to compare items.
Examples
starts-with-subsequence(1 to 10, 1 to 3)
Result: true()
starts-with-subsequence(
  1 to 10,
  ('a', 'bb', 'ccc'),
  fn($a, $b) { $a = string-length($b) }
)
Result: true()

Cardinality

fn:exactly-one

Signature
fn:exactly-one(  $input  as item()*) as item()
SummaryReturns $input if it consists of exactly one item, and raises an error otherwise.
Examples
exactly-one(1)
Result: 1

fn:one-or-more

Signature
fn:one-or-more(  $input  as item()*) as item()+
SummaryReturns $input if it consists of at least one item, and raises an error otherwise.
Examples
one-or-more(1 to 3)
Result: 1, 2, 3

fn:zero-or-one

Signature
fn:zero-or-one(  $input  as item()*) as item()?
SummaryReturns $input if it consists of at most one item, and raises an error otherwise.
Examples
zero-or-one(())
Result: ()

Aggregations

fn:count

Signature
fn:count(  $input  as item()*) as xs:integer
SummaryReturns the number of items in $input.
Examples
count(1 to 10)
Result: 10

fn:all-equal

Signature
fn:all-equal(  $values     as xs:anyAtomicType*,  $collation  as xs:string?  := fn:default-collation()) as xs:boolean
SummaryReturns true if all items in $values are equal, using the specified $collation for string comparisons.
Examples
all-equal((1, 1.0, 1e0))
Result: true()
all-equal((1, '1'))
Result: false()
all-equal(())
Result: true()

fn:all-different

Signature
fn:all-different(  $values     as xs:anyAtomicType*,  $collation  as xs:string?  := fn:default-collation()) as xs:boolean
SummaryReturns true if all items in $values are distinct, using the specified $collation for string comparisons.
Examples
all-different(1 to 5)
Result: true()
all-different(())
Result: true()

fn:avg

Signature
fn:avg(  $values  as xs:anyAtomicType*) as xs:anyAtomicType?
SummaryReturns the average of the atomic items in $values, or an empty sequence if $values is empty.
Examples
avg(1 to 4)
Result: 2.5

fn:max

Signature
fn:max(  $values     as xs:anyAtomicType*,  $collation  as xs:string?  := fn:default-collation()) as xs:anyAtomicType?
SummaryReturns the largest of the atomic items in $values, or an empty sequence if $values is empty. Strings are compared with the specified $collation. Unlike in XQuery 3.1, integers and decimals are no longer converted to doubles; the result keeps the type of the input items.
Examples
max(1 to 10)
Result: 10
max(('Kafka', 'Camus', 'Tawada'))
Result: 'Tawada'

fn:min

Signature
fn:min(  $values     as xs:anyAtomicType*,  $collation  as xs:string?  := fn:default-collation()) as xs:anyAtomicType?
SummaryReturns the smallest of the atomic items in $values, or an empty sequence if $values is empty. Strings are compared with the specified $collation. Unlike in XQuery 3.1, integers and decimals are no longer converted to doubles; the result keeps the type of the input items.
Examples
min((3, 1.5, 2))
Result: 1.5

fn:sum

Signature
fn:sum(  $values  as xs:anyAtomicType*,  $zero    as xs:anyAtomicType?  := 0) as xs:anyAtomicType?
SummaryReturns the sum of the atomic items in $values. If $values is empty, $zero is returned.
Examples
sum(1 to 100)
Result: 5050
sum((), ())
Result: ()

Higher-Order Functions

fn:apply

Signature
fn:apply(  $function   as fn(*),  $arguments  as array(*)) as item()*
SummaryThe supplied $function is invoked with the specified $arguments. The arity of the function must be less than or equal to the size of the array.
Examples
apply(concat#5, array { 1 to 5 })
Result: '12345'
apply(fn($a) { sum($a) }, [ 1 to 5 ])
Result: 15
apply(substring#3, [ "abc", 2 ])
Raises an error as the array has only two members.

fn:do-until

Signature
fn:do-until(  $input      as item()*,  $action     as fn($value as item()*, $pos as xs:integer) as item()*,  $predicate  as fn($value as item()*, $pos as xs:integer) as xs:boolean?) as item()*
SummaryThis function provides a way to write functionally clean and interruptible iterations, commonly known as do while/until loops:
  1. $action is called with $input and the result is adopted as new $input.
  2. $predicate is called with $input. If the result is false, step 1 is repeated.
  3. Otherwise, $input is returned.
Examples
do-until(
  (),
  fn($value, $pos) { $value, $pos * $pos },
  fn($value) { foot($value) > 50  }
)
Result: 1, 4, 9, 16, 25, 36, 49, 64. The loop is interrupted once the last value of the generated sequence is greater than 50.
do-until(
  (1, 0),
  fn($value) { $value[1] + $value[2], $value },
  fn($value) { avg($value) > 10 }
)
Result: 55, 34, 21, 13, 8, 5, 3, 2, 1, 1, 0. The computation is continued as long as the average of the first Fibonacci numbers is smaller than 10.

fn:every

Signature
fn:every(  $input      as item()*,  $predicate  as (fn($item, $pos) as xs:boolean)?  := fn:boolean#1) as xs:boolean
SummaryReturns true if every item in $input matches $predicate. If no predicate is specified, the boolean value of the item will be checked.
Examples
every(1 to 5)
Result: true()
every(1 to 5, fn { . > 3 })
Result: false()
every(())
Result: true()

fn:filter

Signature
fn:filter(  $input      as item()*,  $predicate  as fn($item as item(), $pos as xs:integer) as xs:boolean?) as item()*
SummaryApplies the boolean $predicate to all items of the sequence $input, returning those for which it returns true(). The function can easily be implemented with fn:for-each:
declare function filter($input, $pred) {
  for-each(
    $input,
    fn($item) { if ($pred($item)) { $item } }
  )
};

An equivalent XQuery function is:

declare function filter(
  $input      as item()*,
  $predicate  as fn(item()) as xs:boolean?
) as item()* {
  $input[$predicate(.)]
};
Examples
filter(1 to 10, fn { . mod 2 eq 0 })
Result: 2, 4, 6, 8, 10. Returns all even integers until 10.
let $first-upper := fn($str) {
  let $first := substring($str, 1, 1)
  return $first eq upper-case($first)
}
return filter(('FooBar', 'foo', 'BAR'), $first-upper)
Result: 'FooBar', 'BAR'. Returns strings that start with an upper-case letter.
let $is-prime := fn($x) {
  $x gt 1 and (every $y in 2 to ($x - 1) satisfies $x mod $y != 0)
}
return filter(1 to 20, $is-prime)
Result: 2, 3, 5, 7, 11, 13, 17, 19. An inefficient prime number generator.

fn:fold-left

Signature
fn:fold-left(  $input   as item()*,  $init    as item()*,  $action  as fn($value as item()*, $item as item(), $pos as xs:int) as item()*) as item()*
SummaryThe left fold traverses the $input from the left. The query fold-left(1 to 5, 0, $f), for example, would be evaluated as:
$f($f($f($f($f(0, 1), 2), 3), 4), 5)

An equivalent XQuery function is:

declare function fold-left(
  $input   as item()*,
  $init    as item()*,
  $action  as fn(item()*, item()) as item()*
) as item()* {
  if (empty($input)) then $init
  else fold-left(
    tail($input),
    $action($init, head($input)),
    $action
  )
};

Note: Contrary to the official specification, the current position of the iteration can be retrieved via the third parameter of the higher-order function argument.

Examples
fold-left(1 to 5, 1, fn($result, $curr) { $result * $curr })
Result: 120. Computes the product of a sequence of integers.
fold-left(1 to 5, '$seed',
  concat('$f(', ?, ', ', ?, ')')
)
Illustrates the evaluation order and returns $f($f($f($f($f($seed, 1), 2), 3), 4), 5).
let $from-digits := fold-left(?, 0,
  fn($n, $d) { 10 * $n + $d }
)
return (
  $from-digits(1 to 5),
  $from-digits((4, 2))
)
Result: 12345, 42. Builds a decimal number from digits.
fold-left(
  1 to 10000000000,
  1,
  fn($n, $curr) { if($n > 100000) then $n else $n * $curr }
)
Result: 362880. Once the condition is met, further calculations are skipped.

fn:fold-right

Signature
fn:fold-right(  $input   as item()*,  $init    as item()*,  $action  as fn($item as item(), $value as item()*, $pos as xs:int) as item()*) as item()*
SummaryThe right fold traverses the $input from the right. The query fold-right(1 to 5, 0, $f), for example, would be evaluated as:
$f(1, $f(2, $f(3, $f(4, $f(5, 0)))))

Note that the order of the arguments of $fun are inverted compared to that in fn:fold-left:

declare function fold-right(
  $input   as item()*,
  $init    as item()*,
  $action  as fn(item(), item()*) as item()*
) as item()* {
  if (empty($input)) then $init
  else $action(
    head($input),
    fold-right(tail($input), $init, $action)
  )
};

Note: Contrary to the official specification, the current position of the iteration can be retrieved via the third parameter of the higher-order function argument.

Examples
fold-right(1 to 5, 1,
  fn($curr, $result) { $result * $curr }
)
Result: 120. Computes the product of a sequence of integers.
fold-right(1 to 5, '$seed',
  concat('$f(', ?, ', ', ?, ')')
)
Illustrates the evaluation order and $f(1, $f(2, $f(3, $f(4, $f(5, $seed))))).
let $reverse := fold-right(?, (), fn($item, $rev) { $rev, $item })
return $reverse(1 to 5)
Result: 5, 4, 3, 2, 1. Reverses a sequence of items.

fn:for-each

Signature
fn:for-each(  $input   as item()*,  $action  as fn($item as item(), $pos as xs:integer) as item()*) as item()*
SummaryApplies the specified $action to every item of $input and returns all results as a single sequence.

An equivalent XQuery function is:

declare function for-each(
  $input   as item()*,
  $action  as fn(item()) as item()*
) as item()* {
  for $item in $input
  return $action($item)
}
Examples
for-each(1 to 10, math:pow(?, 2))
Result: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100. Computes the square of all numbers from 1 to 10.
let $fs := (
  upper-case#1,
  substring(?, 4),
  string-length#1
)
return for-each($fs, fn($f) { $f('foobar') })
Result: 'FOOBAR', 'bar', 6. Applies a list of functions to a string.
("one", "two", "three") => for-each(upper-case(?))
Result: 'ONE', 'TWO', 'THREE'. Processes each item of a sequence with the arrow operator.

fn:for-each-pair

Signature
fn:for-each-pair(  $input1  as item()*,  $input2  as item()*,  $action  as fn($a as item(), $b as item(), $pos as xs:integer) as item()*) as item()*
SummaryApplies the specified $action to the successive pairs of items of $input1 and $input2. Evaluation is stopped if one sequence yields no more items.

An equivalent function is:

declare function for-each-pair(
  $input1  as item()*,
  $input2  as item()*,
  $action  as fn(item(), item()) as item()*
) as item()* {
  for $pos in 1 to min((count($input1), count($input2)))
  return $action($input1[$pos], $input2[$pos])
};
Examples
for-each-pair(
  for-each(1 to 10, fn { . mod 2 }),
  replicate(1, 5),
  fn($a, $b) { $a + $b }
)
Result: 2, 1, 2, 1, 2. Adds one to the numbers at odd positions.
let $number-words := fn($str) {
  string-join(
    for-each-pair(
      1 to 1000000000,
      tokenize($str, ' +'),
      concat(?, ': ', ?)
    ),
    ', '
  )
}
return $number-words('how are you?')
Result: '1: how, 2: are, 3: you?'
let $is-sorted := fn($input) {
  every $b in
    for-each-pair(
      $input,
      tail($input),
      fn($a, $b) { $a <= $b }
    )
  satisfies $b
}
return (
  $is-sorted(1 to 10),
  $is-sorted((1, 2, 42, 4, 5))
)
Result: true(), false(). Checks if a sequence is sorted.

fn:highest

Signature
fn:highest(  $input      as item()*,  $collation  as xs:string?  := fn:default-collation(),  $key        as (fn(item()) as xs:anyAtomicType*)?  := fn:data#1) as item()*
SummaryReturns those items from $input for which $key produces the highest value, using the specified $collation for strings.
Examples
highest(8 to 12)
Result: 12
highest(98 to 102, key := string#1)
Result: 99
highest(1 to 7, (), fn { . idiv 3 })
Result: 6, 7

fn:index-where

Signature
fn:index-where(  $input      as item()*,  $predicate  as fn($item as item(), $pos as xs:integer) as xs:boolean?) as xs:integer*
SummaryReturns the positions of all items of $input that match the $predicate function.
Examples
index-where(
  (10, 11, 12, 14, 17, 21, 26),
  fn { . mod 2 = 0 }
)
Result: 1, 3, 4, 7
index-where(
  (1, 8, 2, 7, 3),
  fn($item, $pos) { $item < 5 and $pos > 2 }
)
Result: 3, 5

fn:lowest

Signature
fn:lowest(  $input      as item()*,  $collation  as xs:string?  := fn:default-collation(),  $key        as (fn(item()) as xs:anyAtomicType*)?  := fn:data#1) as item()*
SummaryReturns those items from $input for which $key produces the lowest value, using the specified $collation for strings.
Examples
lowest(8 to 12)
Result: 8
lowest(98 to 102, key := string#1)
Result: 100
lowest(1 to 7, (), fn { . idiv 3 })
Result: 1, 2

fn:partial-apply

Signature
fn:partial-apply(  $function   as fn(*),  $arguments  as map(xs:positiveInteger, item()*)) as fn(*)
SummaryReturns a function with selected arguments of $function pre-bound to values in $arguments. The keys in the map denote 1-based positions of the arguments to bind.
Examples
let $char-at := partial-apply(substring#3, { 3: 1 })
return $char-at("abc", 2)
Result: 'b'

fn:partition

Signature
fn:partition(  $input       as item()*,  $split-when  as fn($group, $next, $pos) as xs:boolean?) as array(item()*)*
SummaryPartitions the $input into a sequence of non-empty arrays, starting a new partition when $split-when is true for a tested item.
Examples
partition((1 to 5), fn($seq) { count($seq) = 2 })
Result: [ 1, 2 ], [ 3, 4 ], [ 5 ]
partition(
  ('Anita', 'Anne', 'Barbara', 'Catherine', 'Christine'), 
  fn($partition, $next) {
    substring(head($partition), 1, 1) ne substring($next, 1, 1)
  }
)
The result:
[ 'Anita', 'Anne' ],
[ 'Barbara' ],
[ 'Catherine', 'Christine' ]

fn:some

Signature
fn:some(  $input      as item()*,  $predicate  as (fn($item, $pos) as xs:boolean)?  := fn:boolean#1) as xs:boolean
SummaryReturns true if some items in $input match $predicate. If no predicate is specified, the boolean value of the item will be checked.
Examples
some(-3 to 3)
Result: true()
some(1 to 5, fn { . > 3 })
Result: true()
some(())
Result: false()

fn:sort

Signature
fn:sort(  $input      as item()*,  $collation  as xs:string?  := fn:default-collation(),  $key        as fn($item as item()) as xs:anyAtomicType*  := fn:data#1) as item()*
SummaryReturns a new sequence with sorted $input items. A $collation and a $key can be supplied, which will be applied to each sort item. The items resulting from the sort key will be sorted using the semantics of the lt operator.
Examples
sort(reverse(1 to 3))
Result: 1, 2, 3
reverse(sort(1 to 3))
Result: 3, 2, 1
sort((-2, 1, 3), key := abs#1)
Result: 1, -2, 3
sort($employees, (), fn { @name, @age })
Sorts employees by their name and age.
sort((1, 'a'))
Raises an error because strings and integers cannot be compared.

fn:sort-by

Signature
fn:sort-by(  $input  as item()*,  $keys   as map(*)*) as item()*
SummaryReturns a new sequence with sorted $input items, matching the order of the sort $keys.
optiondefaultdescription
keyfn:data#1Sort function.
collationCollation URI.
order'ascending'Order (ascending, descending)
Examples
sort-by((-2, 1, 3), { 'key': abs#1 })
Result: 1, -2, 3
sort-by(1 to 3, { 'order': 'descending' })
Result: 3, 2, 1

fn:sort-with

Signature
fn:sort-with(  $input        as item()*,  $comparators  as (fn($a as item(), $b as item()) as xs:integer)+) as item()*
SummaryReturns a new sequence of $input with the order induced by the supplied $comparators.
Examples
sort-with((1, 4, 6, 5, 3), compare#2)
Result: 1, 3, 4, 5, 6
sort-with(
  (1, -2, 5, 10, -12, 8),
  fn($a, $b) { abs($a) - abs($b) }
)
Result: 1, -2, 5, 8, 10, -12
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) }
))
The result:
<person name="Jade" age="6"/>,
<person name="Jie" age="8"/>,
<person name="Josipa" age="8"/>

fn:subsequence-where

Signature
fn:subsequence-where(  $input  as item()*,  $from   as (fn($item as item(), $pos as xs:int) as xs:boolean)?  := true#0,  $to     as (fn($item as item(), $pos as xs:int) as xs:boolean)?  := false#0) as item()*
SummaryReturns a subsequence of $input starting with the first item that matches $from, and ending with the first subsequent item that matches $to.

The function is equivalent to:

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)
Examples
subsequence-where(1 to 5, fn { . >= 3 })
Result: 3, 4, 5
subsequence-where(1 to 5, fn { . >= 2 }, fn { . >= 4 })
Result: 2, 3, 4
let $drop-while := fn($input, $predicate) {
  subsequence-where($input, fn { not($predicate(.)) })
}
return $drop-while(1 to 5, fn { . <= 2 })
Result: 3, 4, 5. The function can be used to emulate the nonexisting drop-while function.

fn:take-while

Signature
fn:take-while(  $input      as item()*,  $predicate  as fn($item as item(), $pos as xs:integer) as xs:boolean?) as item()*
SummaryReturns items of $input as long as $predicate is satisfied. The predicate is called with the current item and position.

The function is equivalent to:

declare function take-while($input, $predicate, $pos := 1) {
  if(exists($input) and $predicate(head($input), $pos)) {
    head($input),
    take-while(tail($input), $predicate, $pos + 1)
  }
};
Examples
take-while((1, 5, 10, 20, 50, 100), fn { . <= 30 })
Returns all integers until a value is larger than 30.
take-while(
  (1 to 100) ! random:integer(50),
  fn($item, $pos) { . >= 10 }
)
Computes at most 100 random integers, but stops if an integer is smaller than 10.

fn:transitive-closure

Signature
fn:transitive-closure(  $node  as gnode()?,  $step  as fn($current as gnode()) as gnode()*) as gnode()*
SummaryComputes the transitive closure of $node by applying $step to each unchecked node and returns all the nodes except for the input node.
Examples
let $nodes := <xml>
  <node id='0'/>
  <node id='1' idref='0'/>
  <node id='2' idref='1'/>
  <node id='3' idref='1'/>
</xml>/node
return transitive-closure(
  head($nodes),
  fn($n) { $nodes[@idref = $n/@id] }
)
The result:
<node id='1' idref='0'/>,
<node id='2' idref='1'/>,
<node id='3' idref='1'/>

fn:while-do

Signature
fn:while-do(  $input      as item()*,  $predicate  as fn($value as item()*, $pos as xs:integer) as xs:boolean?,  $action     as fn($value as item()*, $pos as xs:integer) as item()*) as item()*
SummaryThis function provides a way to write functionally clean and interruptible iterations, commonly known as while loops:
  1. $predicate is called with $input.
  2. If the result is true, $action is called with $input, the result is adopted as new $input, and step 2 is repeated.
  3. Otherwise, $input is returned.
Examples
while-do(2, fn { . <= 100 }, fn { . * . })
Result: 256. The loop is interrupted as soon as the computed product is greater than 100.
while-do(
  1,
  fn($num, $pos) { $pos <= 10 },
  fn($num, $pos) { $num * $pos }
)
Result: 3628800. Returns the factorial of 10, i.e., the product of all integers from 1 to 10.
let $input := (0 to 4, 6 to 10)
return while-do(
  0,
  fn($n) { $n = $input },
  fn($n) { $n + 1 }
)
Result: 5. Returns the first positive number missing in a sequence.
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. Computes the square root of a number.

Booleans

Constants

fn:true

Signature
fn:true() as xs:boolean
SummaryReturns the boolean value true.
Examples
true()
Result: true()

fn:false

Signature
fn:false() as xs:boolean
SummaryReturns the boolean value false.
Examples
false()
Result: false()

Boolean Values

fn:boolean

Signature
fn:boolean(  $input  as item()*) as xs:boolean
SummaryReturns the effective boolean value of $input. An error is raised if $input is a sequence of more than one item that starts with an atomic item.
Examples
boolean('Kafka')
Result: true()
boolean(''), boolean(())
Result: false(), false()

fn:not

Signature
fn:not(  $input  as item()*) as xs:boolean
SummaryReturns the negated effective boolean value of $input.
Examples
not(true())
Result: false()

Numbers

Decimal Division

fn:divide-decimals

Signature
fn:divide-decimals(  $value      as xs:decimal,  $divisor    as xs:decimal,  $precision  as xs:integer?  := 0) as record(quotient as xs:decimal, remainder as xs:decimal)
SummaryThe function returns the quotient and remainder of a decimal division for the specified $value, the $divisor and an optional $precision.
Examples
divide-decimals(10, 3)
Result: { "quotient": 3, "remainder": 1 }
divide-decimals(10, 7, 3)
Result: { "quotient": 1.428, "remainder": 0.004 }

Numeric Values

fn:abs

Signature
fn:abs(  $value  as xs:numeric?) as xs:numeric?
SummaryReturns the absolute value of $value. The type of the result is the same as the type of the argument.
Examples
abs(-3.5)
Result: 3.5

fn:ceiling

Signature
fn:ceiling(  $value  as xs:numeric?) as xs:numeric?
SummaryReturns the smallest number that is not less than $value and has no fractional part.
Examples
ceiling(2.1)
Result: 3

fn:floor

Signature
fn:floor(  $value  as xs:numeric?) as xs:numeric?
SummaryReturns the largest number that is not greater than $value and has no fractional part.
Examples
floor(2.9)
Result: 2

fn:round

Signature
fn:round(  $value      as xs:numeric?,  $precision  as xs:integer?  := 0,  $mode       as xs:string?  := 'half-to-ceiling') as xs:numeric?
SummaryRounds a $value to a specified number of decimal places, using the specified $precision and $mode.
Allowed values for $mode are: floor, ceiling, toward-zero, away-from-zero, half-to-floor, half-to-ceiling, half-toward-zero, half-away-from-zero, and half-to-even.
Examples
round(1.5)
Result: 2.0
round(15, -1)
Result: 20
round(9.9999, 1, 'floor')
Result: 9.9

fn:round-half-to-even

Signature
fn:round-half-to-even(  $value      as xs:numeric?,  $precision  as xs:integer?  := 0) as xs:numeric?
SummaryRounds $value to the number of decimal places specified by $precision. If the value is exactly halfway between two candidates, the candidate with an even last digit is chosen. A negative $precision rounds to a power of ten.
Examples
round-half-to-even(2.5), round-half-to-even(3.5)
Result: 2, 4. Both results are even.
round-half-to-even(12345, -2)
Result: 12300

Testing for NaN

fn:is-NaN

Signature
fn:is-NaN(  $value  as xs:anyAtomicType) as xs:boolean
SummaryReturns true if the argument is the xs:float or xs:double value NaN.
Examples
is-NaN(0e0 div 0)
Result: true()
is-NaN('NaN')
Result: false()

Parsing

fn:number

Signature
fn:number(  $value  as xs:anyAtomicType?  := .) as xs:double
SummaryConverts $value to a double. NaN is returned if the conversion fails or if $value is an empty sequence.
Examples
number('3.5')
Result: 3.5e0
number('Kafka')
Result: NaN

fn:parse-integer

Signature
fn:parse-integer(  $value  as xs:string?,  $radix  as xs:integer?  := 10) as xs:integer?
SummaryConverts $value to an integer, using the supplied $radix in the range 2 to 36. The input may be positive or negative and can contain whitespace and underscore separators.
Examples
parse-integer('7B', 16)
Result: 123
parse-integer('11111111', 2)
Result: 255
parse-integer(' -1_000_000 ')
Result: -1000000

Formatting Integers

fn:format-integer

Signature
fn:format-integer(  $value     as xs:integer?,  $picture   as xs:string,  $language  as xs:string?  := ()) as xs:string
SummaryConverts $value to a string, using the supplied $picture and (optionally) $language.
Examples
format-integer(123, '0')
Result: '123'
format-integer(12, 'w')
Result: 'twelve'
format-integer(21, 'Ww;o', 'de')
Result: 'Einundzwanzigste'
format-integer(65535, '16^xxxx')
Result: 'ffff'
format-integer(15, '2^xxxx')
Result: '1111'

Formatting Numbers

fn:format-number

Signature
fn:format-number(  $value    as xs:numeric?,  $picture  as xs:string,  $options  as (xs:string | map(*))?  := ()) as xs:string
SummaryConverts $value to a string, using the supplied $picture and $options. The options argument can be the name of a statically available decimal-format or a set of options.
Examples
format-number(123, '0')
Result: '123'
format-number(1.23, '0,0##', 'de')
Result: '1,23'
format-number(1234, "0.000,0", { 'format-name': 'de' })
Result: '1.234,0'
format-number(1010, '0^0', { 'exponent-separator': '^' })
Result: '1^3'
format-number(1984.42, '00.0e0')
Result: '19.8e2'

Random Numbers

fn:random-number-generator

Signature
fn:random-number-generator(  $seed  as xs:anyAtomicType?  := ()) as fn:random-number-generator-record
SummaryCreates a random number generator, using an optional seed. The returned map contains three entries:
  • number is a random double between 0 and 1
  • next is a function that returns another random number generator
  • permute is a function that returns a random permutation of its argument
The returned random generator is deterministic: If the function is called twice with the same arguments and in the same execution scope, it will always return the same result.
Examples
let $rng := random-number-generator()
let $number := $rng?number
let $next-rng := $rng?next()
let $next-number := $next-rng?number
let $permutation := $rng?permute(1 to 5)
return ($number, $next-number, $permutation)

Strings

Assemble & Disassemble Strings

fn:codepoints-to-string

Signature
fn:codepoints-to-string(  $values  as xs:integer*) as xs:string
SummaryConverts the Unicode codepoints in $values to a string. Each codepoint must be a valid XML character.
Examples
codepoints-to-string((72, 101, 108, 108, 111))
Result: 'Hello'

fn:string-to-codepoints

Signature
fn:string-to-codepoints(  $value  as xs:string?) as xs:integer*
SummaryReturns the Unicode codepoints of $value.
Examples
string-to-codepoints('AB')
Result: 65, 66

String Comparisons

fn:codepoint-equal

Signature
fn:codepoint-equal(  $value1  as xs:string?,  $value2  as xs:string?) as xs:boolean?
SummaryReturns true if $value1 and $value2 contain the same codepoints. No collation is applied, and no Unicode normalization takes place. An empty sequence is returned if one of the arguments is empty.
Examples
codepoint-equal('Käthe', 'Ka' || char(0x308) || 'the')
Result: false(). Precomposed ä and a + combining diaeresis are different codepoints, see fn:normalize-unicode.

fn:collation

Signature
fn:collation(  $options  as map(*)) as xs:string
SummaryGenerates a collation URI with the specified $options. Depending on the availability of ICU, the returned collation will either use the standard or the ICU collation features (see Collations for more details).
Examples
collation({ 'language': 'de' })
Result: "http://basex.org/collation?language=de". Returned without ICU.
collation({ 'language': 'de' })
Result: "http://www.w3.org/2013/collation/UCA?language=de". Returned with ICU embedded.

fn:collation-available

Signature
fn:collation-available(  $collation  as xs:string) as xs:boolean
SummaryChecks if the specified $collation is supported.

fn:collation-key

Signature
fn:collation-key(  $value      as xs:string,  $collation  as xs:string?  := fn:default-collation()) as xs:base64Binary
SummaryReturns a binary item for $value, using the supplied $collation, which can be used for unambiguous and context-free comparisons.
Examples
for value $v in map:build(
  ('a', 'A', 'b'),
  collation-key(?, collation({ 'strength': 'primary' })),
  identity#1
)
return [ $v ]
The result:
[ "b" ]
[ ("a", "A") ]

fn:contains-token

Signature
fn:contains-token(  $value      as xs:string*,  $token      as xs:string,  $collation  as xs:string?  := fn:default-collation()) as xs:boolean
SummaryThe supplied strings will be tokenized at whitespace boundaries. The function returns true if one of the strings equals the supplied token, possibly under the rules of a supplied collation.
Examples
contains-token(('a', 'b c', 'd'), 'c')
Result: true()

String Values

fn:char

Signature
fn:char(  $value  as (xs:string | xs:positiveInteger)) as xs:string
SummaryReturns a single-character string for the specified $value, which can be:
Examples
char(65), char(0x41), char(0b01000001)
Result: 'A', 'A', 'A'
char('xcirc')
Result: '◯'
string-join(('auml', 'ouml', 'uuml', 'szlig') ! char(.))
Result: 'äöüß'
string-to-codepoints(char('\t'))
Result: 9

fn:characters

Signature
fn:characters(  $value  as xs:string?) as xs:string*
SummaryReturns the single characters of $value as a string sequence. Equivalent to:
for $cp in string-to-codepoints($value)
return codepoints-to-string($cp)
Examples
characters('AB')
Result: 'A', 'B'

fn:graphemes

Added: New function.

Signature
fn:graphemes(  $value  as xs:string?) as xs:string*
SummarySplits $value into a sequence of strings, each containing a single extended grapheme cluster as defined by UAX #29. Returns an empty sequence if $value is empty or the empty sequence.
Examples
graphemes("a" || char(0x308) || "b")
Result: "a" || char(0x308), "b". I.e., a + ◌̈ + b, three characters, two graphemes.
graphemes(char(0x1F476) || char(0x200D) || char(0x1F6D1))
Result: char(0x1F476) || char(0x200D) || char(0x1F6D1). I.e., 👶 + ZWJ + 🛑: three characters, one grapheme.
graphemes(char('\r') || char('\n'))
Result: char('\r') || char('\n'). I.e., CR + LF: two characters, one grapheme.

fn:concat

Signature
fn:concat(  $values...  as xs:anyAtomicType*) as xs:string
SummaryConcatenates the string representations of the supplied $values. Unlike in XQuery 3.1, the function accepts any number of arguments, and each argument may be a sequence.
Examples
concat('Gerhard', ' ', 'Richter')
Result: 'Gerhard Richter'
concat(1 to 3)
Result: '123'. A single argument with three items.
concat()
Result: ''

fn:string-join

Signature
fn:string-join(  $values     as xs:anyAtomicType*,  $separator  as xs:string?  := '') as xs:string
SummaryCreates a string by concatenating the supplied $values, optionally interspersed by a $separator.
Examples
string-join(1 to 3)
Result: '123'
string-join(('one', 'two', 'three'), ', ')
Result: 'one, two, three'

fn:pad-string

Added: New function.

Signature
fn:pad-string(  $value    as xs:anyAtomicType?,  $length   as xs:integer,  $options  as map(*)?  := {}) as xs:string
SummaryCreates a string by extending $value to the requested $length with padding characters. As in fn:string-join, the supplied value is atomized and cast to a string. A value that is already long enough is returned unchanged: the function never truncates. The following $options are available:
  • padding: string to pad with (default: ' '). It is repeated as often as required and cut to fit; it must not be empty.
  • side: side on which the padding is added: end (default), start or both. With both, an odd padding character is appended at the end.
Examples
pad-string('abc', 6)
Result: 'abc '
pad-string('abc', 6, { 'side': 'start' })
Result: ' abc'
pad-string(42, 6, { 'padding': '0', 'side': 'start' })
Result: '000042'
pad-string('Chapter 1', 20, { 'padding': '.' })
Result: 'Chapter 1...........'. Dot leaders in a table of contents.
pad-string('abcdef', 3)
Result: 'abcdef'. Padding never truncates.

fn:substring

Signature
fn:substring(  $value   as xs:string?,  $start   as xs:numeric,  $length  as xs:numeric?  := ()) as xs:string
SummaryReturns the characters of $value starting at the position $start, and at most $length characters. Positions outside the range of the string are ignored. If $length is omitted or empty, all remaining characters are returned.
Examples
substring('Camus', 2)
Result: 'amus'
substring('Camus', 2, 3)
Result: 'amu'
substring('Camus', 0, 3)
Result: 'Ca'. Requested positions 0 to 2, of which only 1 and 2 exist.

fn:string-length

Signature
fn:string-length(  $value  as xs:anyAtomicType?  := .) as xs:integer
SummaryReturns the number of characters in $value. Unlike in XQuery 3.1, the argument may be any atomic item, which will be cast to a string.
Examples
string-length('Kollwitz')
Result: 8
string-length('Ka' || char(0x308) || 'the')
Result: 6. Codepoints are counted, not graphemes, see fn:graphemes.

fn:normalize-space

Signature
fn:normalize-space(  $value  as xs:anyAtomicType?  := .) as xs:string
SummaryRemoves leading and trailing whitespace from $value and replaces all other sequences of whitespace by a single space. Unlike in XQuery 3.1, the argument may be any atomic item, which will be cast to a string.
Examples
normalize-space('  Mark   Rothko  ')
Result: 'Mark Rothko'

fn:normalize-unicode

Signature
fn:normalize-unicode(  $value  as xs:string?,  $form   as xs:string?  := 'NFC') as xs:string
SummaryConverts $value to the Unicode normalization form specified by $form, which can be NFC, NFD, NFKC, NFKD or the empty string.
Examples
normalize-unicode('Ka' || char(0x308) || 'the') eq 'Käthe'
Result: true(). The combining diaeresis is composed into a single character.

fn:upper-case

Signature
fn:upper-case(  $value  as xs:string?) as xs:string
SummaryConverts all characters of $value to upper case.
Examples
upper-case('Rothko')
Result: 'ROTHKO'

fn:lower-case

Signature
fn:lower-case(  $value  as xs:string?) as xs:string
SummaryConverts all characters of $value to lower case.
Examples
lower-case('KAFKA')
Result: 'kafka'

fn:translate

Signature
fn:translate(  $value    as xs:string?,  $replace  as xs:string,  $with     as xs:string) as xs:string
SummaryReplaces the characters of $value that occur in $replace by the character at the same position in $with. Characters without a counterpart in $with are removed.
Examples
translate('2026-07-28', '-', '/')
Result: '2026/07/28'

fn:hash

Signature
fn:hash(  $value      as (xs:string|xs:hexBinary|xs:base64Binary)?,  $algorithm  as xs:string?  := 'MD5',  $options    as map(*)?  := {}) as xs:hexBinary?
SummaryComputes a hash for the given $value, using the specified $algorithm. The supported algorithms are MD5, SHA-1, SHA-256, BLAKE3, and the cyclic redundancy check CRC-32. The $options have no effect in BaseX.
Examples
string(hash(''))
Result: 'D41D8CD98F00B204E9800998ECF8427E'
string(hash('', 'SHA-1'))
Result: 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'
hash('', 'CRC-32') => string()
Result: '00000000'
hash('BaseX', 'CRC-32') => string()
Result: '4C06FC7F'

Substring Matching

fn:contains

Signature
fn:contains(  $value      as xs:string?,  $substring  as xs:string?,  $collation  as xs:string?  := fn:default-collation()) as xs:boolean
SummaryReturns true if $value contains $substring, possibly under the rules of a supplied $collation.
Examples
contains('Yoko Tawada', 'wad')
Result: true()

fn:starts-with

Signature
fn:starts-with(  $value      as xs:string?,  $substring  as xs:string?,  $collation  as xs:string?  := fn:default-collation()) as xs:boolean
SummaryReturns true if $value starts with $substring, possibly under the rules of a supplied $collation.
Examples
starts-with('Anselm Neft', 'Anselm')
Result: true()

fn:ends-with

Signature
fn:ends-with(  $value      as xs:string?,  $substring  as xs:string?,  $collation  as xs:string?  := fn:default-collation()) as xs:boolean
SummaryReturns true if $value ends with $substring, possibly under the rules of a supplied $collation.
Examples
ends-with('Mark Rothko', 'ko')
Result: true()

fn:substring-before

Signature
fn:substring-before(  $value      as xs:string?,  $substring  as xs:string?,  $collation  as xs:string?  := fn:default-collation()) as xs:string
SummaryReturns the characters of $value that precede the first occurrence of $substring, or the empty string if $substring does not occur.
Examples
substring-before('Franz Kafka', ' ')
Result: 'Franz'

fn:substring-after

Signature
fn:substring-after(  $value      as xs:string?,  $substring  as xs:string?,  $collation  as xs:string?  := fn:default-collation()) as xs:string
SummaryReturns the characters of $value that follow the first occurrence of $substring, or the empty string if $substring does not occur.
Examples
substring-after('Franz Kafka', ' ')
Result: 'Kafka'

Regular Expressions

fn:matches

Signature
fn:matches(  $value    as xs:string?,  $pattern  as xs:string,  $flags    as xs:string?  := '') as xs:boolean
SummaryReturns true if $value matches the regular expression $pattern, using the optional $flags.
Examples
matches('Kafka', '^K')
Result: true()

fn:replace

Signature
fn:replace(  $value        as xs:string?,  $pattern      as xs:string,  $replacement  as (xs:string|fn($s, $g) as item()?)?  := (),  $flags        as xs:string?  := '') as xs:string
SummarySearches the regular expression $pattern in $value and performs a $replacement, using optional $flags. The replacement argument can either be a string, or a function that will be invoked with the currently matched string ($s) and the currently captured groups ($g).
Examples
replace('a1c', '\d', 'X')
Result: 'aXc'
replace('1aBc2', '[a-z]', 'X', 'i')
Result: '1XXX2'
replace('1aBc2', '[a-z]', upper-case#1)
Result: '1ABC2'
replace("Chapter 9", "[0-9]+", fn($match) { $match + 1 })
Result: 'Chapter 10'
replace(
  "12°34′57″",
  "([0-9]+)°([0-9]+)′([0-9]+)″",
  fn($full-match, $groups) {
    message($full-match),
    ($groups[1] + $groups[2] ÷ 60 + $groups[3] ÷ 3600) || '°'
  }
)
Result: '12.5825°'. Creates debugging output for the current match.

fn:tokenize

Signature
fn:tokenize(  $value    as xs:string?,  $pattern  as xs:string?  := (),  $flags    as xs:string?  := '') as xs:string*
SummarySplits $value into several substrings wherever the regular expression $pattern is found, or (if no pattern is supplied) at whitespace boundaries.
Examples
tokenize('a b c')
Result: 'a', 'b', 'c'
tokenize('a, b,  c', ',\s*')
Result: 'a', 'b', 'c'
tokenize('a|b|c', '|', 'q')
Result: 'a', 'b', 'c'. Literal pattern search.

fn:analyze-string

Signature
fn:analyze-string(  $value    as xs:string?,  $pattern  as xs:string,  $flags    as xs:string?  := '') as element(fn:analyze-string-result)
SummaryApplies the regular expression $pattern to $value, using the optional $flags, and returns an element in which matching and non-matching substrings are marked up. Unlike in XQuery 3.1, the pattern may match a zero-length string.
Examples
analyze-string('Kafka 1924', '\d+')
Result: <analyze-string-result xmlns="http://www.w3.org/2005/xpath-functions"><non-match>Kafka </non-match><match>1924</match></analyze-string-result>

fn:matching-segments

Added: New function.

Signature
fn:matching-segments(  $value    as xs:string?,  $pattern  as xs:string,  $flags    as xs:string?  := '') as fn:matching-segment-record*
SummaryApplies the regular expression $pattern to $value, using optional $flags, and returns one record for each match. Each record contains the matched substring, its 1-based position within the input, and a map of all captured groups. A group is keyed by its group number, or, if it is a named capturing group ((?<name>…)), by its name.
Examples
matching-segments("The cat sat on the mat.", "\w+")?substring
Result: 'The', 'cat', 'sat', 'on', 'the', 'mat'
matching-segments(
  "08-12-03",
  "^(\d+)\-(\d+)\-(\d+)$")?groups ! (?1?group, ?2?group, ?3?group
)
Result: '08', '12', '03'
matching-segments("Chapter 5", "(Chapter|Appendix)(?=\s+([0-9]+))")?groups?2
Result: { "group": "5", "position": 9 }. Groups captured inside a lookahead are included in the result.
matching-segments(
  "2026-06-25",
  "(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"
)?groups?year?group
Result: '2026'. A named capturing group is keyed by its name instead of its group number.

fn:regex

Signature
fn:regex(  $pattern  as xs:string,  $flags    as xs:string?  := '') as fn:compiled-regex-record
SummaryCompiles the regular expression $pattern with the optional $flags and returns a record whose fields provide the regular expression operations for the compiled pattern. Next to the pattern and flags strings, the record supplies the functions matches, tokenize, replace, analyze-string and matching-segments. Compiling a pattern once and applying it repeatedly is faster than passing the pattern to each single function call.
Examples
let $re := regex('\d+')
return $re?matches('Kafka 1924')
Result: true()
let $re := regex(',\s*')
return $re?tokenize('Kafka, Camus, Tawada')
Result: 'Kafka', 'Camus', 'Tawada'

URIs

fn:decode-from-uri

Signature
fn:decode-from-uri(  $value  as xs:string?) as xs:string
SummaryDecodes URI-escaped characters in $value.
Examples
decode-from-uri('S%C3%A3o%20Paulo%3F')
Result: 'São Paulo?'
decode-from-uri('%00')
Result: `�`. Invalid characters are replaced with the Unicode replacement character FFFD.

fn:encode-for-uri

Signature
fn:encode-for-uri(  $value  as xs:string?) as xs:string
SummaryEscapes characters in $value for use in a URI.
Examples
encode-for-uri('São Paulo?')
Result: 'S%C3%A3o%20Paulo%3F'
encode-for-uri('ABC123-~._ !"#<=>?')
Result: `ABC123-~._%20%21%22%23%3C%3D%3E%3F`

fn:escape-html-uri

Signature
fn:escape-html-uri(  $value  as xs:string?) as xs:string
SummaryEscapes all characters of $value that cannot be written directly in a URI, using the rules that HTML user agents apply to attributes such as href. Unlike fn:iri-to-uri, reserved ASCII characters, including spaces, are left untouched.
Examples
escape-html-uri('http://basex.org/Käthe Kollwitz')
Result: 'http://basex.org/K%C3%A4the Kollwitz'

fn:iri-to-uri

Signature
fn:iri-to-uri(  $value  as xs:string?) as xs:string
SummaryConverts the IRI $value to a URI by escaping all characters that are not permitted in a URI.
Examples
iri-to-uri('http://basex.org/Käthe Kollwitz')
Result: 'http://basex.org/K%C3%A4the%20Kollwitz'. Unlike fn:escape-html-uri, the space is escaped as well.

fn:resolve-uri

Signature
fn:resolve-uri(  $href  as xs:string?,  $base  as xs:string?  := ()) as xs:anyURI?
SummaryResolves the relative URI $href against $base, or against the static base URI of the query if $base is omitted or empty.
Examples
resolve-uri('b.xml', 'http://basex.org/a/')
Result: 'http://basex.org/a/b.xml'

Parsing & Building

fn:parse-uri

Signature
fn:parse-uri(  $value    as xs:string?,  $options  as map(*)?  := {}) as fn:uri-structure-record?
Summary Parses the supplied URI string and returns a map with its constituent components.

For a full description of behavior and the $options, see fn:parse-uri.

Examples
parse-uri("https://basex.org/download/")
The result:
{
  "uri": "https://basex.org/download/",
  "scheme": "https",
  "hierarchical": true(),
  "authority": "basex.org",
  "host": "basex.org",
  "path": "/download/",
  "path-segments": ("", "download", ""),
  "absolute": true()
}

fn:build-uri

Added: New function.

Signature
fn:build-uri(  $parts    as fn:uri-structure-record,  $options  as map(*)  := {}) as xs:string
Summary Constructs a URI from the $parts provided.

For a full description of behavior and the $options, see fn:build-uri.

Examples
build-uri({
  "scheme": "https",
  "host": "docs.basex.org",
  "path": "/main"
})
Result: 'https://docs.basex.org/main'

Durations

Duration Components

fn:years-from-duration

Signature
fn:years-from-duration(  $value  as xs:duration?) as xs:integer?
SummaryReturns the years component of $value.
Examples
years-from-duration(xs:yearMonthDuration('P2Y6M'))
Result: 2

fn:months-from-duration

Signature
fn:months-from-duration(  $value  as xs:duration?) as xs:integer?
SummaryReturns the months component of $value.
Examples
months-from-duration(xs:yearMonthDuration('P2Y6M'))
Result: 6

fn:days-from-duration

Signature
fn:days-from-duration(  $value  as xs:duration?) as xs:integer?
SummaryReturns the days component of $value.
Examples
days-from-duration(xs:dayTimeDuration('P3DT4H'))
Result: 3

fn:hours-from-duration

Signature
fn:hours-from-duration(  $value  as xs:duration?) as xs:integer?
SummaryReturns the hours component of $value.
Examples
hours-from-duration(xs:dayTimeDuration('P3DT4H'))
Result: 4

fn:minutes-from-duration

Signature
fn:minutes-from-duration(  $value  as xs:duration?) as xs:integer?
SummaryReturns the minutes component of $value.
Examples
minutes-from-duration(xs:dayTimeDuration('PT90M'))
Result: 30. 90 minutes are normalized to 1 hour and 30 minutes.

fn:seconds-from-duration

Signature
fn:seconds-from-duration(  $value  as xs:duration?) as xs:decimal?
SummaryReturns the seconds component of $value, including fractional seconds.
Examples
seconds-from-duration(xs:dayTimeDuration('PT1.5S'))
Result: 1.5

Constructing

fn:seconds

Signature
fn:seconds(  $value  as xs:decimal?) as xs:dayTimeDuration?
SummaryReturns a duration for $value, which specifies the number of seconds.
Examples
seconds(0)
Result: xs:dayTimeDuration('PT0S')
seconds(86_400.1)
Result: xs:dayTimeDuration('P1DT0.1S')
current-time() + seconds(100)
Adds 100 seconds to the current time.

Dates and Times

Constructing a dateTime

fn:dateTime

Signature
fn:dateTime(  $date  as xs:date?,  $time  as xs:time?) as xs:dateTime?
SummaryCombines $date and $time into a single xs:dateTime item.
Examples
dateTime(xs:date('2026-07-28'), xs:time('14:30:00'))
Result: 2026-07-28T14:30:00

fn:build-dateTime

Added: New function.

Signature
fn:build-dateTime(  $value  as fn:dateTime-record?) as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?
SummaryConstructs a Gregorian value from the components of the $value record.
Examples
build-dateTime({
  "year": 2026,
  "month": 4,
  "day": 8,
  "hours": 18,
  "minutes": 46,
  "seconds": 12,
  "timezone": xs:dayTimeDuration("PT2H")
})
Result: xs:dateTime("2026-04-08T18:46:12+02:00")

fn:unix-dateTime

Signature
fn:unix-dateTime(  $value  as xs:nonNegativeInteger?  := 0) as xs:dateTimeStamp
SummaryConverts a Unix timestamp to an xs:dateTimeStamp.
Examples
unix-dateTime(0)
Result: xs:dateTime('1970-01-01T00:00:00Z')

Date & Time Components

fn:year-from-dateTime

Signature
fn:year-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:integer?
SummaryReturns the year component of $value. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
year-from-dateTime(xs:dateTime('2026-07-28T14:30:15.5'))
Result: 2026
year-from-dateTime(xs:gYearMonth('2026-07'))
Result: 2026

fn:month-from-dateTime

Signature
fn:month-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:integer?
SummaryReturns the month component of $value. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
month-from-dateTime(xs:dateTime('2026-07-28T14:30:15.5'))
Result: 7

fn:day-from-dateTime

Signature
fn:day-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:integer?
SummaryReturns the day component of $value. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
day-from-dateTime(xs:dateTime('2026-07-28T14:30:15.5'))
Result: 28

fn:hours-from-dateTime

Signature
fn:hours-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:integer?
SummaryReturns the hours component of $value. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
hours-from-dateTime(xs:dateTime('2026-07-28T14:30:15.5'))
Result: 14

fn:minutes-from-dateTime

Signature
fn:minutes-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:integer?
SummaryReturns the minutes component of $value. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
minutes-from-dateTime(xs:dateTime('2026-07-28T14:30:15.5'))
Result: 30

fn:seconds-from-dateTime

Signature
fn:seconds-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:decimal?
SummaryReturns the seconds component of $value, including fractional seconds. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
seconds-from-dateTime(xs:dateTime('2026-07-28T14:30:15.5'))
Result: 15.5

fn:timezone-from-dateTime

Signature
fn:timezone-from-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as xs:dayTimeDuration?
SummaryReturns the timezone component of $value as a duration, or an empty sequence if $value has no timezone. Unlike in XQuery 3.1, other Gregorian types are accepted as well.
Examples
timezone-from-dateTime(xs:dateTime('2026-07-28T14:30:00+02:00'))
Result: PT2H

fn:year-from-date

Signature
fn:year-from-date(  $value  as xs:date?) as xs:integer?
SummaryReturns the year component of $value.
Examples
year-from-date(xs:date('2026-07-28'))
Result: 2026

fn:month-from-date

Signature
fn:month-from-date(  $value  as xs:date?) as xs:integer?
SummaryReturns the month component of $value.
Examples
month-from-date(xs:date('2026-07-28'))
Result: 7

fn:day-from-date

Signature
fn:day-from-date(  $value  as xs:date?) as xs:integer?
SummaryReturns the day component of $value.
Examples
day-from-date(xs:date('2026-07-28'))
Result: 28

fn:timezone-from-date

Signature
fn:timezone-from-date(  $value  as xs:date?) as xs:dayTimeDuration?
SummaryReturns the timezone component of $value as a duration, or an empty sequence if $value has no timezone.
Examples
timezone-from-date(xs:date('2026-07-28+02:00'))
Result: PT2H

fn:hours-from-time

Signature
fn:hours-from-time(  $value  as xs:time?) as xs:integer?
SummaryReturns the hours component of $value.
Examples
hours-from-time(xs:time('14:30:15'))
Result: 14

fn:minutes-from-time

Signature
fn:minutes-from-time(  $value  as xs:time?) as xs:integer?
SummaryReturns the minutes component of $value.
Examples
minutes-from-time(xs:time('14:30:15'))
Result: 30

fn:seconds-from-time

Signature
fn:seconds-from-time(  $value  as xs:time?) as xs:decimal?
SummaryReturns the seconds component of $value, including fractional seconds.
Examples
seconds-from-time(xs:time('14:30:15'))
Result: 15

fn:timezone-from-time

Signature
fn:timezone-from-time(  $value  as xs:time?) as xs:dayTimeDuration?
SummaryReturns the timezone component of $value as a duration, or an empty sequence if $value has no timezone.
Examples
timezone-from-time(xs:time('14:30:00+02:00'))
Result: PT2H

fn:parts-of-dateTime

Signature
fn:parts-of-dateTime(  $value  as (xs:dateTime | xs:date | xs:time | xs:gYear | xs:gYearMonth | xs:gMonth | xs:gMonthDay | xs:gDay)?) as fn:dateTime-record?
SummaryReturns the components of a Gregorian value.
Examples
parts-of-dateTime(xs:dateTime('2026-04-08T18:46:12+02:00'))
Result: { "year": 2026, "month": 4, "day": 8, "hours": 18, "minutes": 46, "seconds": 12, "timezone": "PT2H"}

Adjusting Timezones

fn:adjust-dateTime-to-timezone

Signature
fn:adjust-dateTime-to-timezone(  $value     as xs:dateTime?,  $timezone  as xs:dayTimeDuration?  := fn:implicit-timezone()) as xs:dateTime?
SummaryAdjusts $value to the specified $timezone. If $timezone is supplied as an empty sequence, the timezone component is removed.
Examples
adjust-dateTime-to-timezone(
  xs:dateTime('2026-07-28T14:30:00+02:00'),
  xs:dayTimeDuration('PT0S')
)
Result: 2026-07-28T12:30:00Z

fn:adjust-date-to-timezone

Signature
fn:adjust-date-to-timezone(  $value     as xs:date?,  $timezone  as xs:dayTimeDuration?  := fn:implicit-timezone()) as xs:date?
SummaryAdjusts $value to the specified $timezone. If $timezone is supplied as an empty sequence, the timezone component is removed.
Examples
adjust-date-to-timezone(xs:date('2026-07-28'), xs:dayTimeDuration('PT2H'))
Result: 2026-07-28+02:00
adjust-date-to-timezone(xs:date('2026-07-28+02:00'), ())
Result: 2026-07-28

fn:adjust-time-to-timezone

Signature
fn:adjust-time-to-timezone(  $value     as xs:time?,  $timezone  as xs:dayTimeDuration?  := fn:implicit-timezone()) as xs:time?
SummaryAdjusts $value to the specified $timezone. If $timezone is supplied as an empty sequence, the timezone component is removed.
Examples
adjust-time-to-timezone(
  xs:time('14:30:00+02:00'),
  xs:dayTimeDuration('-PT5H')
)
Result: 07:30:00-05:00

fn:civil-timezone

Signature
fn:civil-timezone(  $value  as xs:dateTime,  $place  as xs:string?  := ()) as xs:dayTimeDuration
SummaryReturns the timezone offset for a given date/time $value and $place. If no place is specified, the system’s default place is used.
Examples
civil-timezone(xs:dateTime('2001-01-01T11:11:11Z'))
Result: xs:dayTimeDuration('PT1H'). Returned when being executed in Germany.
civil-timezone(xs:dateTime('2001-07-01T11:11:11Z'))
Result: xs:dayTimeDuration('PT2H'). Returned when being executed in Germany.
let $dt := xs:dateTime('2024-07-01T01:01:01Z')
return adjust-dateTime-to-timezone($dt, civil-timezone($dt))
Result: xs:dateTime('2024-07-01T03:01:01+02:00'). Returned when being executed in Germany.
civil-timezone(xs:dateTime('2024-12-24T12:24:48'), 'Africa/Abidjan')
Result: xs:dayTimeDuration('PT0S')

Formatting

fn:format-dateTime

Signature
fn:format-dateTime(  $value     as xs:dateTime?,  $picture   as xs:string,  $language  as xs:string?  := (),  $calendar  as xs:string?  := (),  $place     as xs:string?  := ()) as xs:string?
SummaryConverts $value to a string, using the supplied $picture and (optionally) $language, $calendar and $place. Unlike in XQuery 3.1, the last three arguments can be omitted individually.
Examples
format-dateTime(xs:dateTime('2026-07-28T14:30:15.5'), '[H01]:[m01]')
Result: '14:30'

fn:format-date

Signature
fn:format-date(  $value     as xs:date?,  $picture   as xs:string,  $language  as xs:string?  := (),  $calendar  as xs:string?  := (),  $place     as xs:string?  := ()) as xs:string?
SummaryConverts $value to a string, using the supplied $picture and (optionally) $language, $calendar and $place. Unlike in XQuery 3.1, the last three arguments can be omitted individually.
Examples
format-date(xs:date('2026-07-28'), '[D1] [MNn] [Y]')
Result: '28 July 2026'
format-date(xs:date('2026-07-28'), '[D1]. [MNn] [Y]', 'de')
Result: '28. Juli 2026'

fn:format-time

Signature
fn:format-time(  $value     as xs:time?,  $picture   as xs:string,  $language  as xs:string?  := (),  $calendar  as xs:string?  := (),  $place     as xs:string?  := ()) as xs:string?
SummaryConverts $value to a string, using the supplied $picture and (optionally) $language, $calendar and $place. Unlike in XQuery 3.1, the last three arguments can be omitted individually.
Examples
format-time(xs:time('14:30:00'), '[h].[m01] [PN]')
Result: '2.30 PM'

Parsing Dates

fn:parse-ietf-date

Signature
fn:parse-ietf-date(  $value  as xs:string?) as xs:dateTime?
SummaryParses a string in the IETF format (which is widely used on the Internet) and returns an xs:dateTime item:
Examples
parse-ietf-date('28-Feb-1984 07:07:07')
Result: xs:dateTime('1984-02-28T07:07:07Z')
parse-ietf-date('Wed, 01 Jun 2001 23:45:54 +02:00')
Result: xs:dateTime('2001-06-01T23:45:54+02:00')

QNames and Notations

Creating QNames

fn:QName

Signature
fn:QName(  $uri    as xs:string?,  $qname  as xs:string) as xs:QName
SummaryCreates a QName from $uri and the lexical QName $qname, which may include a prefix.
Examples
QName('http://basex.org/', 'p:a')
Result: p:a

fn:parse-QName

Signature
fn:parse-QName(  $value  as xs:string?) as xs:QName?
SummaryConverts $value to a QName. The supplied string can be a local name, have a namespace prefix, or use the braced URI syntax.
Examples
parse-QName('xml:node') => namespace-uri-from-QName()
Result: 'http://www.w3.org/XML/1998/namespace'
let $qname := parse-QName('Q{http://gotcha.org/works}fine')
return string-join((
  namespace-uri-from-QName($qname),
  local-name-from-QName($qname)
), ': ')
Result: 'http://gotcha.org/works: fine'

fn:resolve-QName

Signature
fn:resolve-QName(  $value    as xs:string?,  $element  as element()) as xs:QName?
SummaryCreates a QName from the lexical QName $value, resolving its prefix against the namespaces that are in scope for $element.
Examples
resolve-QName('p:a', <e xmlns:p='http://basex.org/'/>)
Result: p:a

QName Components

fn:prefix-from-QName

Signature
fn:prefix-from-QName(  $value  as xs:QName?) as xs:NCName?
SummaryReturns the prefix of $value, or an empty sequence if the QName has no prefix.
Examples
prefix-from-QName(QName('http://basex.org/', 'p:a'))
Result: 'p'

fn:local-name-from-QName

Signature
fn:local-name-from-QName(  $value  as xs:QName?) as xs:NCName?
SummaryReturns the local name of $value.
Examples
local-name-from-QName(QName('http://basex.org/', 'p:a'))
Result: 'a'

fn:namespace-uri-from-QName

Signature
fn:namespace-uri-from-QName(  $value  as xs:QName?) as xs:anyURI?
SummaryReturns the namespace URI of $value, or the empty string if the QName is in no namespace.
Examples
namespace-uri-from-QName(QName('http://basex.org/', 'p:a'))
Result: 'http://basex.org/'

fn:expanded-QName

Signature
fn:expanded-QName(  $value  as xs:QName?) as xs:string?
SummaryReturns a string representation of the QName $value in the format Q{uri}local.
Examples
expanded-QName(xs:QName('country'))
Result: 'Q{}country'
expanded-QName(QName('http://eat.org/lunch', 'cake'))
Result: 'Q{http://eat.org/lunch}cake'

Nodes

Accessors

fn:base-uri

Signature
fn:base-uri(  $node  as node()?  := .) as xs:anyURI?
SummaryReturns the base URI of $node, or an empty sequence if no base URI is available.
Examples
base-uri(<a xml:base='http://basex.org/'/>)
Result: 'http://basex.org/'

fn:document-uri

Signature
fn:document-uri(  $node  as node()?  := .) as xs:anyURI?
SummaryReturns the URI of the document node $node, or an empty sequence if the node is no document node or has no URI.
Examples
document-uri(document { <a/> })
Result: (). A constructed document has no URI.

fn:nilled

Signature
fn:nilled(  $node  as node()?  := .) as xs:boolean?
SummaryReturns true if $node is an element that was validated against a schema and marked as nilled. An empty sequence is returned if $node is no element.
Examples
nilled(<a/>)
Result: false()

fn:node-name

Signature
fn:node-name(  $node  as node()?  := .) as xs:QName?
SummaryReturns the name of $node as a QName, or an empty sequence if the node has no name.
Examples
node-name(<xml/>)
Result: xml

fn:string

Signature
fn:string(  $value  as item()?  := .) as xs:string
SummaryReturns the string value of $value. If $value is an empty sequence, the empty string is returned.
Examples
string(<a>Kafka</a>)
Result: 'Kafka'

fn:data

Signature
fn:data(  $input  as item()*  := .) as xs:anyAtomicType*
SummaryReturns the atomized value of $input: nodes are replaced by their typed values, and atomic items are returned unchanged.
Examples
data(<a>1</a>)
Result: 1. The result is an xs:untypedAtomic item.

Other Properties

fn:has-children

Signature
fn:has-children(  $node  as gnode()?  := .) as xs:boolean
SummaryReturns true if $node has one or more child nodes.
Examples
has-children(<a><b/></a>)
Result: true()

fn:in-scope-namespaces

Signature
fn:in-scope-namespaces(  $element  as element()) as map((xs:NCName | enum('')), xs:anyURI)
SummaryReturns the in-scope namespaces of $element as a map.
Examples
in-scope-namespaces(<xsi:a/>)
The result:
{
  "xsi": "http://www.w3.org/2001/XMLSchema-instance",
  "xml": "http://www.w3.org/XML/1998/namespace"
}

fn:in-scope-prefixes

Signature
fn:in-scope-prefixes(  $element  as element()) as xs:string*
SummaryReturns the namespace prefixes that are in scope for $element.
Examples
in-scope-prefixes(<a xmlns:p='http://basex.org/'/>)
Result: 'p', 'xml'

fn:lang

Signature
fn:lang(  $language  as xs:string?,  $node      as node()?  := .) as xs:boolean
SummaryReturns true if $node has an xml:lang attribute whose value equals $language, or starts with $language followed by a hyphen.
Examples
lang('en', <a xml:lang='en-US'/>)
Result: true()

fn:local-name

Signature
fn:local-name(  $node  as node()?  := .) as xs:string
SummaryReturns the local name of $node, or the empty string if the node has no name.
Examples
local-name(<p:a xmlns:p='http://basex.org/'/>)
Result: 'a'

fn:name

Signature
fn:name(  $node  as node()?  := .) as xs:string
SummaryReturns the name of $node, including its prefix, or the empty string if the node has no name.
Examples
name(<p:a xmlns:p='http://basex.org/'/>)
Result: 'p:a'

fn:namespace-uri

Signature
fn:namespace-uri(  $node  as node()?  := .) as xs:anyURI
SummaryReturns the namespace URI of $node, or the empty string if the node is in no namespace.
Examples
namespace-uri(<p:a xmlns:p='http://basex.org/'/>)
Result: 'http://basex.org/'

fn:namespace-uri-for-prefix

Signature
fn:namespace-uri-for-prefix(  $value    as xs:string?,  $element  as element()) as xs:anyURI?
SummaryReturns the namespace URI that is bound to the prefix $value in the scope of $element, or an empty sequence if the prefix is not bound.
Examples
namespace-uri-for-prefix('p', <a xmlns:p='http://basex.org/'/>)
Result: 'http://basex.org/'

fn:path

Signature
fn:path(  $node     as gnode()?  := .,  $options  as map(*)?  := {}) as xs:string?
SummaryReturns a string that can be used as path expression to select the supplied $node, using the specified $options:
optiondefaultdescription
origin()If present, the returned path will be relative to this origin node.
lexicalfalse()If enabled, fn:name() will be used to serialize node names.
namespaces{}A map with prefixes and namespace URIs. If a prefix is used for a namespace, it is used instead of the Q{URI} syntax when serializing nodes names.
indexestrue()By default, the index positions of nodes are included in the string.
Examples
let $xml := document { <xml><a/></xml> }
return path($xml/xml/a, { 'indexes': false() })
Result: '/Q{}xml/Q{}a'
let $xml := document {
  <xml xmlns='URI'><a/></xml>
}
let $namespaces := in-scope-namespaces($xml/*)
return path($xml/*:xml/*:a, { 'namespaces': $namespaces })
Result: '/xml[1]/a[1]'

fn:root

Signature
fn:root(  $node  as gnode()?  := .) as gnode()?
SummaryReturns the root of the tree that contains $node.
Examples
root(<a><b><c/></b></a>/b)
Result: <a><b><c/></b></a>

fn:siblings

Signature
fn:siblings(  $node  as gnode()?  := .) as gnode()*
SummaryReturns the siblings of a $node, including the node itself.
Examples
siblings(<xml><a/><b/><c/></xml>/b)
Result: <a/>, <b/>, <c/>

Node Sequences

fn:distinct-ordered-nodes

Signature
fn:distinct-ordered-nodes(  $nodes  as gnode()*) as gnode()*
SummaryReturns nodes in distinct document order: duplicate nodes (nodes with the same node identity) are removed, and the remaining nodes are returned in document order. This function makes explicit what the path expression does before returning the result of a node traversal. It is equivalent to expressions like:
$nodes/self::node()
$nodes/.
$nodes union .
$nodes except ()
Examples
let $doc := <doc><b/><a/></doc>
return distinct-ordered-nodes(($doc/a, $doc/a, $doc/b))
Result: <b/>, <a/>

fn:innermost

Signature
fn:innermost(  $nodes  as gnode()*) as gnode()*
SummaryReturns those nodes of $nodes that have no descendant in $nodes, in document order and without duplicates.
Examples
let $xml := <a><b><c/></b></a>
return innermost(($xml, $xml//c))
Result: <c/>

fn:outermost

Signature
fn:outermost(  $nodes  as gnode()*) as gnode()*
SummaryReturns those nodes of $nodes that have no ancestor in $nodes, in document order and without duplicates.
Examples
let $xml := <a><b><c/></b></a>
return outermost(($xml, $xml//c))
Result: <a><b><c/></b></a>

Identifiers

fn:id

Signature
fn:id(  $values  as xs:string*,  $node    as node()?  := .) as element()*
SummaryReturns the elements of the tree containing $node that have an ID value matching one of the supplied $values.
Examples
id('x', document { <a><b xml:id='x'/></a> })
Result: <b xml:id="x"/>

fn:element-with-id

Signature
fn:element-with-id(  $values  as xs:string*,  $node    as node()?  := .) as element()*
SummaryReturns the elements of the tree containing $node that have an ID value matching one of the supplied $values. For most documents, the result is the same as for fn:id; the functions differ for schema-validated documents in which an ID is not supplied by an attribute.
Examples
element-with-id('x', document { <a><b xml:id='x'/></a> })
Result: <b xml:id="x"/>

fn:idref

Signature
fn:idref(  $values  as xs:string*,  $node    as node()?  := .) as node()*
SummaryReturns the elements and attributes of the tree containing $node that have an IDREF value matching one of the supplied $values. Attributes are only typed as IDREF if the document was parsed with a DTD or validated against a schema.
Examples
idref('x', document { <a><b xml:id='x'/></a> })
Result: (). Without a DTD or schema, no attribute is typed as IDREF.

fn:generate-id

Signature
fn:generate-id(  $node  as gnode()?  := .) as xs:string
SummaryReturns a string that uniquely identifies $node. The returned value is implementation-dependent; it is guaranteed to be the same for every call on the same node, and different for distinct nodes. The empty string is returned if $node is an empty sequence.
Examples
let $node := <a/>
return generate-id($node) eq generate-id($node)
Result: true()

Function Items

fn:function-lookup

Signature
fn:function-lookup(  $name   as xs:QName,  $arity  as xs:integer) as fn(*)?
SummaryReturns the function with the supplied $name and $arity, or an empty sequence if no such function exists.
Examples
function-lookup(xs:QName('fn:abs'), 1)(-3)
Result: 3

fn:function-name

Signature
fn:function-name(  $function  as fn(*)) as xs:QName?
SummaryReturns the name of a $function item.
Examples
function-name(true#0)
Result: xs:QName('fn:true')
function-name(fn { . + 1 })
Result: ()

fn:function-arity

Signature
fn:function-arity(  $function  as fn(*)) as xs:integer
SummaryReturns the arity (number of parameters) of a $function item.
Examples
function-arity(true#0)
Result: 0
function-arity(fn { . + 1 })
Result: 1

fn:function-identity

Signature
fn:function-identity(  $function  as fn(*)) as xs:string
SummaryReturns a string representing the identity of a $function. The generated string may change when the function is run multiple times.
Examples
function-identity({})
Result: 'map0'
function-identity(abs#1)
Result: 'fn:abs#1'

fn:function-annotations

Signature
fn:function-annotations(  $function  as fn(*)) as map(xs:QName, xs:anyAtomicType*)*
SummaryReturns the annotations of a $function item as a sequence of single-entry maps. Each map contains one annotation name as its key and the annotation arguments as its value.
Examples
declare
  %public
  %rest:GET
  %rest:path('/')
  %perm:allow('all')
function local:index($n) {
  <html>Welcome!</html>
};
function-annotations(local:index#1)
The result:
{ #xq:public: () },
{ #rest:GET: () },
{ #rest:path: '/' },
{ #perm:allow: 'all' }

let $add := fn($a, $b) { $a * $b }
let $double := %local:deprecated fn($a) { $a + $a }
for $ann in ($add, $double) =!> function-annotations()
where map:keys($ann) = #local:deprecated
return 'Deprecated function found.'
Result: 'Deprecated function found.'

Maps

fn:element-to-map-plan

Added: New function.

Signature
fn:element-to-map-plan(  $input  as (document-node() | element())*) as map(xs:string, (fn:element-conversion-plan-record | fn:attribute-conversion-plan-record))
SummaryAnalyzes the supplied $input nodes and returns a conversion plan suitable for use as the plan option of fn:element-to-map. The returned map contains entries for element and attribute names. Each value is either an fn:element-conversion-plan-record:
record(
  layout  as xs:string,
  child?  as xs:string?,
  type?   as enum('integer', 'decimal', 'double', 'boolean', 'string')
)
or an fn:attribute-conversion-plan-record (for entries prefixed with @):
record(
  type  as enum('integer', 'decimal', 'double', 'boolean', 'string', 'skip')
)
The type is inferred from the string values: integer, decimal or double if all values are numeric, boolean if all values are true/false, and string otherwise. Values with a leading zero (such as 007) are kept as string to preserve their lexical form.
Examples
element-to-map-plan(<a><b>3</b><b>4</b></a>)
The result:
{
  "a": {
    "layout": "list",
    "child": "b"
  },
  "b": {
    "layout": "simple",
    "type": "integer"
  }
}

element-to-map-plan((<a x="2">red</a>, <a x="3">blue</a>))
The result:
{
  "a": {
    "layout": "simple-plus"
  },
  "@x": {
    "type": "integer"
  }
}

let $plan := element-to-map-plan(
  <name><first>Jane</first><last>Smith</last></name>
)
return element-to-map(
  <name><first>John</first><last>Doe</last></name>,
  { 'plan': $plan }
)
Generate a plan from sample data and apply it to a new document. The result:
{
  "name": {
    "first": "John",
    "last": "Doe"
  }
}

fn:element-to-map

Added: New function.

Signature
fn:element-to-map(  $element  as (document-node() | element())?,  $options  as map(*)?  := {}) as map(xs:string, item()?)?
SummaryConverts $element into a map suitable for JSON serialization. The returned map has a single entry whose key is the element name and whose value represents the element’s attributes and children. The $options argument accepts the following keys:
optiondefaultdescription
plan{}A conversion plan, as generated by fn:element-to-map-plan, controlling how individual elements and attributes are converted.
attribute-marker'@'String prepended to keys representing attributes in the output.
content-key'#content'String used in place of #content as the key for content derived from an element’s children. If the chosen key clashes with another key in the generated map, # characters are prepended until it is unique.
name-format'default'Controls how element and attribute names are serialized: lexical, local, eqname, or default.
Examples
element-to-map(<foo>bar</foo>)
Result: { "foo": "bar" }
element-to-map(
  <name>
    <first>Jane</first>
    <last>Smith</last>
  </name>
)
Result: { "name": { "first": "Jane", "last": "Smith" } }
element-to-map(
  <list>
    <item value='1'/>
    <item value='2'/>
  </list>,
  { 'attribute-marker': '' }
)
Result: { "list": [ { "value": "1" }, { "value": "2" } ] }. The empty attribute marker avoids an @ prefix on attribute keys.

JNodes

JNodes wrap maps and arrays in a tree structure with identity and document order, enabling XPath path expressions to navigate JSON-like data. See JNodes for an introduction.

fn:jtree

Added: New function.

Signature
fn:jtree(  $input  as (map(*)|array(*))) as jnode((), (map(*)|array(*)))
SummaryCreates a root JNode that wraps the supplied map or array $input, enabling path expressions to navigate the resulting JTree.

Calling jtree($X) is equivalent to the path expression $X/.: maps and arrays that appear as the left-hand operand of / are wrapped implicitly, so an explicit call is rarely needed.

Examples
jtree([ "a", "b", "c" ])/*[1]
Result: "a"
jtree([ "a", "b" ])/* =!> jkey()
Result: 1, 2

fn:jkey

Added: New function.

Signature
fn:jkey(  $input  as jnode()?  := .) as xs:anyAtomicType?
SummaryReturns the jkey property of $input: the map key or 1-based array index that identifies this JNode within its parent. Returns an empty sequence if $input is empty or is a root JNode.
Examples
let $array := [ 1, 4.5, "eight", 10 ]
return $array/jnode(*, xs:integer) =!> jkey()
Result: 1, 4. Positions of the integer members.
let $map := { 'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday' }
return $map/(Mo|We|Fr|Su) =!> jkey()
Result: "Mo", "We". Keys that actually exist in the map; Fr and Su are silently ignored.
let $array := [ [ 4, 18 ], [ 30, 4, 22 ] ]
return $array/descendant::*[. > 25]/ancestor-or-self::* =!> jkey()
Result: 2, 1. The subarray [ 30, 4, 22 ] is at index 2; the value 30 is at index 1 within it.

fn:jvalue

Added: New function.

Signature
fn:jvalue(  $input  as jnode()?  := .) as item()*
SummaryReturns the jvalue property of $input: the value wrapped by the JNode. Returns an empty sequence if $input is empty. In most contexts an explicit call is unnecessary, because the coercion rules automatically extract the value when a JNode is used where an atomic value, map, or array is required. Notable exceptions are effective boolean value tests (if ($jnode) tests for the existence of a child, not its truth value) and calls to functions that accept arbitrary sequences such as count or deep-equal.
Examples
let $array := [ 1, 3, 4.5, 7, "eight", 10 ]
return $array/jnode(*, xs:integer) =!> jvalue()
Result: 1, 3, 7, 10
let $map := { 'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday' }
return $map/(Mo|We|Fr|Su) =!> jvalue()
Result: "Monday", "Wednesday"

fn:jposition

Added: New function.

Signature
fn:jposition(  $input  as jnode()?  := .) as xs:integer?
SummaryReturns the jposition property of $input: the 1-based position of the map or array that produced this JNode within the sequence that is the parent entry or member value. Returns an empty sequence if $input is empty or is a root JNode. This property is always 1 for JTrees derived from JSON, since JSON values are never multi-item sequences. It is only relevant when a map entry or array member holds a sequence containing multiple maps or arrays.
Examples
let $input := {
  "a": [ 10, 20 ],
  "b": ([ 30, 40 ], [], 0, [ 50 ])
}
return $input/b/* ! [ jposition(), jkey(), jvalue() ]
The result:
[ 1, 1, 30 ]
[ 1, 2, 40 ]
[ 4, 1, 50 ]

External Resources and Data Formats

External Information

fn:doc

Added: trusted option.

Removed: entity-expansion-limit and allow-external-entities options.

Signature
fn:doc(  $source   as xs:string?,  $options  as map(*)?  := {}) as document-node()?
Summary

Retrieves and parses an XML document from a given URI and returns a document-node() item.

Within $options, the following keys can be specified:

optiondefaultdescription
dtd-validationfalse()Enables DTD validation if set to true().
stabletrue()Ensures deterministic results when set to true().
strip-spacefalse()Controls whether whitespace-only text nodes are stripped.
xincludefalse()Expands xi:include elements if set to true(). Requires the trusted option to be enabled.
xsd-validationskipSpecifies XSD validation mode: strict, lax, or skip.
use-xsi-schema-locationfalse()Determines whether xsi:schemaLocation and xsi:noNamespaceSchemaLocation declarations are followed. Requires the trusted option to be enabled.

Additionally, these BaseX XML Parsing options are supported, using lower-case option names:

optiondefaultdescription
dtdtrue()When set to true(), external entities are processed, otherwise they are ignored.
intparsefalse()Uses the internal XML parser instead of the standard Java XML parser.
stripnsfalse()Strips all namespaces from an XML document while parsing.
trustedfalse()Allows the parser to fetch external resources (external DTDs and entities, XInclude documents, and referenced schemas). The default is controlled by the FNXMLTRUSTED option.
Examples
doc('example.xml')
The result:
<root>
   <a> </a>
</root>

doc('example.xml', { 'strip-space': true() })
Result: <root><a/></root>
doc('book.xml', { 'xinclude': true(), 'trusted': true() })
Parses book.xml and expands its xi:include elements. Because XInclude fetches external documents, the trusted option must be enabled; without it, the call is rejected with err:FODC0016 (External resources not available, call is untrusted).

fn:doc-available

Added: trusted option.

Signature
fn:doc-available(  $source   as xs:string?,  $options  as map(*)?  := {}) as xs:boolean
Summary

Checks if an XML document can be retrieved and parsed from a given URI. Returns true if fn:doc($source, $options) would return a document node, and false otherwise.

The recognized options are the same as for fn:doc.

Examples
doc-available('https://example.org/example.xml')
Result: false()

fn:collection

Signature
fn:collection(  $source  as xs:string?  := ()) as item()*
SummaryReturns the documents of the collection that is addressed by $source. If no argument is supplied, the documents of the currently opened database are returned.
Examples
collection('factbook')
Returns all documents of the database factbook.

fn:uri-collection

Signature
fn:uri-collection(  $source  as xs:string?  := ()) as xs:anyURI*
SummaryReturns the URIs of the documents of the collection that is addressed by $source.
Examples
uri-collection('factbook')
Returns the URIs of all documents of the database factbook.

fn:unparsed-text

Signature
fn:unparsed-text(  $source   as xs:string?,  $options  as item()?  := ()) as xs:string?
SummaryRetrieves $source and returns its contents as a string. The $options argument can be supplied as an encoding string or as a map with an encoding key.
Examples
unparsed-text('notes.txt')
Returns the contents of the file notes.txt.

fn:unparsed-text-lines

Signature
fn:unparsed-text-lines(  $source   as xs:string?,  $options  as item()?  := ()) as xs:string*
SummaryRetrieves $source and returns its contents as a sequence of strings, one for each line. As in fn:unparsed-text, the $options argument can be supplied as an encoding string or as a map.
Examples
count(unparsed-text-lines('notes.txt'))
Counts the lines of the file notes.txt.

fn:unparsed-text-available

Signature
fn:unparsed-text-available(  $source   as xs:string?,  $options  as item()?  := ()) as xs:boolean
SummaryReturns true if $source can be retrieved and decoded by fn:unparsed-text.
Examples
unparsed-text-available('notes.txt')
Indicates whether the file notes.txt can be read.

fn:unparsed-binary

Signature
fn:unparsed-binary(  $source  as xs:string?) as xs:base64Binary?
SummaryRetrieves $source and returns it as a binary.
Examples
unparsed-binary('https://files.basex.org/releases/BaseX.jar')
Retrieves the latest release of BaseX.

fn:system-properties

Added: New function.

Signature
fn:system-properties() as map(xs:QName, xs:anyAtomicType)
SummaryReturns a map with information about the processor. As the map keys are QNames, a property is looked up with ?#name. BaseX supplies the following properties:
  • xpath-version: version of the supported XPath specification
  • xsd-version: version of the supported XML Schema specification
  • product-name: name of the product
  • product-version: version of the product
  • schema-aware: indicates whether schema-awareness is supported
  • accepts-typed-data: indicates whether typed data is accepted
  • supports-xinclude: indicates whether XInclude is supported
  • supports-dtd: indicates whether DTDs are fully supported
  • supports-invisible-xml: indicates whether Invisible XML is available
  • supports-dynamic-xquery: indicates whether dynamic XQuery evaluation is supported
  • supports-dynamic-xslt: indicates whether dynamic XSLT evaluation is supported
Examples
system-properties()?#product-name
Result: 'BaseX'
system-properties()?#xpath-version
Result: 4
sort(map:keys(system-properties()) ! string())
Lists the names of all available properties.

fn:environment-variable

Signature
fn:environment-variable(  $name  as xs:string) as xs:string?
SummaryReturns the value of the environment variable $name, or an empty sequence if no such variable exists.
Examples
environment-variable('PATH')
Returns the search path of the operating system.

fn:available-environment-variables

Signature
fn:available-environment-variables() as xs:string*
SummaryReturns the names of all environment variables.
Examples
sort(available-environment-variables())
Lists the names of all environment variables.

XML Data

fn:parse-xml

Added: trusted option.

Removed: entity-expansion-limit and allow-external-entities options.

Signature
fn:parse-xml(  $value    as (xs:string | xs:hexBinary | xs:base64Binary)?,  $options  as map(*)?  := ()) as document-node(*)?
Summary

Parses a string as XML and returns a document-node() item.

If the $value is supplied as a binary (e.g., xs:base64Binary or xs:hexBinary), its encoding may be inferred from a byte order mark or an XML declaration. The input is then processed like a resource retrieved via fn:doc.

The recognized $options are the same as for fn:doc, with the following differences: the stable option does not apply; a base-uri can be supplied (defaulting to the static base URI of the function call); and xsd-validation does not support the values lax and type.

Examples
parse-xml('<Greeting>Hello, world!</Greeting>')
Result: <Greeting>Hello, world!</Greeting>

fn:parse-xml-fragment

Signature
fn:parse-xml-fragment(  $value    as (xs:string | xs:hexBinary | xs:base64Binary)?,  $options  as map(*)?  := ()) as document-node()?
Summary

This function takes an XML fragment as a string or binary input and returns it as a document node.

If the $value is supplied as binary, its encoding is inferred as with fn:unparsed-text, and it is parsed as an external general parsed entity.

Within $options, the standard options are supported:

optiondefaultdescription
base-uribase-uri of document. Defaults to the static base URI of the function call.
strip-spacefalse()Determines whether whitespace-only text nodes are removed from the resulting document.

Additionally, this BaseX XML Parsing option is supported, using a lower-case option name:

optiondefaultdescription
stripnsfalse()Strips all namespaces from an XML document while parsing.
Examples
parse-xml-fragment('<a/> <b/> <c/>')/node()
Result: (<a/>, text {' '}, <b/>, text {' '}, <c/>)

fn:serialize

Signature
fn:serialize(  $input    as item()*,  $options  as (element(output:serialization-parameters) | map(*))?  := ()) as xs:string
SummaryReturns a string representation of $input. The $options argument contains serialization parameters, which can be supplied…
  1. as a map…
    { "method": "xml", "cdata-section-elements": "div" }
  2. or (for backward compliance) as an element:
    <output:serialization-parameters>
      <output:method value='xml'/>
      <output:cdata-section-elements value='div'/>
    </output:serialization-parameters>
Examples
serialize(1 to 3)
Result: '1 2 3'
serialize(<xml id='1'></xml>)
Result: '<xml id="1"/>'
serialize(<html/>, { 'method': 'html', 'html-version': 5.0 })
Result: '<!DOCTYPE HTML><html></html>'
serialize({ 1: "one" }, { 'method': 'json' })
Result: '{"1":"one"}'

fn:xsd-validator

Added: New function.

Signature
fn:xsd-validator(  $options  as map(*)?  := {}) as fn((document-node() | element() | attribute())?) as fn:validation-result-record?
Summary

Assembles an XSD schema and returns a function that validates a document or element node against this schema. The returned function returns an empty sequence if the supplied node is empty. Otherwise, it returns a record with the field is-valid and, on demand, the validated node (typed-node) and details on the invalidities that were found (error-details).

The following $options are available:

optiondefaultdescription
schema()Schema documents, supplied as element(xs:schema)* nodes.
schema-location()URIs of schema documents. Relative URIs are resolved against the static base URI of the function call.
use-xsi-schema-locationfalse()Retrieve schema documents that are referenced by xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes of the validated node.
trustedAllow access to schema documents that are indirectly referenced (e.g., via xs:include). The default is controlled by the FNXMLTRUSTED option.
xsd-versionRequested XSD version. An error is raised if no processor with this version is available (see Validation Functions).
return-typed-nodetrue()Include the validated node in the result.
return-error-detailsfalse()Include details on invalidities in the result. If the option is disabled, validation stops after the first error.

As BaseX is not schema-aware, the returned node has no type annotations, and the options validation-mode (with the values lax and by-type), type and target-namespace are rejected with an error.

Examples
let $schema := <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:element name='distance' type='xs:decimal'/>
</xs:schema>
let $validator := xsd-validator({ 'schema': $schema })
return (
  $validator(<distance>8.5</distance>)?is-valid,
  $validator(<distance>8.5km</distance>)?is-valid
)
Result: (true(), false())

HTML Data

Strings and binary data can be parsed as HTML to XDM items.

fn:parse-html

Signature
fn:parse-html(  $value    as (xs:string | xs:hexBinary | xs:base64Binary)?,  $options  as map(*)?  := {}) as document-node(*:html)?
SummaryParses the supplied $value as HTML and returns an item representation, using the supplied $options. The result is returned as a document node with a *:html root element. In contrast to other HTML parsing in BaseX, this function by default uses the Validator.nu HTML Parser, but it can be instructed to use TagSoup by supplying option method=tagsoup. See HTML Functions for more information about the available HTML parsers and their options.

With our custom html:parse function, additional conversion formats are available.

Examples
parse-html(
  '<!DOCTYPE html><html><head><meta charset="UTF-8"></head>' ||
  '<body>Hello, World!</body></html>'
)
The result:
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="UTF-8"/>
  </head>
  <body>Hello, World!</body>
</html>

fn:html-doc

Signature
fn:html-doc(  $source   as xs:string?,  $options  as map(*)?  := {}) as document-node(*:html)?
SummaryReads an external resource containing HTML data, retrieved from the $source location, and returns the result of parsing the resource as HTML, using the supplied $options.

The returned result is a document node whose root element is <html>, conforming to the HTML structure parsed from the input. The result is equivalent to reading the file as binary with unparsed-binary and parsing it via parse-html.

For more details and allowable options, see fn:html-doc.

With our custom html:doc function, additional parsing features are available.

Examples
html-doc('https://basex.org/download')//@href[contains(., '.zip')]!string()
Returns the download link for the latest BaseX version.

JSON Data

Strings and resources can be parsed to XDM items and serialized back to their original form.

fn:parse-json

Signature
fn:parse-json(  $value    as xs:string?,  $options  as map(*)?  := {}) as item()?
SummaryParses the supplied $value as JSON and returns an item representation, using the supplied $options. The result may be a map, an array, a string, a double, a boolean, or an empty sequence.

With our custom json:parse function, additional conversion formats are available.

Examples
parse-json('{ "name": "john" }')
Result: { "name": "john" }
parse-json('[ 1, 2, 4, 8, 16 ]')
Result: [ 1, 2, 4, 8, 16 ]

fn:json-doc

Signature
fn:json-doc(  $source   as xs:string?,  $options  as map(*)?  := {}) as item()?
SummaryParses the JSON string retrieved from the $source location and returns an item representation, using the supplied $options.

With our custom json:doc function, additional conversion formats are available.

Examples
json-doc("http://ip.jsontest.com/")?id
Returns your IP address.

fn:json-to-xml

Signature
fn:json-to-xml(  $value    as xs:string?,  $options  as map(*)?  := {}) as document-node(fn:*)?
SummaryParses the supplied $value as a JSON string and returns an XML representation, using the supplied $options.

With our custom json:parse function, additional conversion formats are available.

Examples
json-to-xml('{ "message": "world" }')
The result:
document {
  <map xmlns="http://www.w3.org/2005/xpath-functions">
    <string key="message">world</string>
  </map>
}

fn:xml-to-json

Signature
fn:xml-to-json(  $node     as node()?,  $options  as map(*)?  := {}) as xs:string?
SummaryConverts a $node, whose format conforms to the results created by fn:json-to-xml, to a JSON string, using the supplied $options.

The resulting string can also be created with our custom json:serialize function.

Examples
<map xmlns="http://www.w3.org/2005/xpath-functions">
  <string key="message">world</string>
</map>
=> xml-to-json()
Result: '{"message":"world"}'

CSV Data

Strings and resources can be parsed to XDM items and serialized back to their original form.

fn:csv-to-arrays

Signature
fn:csv-to-arrays(  $value    as xs:string?,  $options  as map(*)?  := {}) as array(xs:string)*
SummaryParses the supplied $value as CSV data and returns an item representation, using the supplied $options. The result is returned as a sequence of arrays of strings. For details, and for the allowable options, see fn:csv-to-arrays.

With our custom csv:parse function, additional conversion formats are available.

Examples
csv-to-arrays(
  string-join(
    ('Name,City', 'Rossi,Rome', 'Da Silva,Rio de Janeiro'),
    char('\n')
  )
)
Result: [ "Name", "City" ], [ "Rossi", "Rome" ], [ "Da Silva", "Rio de Janeiro" ]
csv-to-arrays(
  string-join(('Product;Price', 'Laptop;1200', 'Mouse;25'), char('\n')),
  { 'separator': ';' }
)
Result: [ "Product", "Price" ], [ "Laptop" , "1200" ], [ "Mouse", "25" ]

fn:parse-csv

Signature
fn:parse-csv(  $value    as xs:string?,  $options  as map(*)?  := {}) as fn:parsed-csv-structure-record?
SummaryParses the supplied $value as CSV data and returns an item representation, using the supplied $options. The result is returned as a map with these keys:
  • columns is a sequence of strings containing column names, if any
  • column-index is a map from column names to (one-based) column positions
  • rows is a sequence of arrays of strings representing the parsed rows of the CSV data
  • get is a function that returns a field in the result that is identified by row and column. Its signature is function(xs:positiveInteger, (xs:positiveInteger | xs:string)) as xs:string? where the first argument is the (one-based) row number, and the second argument is the (one-based) column number or the column name.

For more details, and for the allowable options, see fn:parse-csv.

With our custom csv:parse function, additional conversion formats are available.

Examples
parse-csv(
  string-join(
    ('Name,City', 'Rossi,Rome', 'Da Silva,Rio de Janeiro'),
    char('\n')
  ),
  { 'header': true() }
)
The result:
{
  "columns": ("Name", "City"),
  "column-index": { "Name": 1, "City": 2},
  "rows": ([ "Rossi", "Rome" ], [ "Da Silva", "Rio de Janeiro" ]),
  "get": (anonymous-function)#2
}

parse-csv(
  string-join(('Laptop;1200', 'Mouse;25', 'Keyboard;45'), char('\n')),
  { 'separator': ';', 'header': ('Product', 'Price') }
)
The result:
{
  "columns": ("Product", "Price"),
  "column-index": { "Product": 1, "Price": 2},
  "rows": ([ "Laptop", "1200" ], [ "Mouse", "25" ], [ "Keyboard", "45" ]),
  "get": (anonymous-function)#2
}

fn:csv-doc

Signature
fn:csv-doc(  $source   as xs:string?,  $options  as map(*)?  := {}) as fn:parsed-csv-structure-record?
Summary Reads an external resource containing CSV data, and returns the results as a record structure with information about column names and row data, using the supplied $options.

The result is equivalent to reading the CSV file as binary using unparsed-binary and parsing the content with parse-csv. If the $source is the empty sequence, the function returns the empty sequence.

For more details and allowable options, see fn:csv-doc.

With our custom csv:doc function, additional conversion formats are available.

Examples
csv-doc('input.csv', { 'header': true() })
The result:
{
  "columns": ("Name", "Age"),
  "column-index": { "Name": 1, "Age": 2 },
  "rows": ([ "Alice", "30" ], [ "Bob", "25" ]),
  "get": (anonymous-function)#2
}

fn:csv-to-xml

Signature
fn:csv-to-xml(  $value    as xs:string?,  $options  as map(*)?  := {}) as document-node(fn:csv)?
SummaryParses the supplied $value as CSV data and returns an item representation, using the supplied $options. The result is returned as a document node with a csv root element in the http://www.w3.org/2005/xpath-functions namespace. It has these child elements, which are all in the same namespace:
  • columns holds the column names in a sequence of column elements (only present if there are column names)
  • rows holds a sequence of row elements representing the rows of the CSV data. Each row has field elements with the field values. Their column attribute indicates the column name, if any
For more details, and for the allowable options, see fn:csv-to-xml.

With our custom csv:parse function, additional conversion formats are available.

Examples
csv-to-xml(
  string-join(('Laptop;1200', 'Mouse;25', 'Keyboard;45'), char('\n')),
  { 'separator': ';', 'header': ('Product', 'Price') }
)
The result:
<csv xmlns="http://www.w3.org/2005/xpath-functions">
  <columns>
    <column>Product</column>
    <column>Price</column>
  </columns>
  <rows>
    <row>
      <field column="Product">Laptop</field>
      <field column="Price">1200</field>
    </row>
    <row>
      <field column="Product">Mouse</field>
      <field column="Price">25</field>
    </row>
    <row>
      <field column="Product">Keyboard</field>
      <field column="Price">45</field>
    </row>
  </rows>
</csv>

Invisible XML

A separate page is available on Invisible XML and how to use it in XQuery.

fn:invisible-xml

Signature
fn:invisible-xml(  $grammar  as (xs:string | element(ixml))?,  $options  as map(*)?  := {}) as fn($value as xs:string) as document-node()
SummaryGenerates a parser for the supplied Invisible XML $grammar and returns it as a function. The returned function converts an input string to an XML document. See Invisible XML for a full description.
Examples
let $string := 'greeting: "Hello", " ", name. name: ["A"-"Z"], ["a"-"z"]+.'
return invisible-xml($string)('Hello World')
Result: <greeting>Hello <name>World</name></greeting>

Dynamic Evaluation

fn:load-xquery-module

Signature
fn:load-xquery-module(  $module-uri  as xs:string,  $options     as map(*)?  := {}) as fn:load-xquery-module-record
Summary

Loads an XQuery library module by its namespace URI and returns a map of its public functions and global variables.

Within $options, the following keys can be specified:

optiondefaultdescription
location-hints()URI(s) of the module.
content()Literal string content of the module. If supplied, location-hints are ignored.
context-item()Initial context item for evaluating global variables.
variables{}Map of external variable bindings.
vendor-options{}Ignored by BaseX.
xquery-versionSpecifies the minimum XQuery version; must be supported by BaseX. Default: version declared in the module, otherwise unspecified.
Examples
let $module := '
  module namespace m = "http://ex.org";
  declare %public function m:two() { 2 };'
let $functions := load-xquery-module(
  'http://ex.org',
  { 'content': $module }
)?functions
let $two := $functions(QName('http://ex.org', 'two'))?0
return $two()
Result: 2
let $uri := 'http://basex.org/modules/code'
let $functions := load-xquery-module(
  $uri,
  { 'location-hints': 'code.xqm' }
)?functions
let $run := $functions(QName($uri, 'run'))(0)
return $run()
Parses code.xqm and evaluates a zero-arity function named run.

fn:transform

Added: New function.

Signature
fn:transform(  $options  as map(*)) as map(*)
Summary

Invokes an XSLT transformation and returns a map with the result documents. The key of the principal result document is the base output URI, or the string output; secondary result documents, generated by xsl:result-document, are returned under their own output URI. See XSLT Functions for the processors that can be used.

The following $options are available:

optiondefaultdescription
stylesheet-locationURI of the stylesheet. Relative URIs are resolved against the static base URI of the function call.
stylesheet-nodeStylesheet, supplied as document or element node.
stylesheet-textStylesheet, supplied as string. Exactly one of the three stylesheet options must be specified.
stylesheet-base-uriStatic base URI of the stylesheet.
stylesheet-params{}Map with stylesheet parameters. The keys are QNames.
source-locationURI of the source document.
source-nodeSource document, supplied as node. Exactly one of the two source options must be specified.
base-output-uriURI of the principal result document.
delivery-formatdocumentResult format: document (document node), serialized (string), raw (the result of the transformation, converted to XQuery items) or file (the result is written to the base output URI, and the returned value is an empty sequence).
serialization-params{}Serialization parameters for the principal result document. Parameters that are unknown to the XSLT processor are ignored; parameters with map or array values, such as use-character-maps, are rejected. As document results are built directly, the option only applies to the delivery formats serialized and file.
post-processFunction that is applied to the key and the value of the result before it is returned.
cachetrue()Cache the compiled stylesheet. Only applies to stylesheets that are supplied by location.
trustedAllow the stylesheet to access external resources (e.g., via xsl:include or document). The default is controlled by the FNXMLTRUSTED option.
xslt-versionRequested XSLT version. An error is raised if no processor with this version is available.

If Saxon is available, it is addressed via its s9api interface, and all options of the specification are supported. Otherwise, transformations are performed via Java’s JAXP interface: the options that cannot be assigned via this interface (initial-template, initial-mode, initial-function, global-context-item, initial-match-selection, static-params, template-params, tunnel-params, the package-* options) are rejected with an error, as is the delivery format raw, and secondary result documents are lost.

The options enable-trace and vendor-options are accepted and ignored, as are enable-assertions and enable-messages if the JAXP interface is used. Requested properties other than xsl:version are always rejected.

Examples
transform({
  'stylesheet-text':
    "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
      version='1.0'><xsl:template match='/'><out><xsl:value-of
      select='//b'/></out></xsl:template></xsl:stylesheet>",
  'source-node': <a><b>89</b></a>
})?output
Result: <out>89</out>

fn:op

Signature
fn:op(  $operator  as xs:string) as fn($op1 as item()*, $op2 as item()*) as item()*
SummaryReturns a new function that applies the specified $operator to two arguments. The supported operators are:
+ * - | || < <= = >= > != << >> is mod div idiv and or lt le eq ge gt ne to union intersect except otherwise
Examples
for-each-pair(1 to 3, 4  to 6, op('+'))
Result: 5, 7, 9
map:keys(map:filter(
  { 2: 1234, 3: 3, 4: 5678, 5: 5 },
  op('=')
))
Result: 3, 5. Keeps the entries whose value equals the key, and returns those keys.

Types

fn:schema-type

Added: New function.

Signature
fn:schema-type(  $name  as xs:QName) as fn:schema-type-record?
SummaryReturns a fn:schema-type-record with information about the schema type named $name, with the fields described for fn:atomic-type-annotation. An empty sequence is returned if no such type is known; BaseX provides the built-in XSD types.
Examples
schema-type(xs:QName('xs:integer'))?variety
Result: 'atomic'
schema-type(xs:QName('xs:integer'))?base-type()?name => string()
Result: 'decimal'
schema-type(xs:QName('xs:positiveInteger'))?constructor('42')
    instance of xs:positiveInteger
Result: true()
empty(schema-type(xs:QName('unknown-type')))
Result: true()

fn:type-of

Signature
fn:type-of(  $value  as item()*) as xs:string
SummaryReturns a string representation of the type of $value. The function is similar to inspect:type.
Examples
type-of('Hello')
Result: 'xs:string'
type-of(1 to 10)
Result: 'xs:integer+'
type-of((1, 'a'))
Result: '(xs:integer|xs:string)+'
type-of(())
Result: 'empty-sequence()'

fn:atomic-type-annotation

Added: New function.

Signature
fn:atomic-type-annotation(  $value  as xs:anyAtomicType) as fn:schema-type-record
SummaryReturns a record with information about the type annotation of $value. The record has the following fields:
  • name: QName of the type
  • is-simple: indicates whether it is a simple type
  • base-type: function returning the annotation of the base type
  • primitive-type: function returning the annotation of the primitive type
  • variety: variety of the type (atomic, list, union or mixed)
  • members: function returning the annotations of the member types
  • simple-content-type: function returning the annotation of the simple content type
  • matches: function testing whether an atomic value is an instance of the type
  • constructor: function constructing a value of the type
Examples
atomic-type-annotation(1e0)?variety
Result: 'atomic'
atomic-type-annotation(xs:byte(1))?base-type()?name => string()
Result: 'short'
atomic-type-annotation(1)?matches(2)
Result: true()
atomic-type-annotation(xs:byte(1))?constructor('127') instance of xs:byte
Result: true()

fn:node-type-annotation

Added: New function.

Signature
fn:node-type-annotation(  $node  as (element() | attribute())) as fn:schema-type-record
SummaryReturns a fn:schema-type-record with information about the type annotation of an element or attribute node, with the fields described for fn:atomic-type-annotation. As BaseX is not schema-aware, elements are annotated as xs:untyped and attributes as xs:untypedAtomic.
Examples
node-type-annotation(<a/>)?name => string()
Result: 'untyped'
node-type-annotation(attribute id { 1 })?name => string()
Result: 'untypedAtomic'
node-type-annotation(<a/>)?variety
Result: 'mixed'

Context

fn:current-date

Signature
fn:current-date() as xs:date
SummaryReturns the current date, including a timezone. All calls within a single query return the same value.
Examples
current-date() eq current-date()
Result: true(). The value is stable throughout the query.

fn:current-dateTime

Signature
fn:current-dateTime() as xs:dateTimeStamp
SummaryReturns the current date and time, including a timezone. All calls within a single query return the same value.
Examples
current-dateTime() eq current-dateTime()
Result: true(). The value is stable throughout the query.

fn:current-time

Signature
fn:current-time() as xs:time
SummaryReturns the current time, including a timezone. All calls within a single query return the same value.
Examples
current-time() eq current-time()
Result: true(). The value is stable throughout the query.

fn:default-collation

Signature
fn:default-collation() as xs:string
SummaryReturns the default collation of the query.
Examples
default-collation()
Result: 'http://www.w3.org/2005/xpath-functions/collation/codepoint'

fn:default-language

Signature
fn:default-language() as xs:language
SummaryReturns the default language used for formatting numbers and dates. BaseX always returns en.

fn:implicit-timezone

Signature
fn:implicit-timezone() as xs:dayTimeDuration
SummaryReturns the timezone of the system on which the query is run. It is used whenever a date, time or dateTime value without an explicit timezone needs to be compared or adjusted.
Examples
implicit-timezone()
Returns e.g. PT2H for Central European Summer Time.

fn:last

Signature
fn:last() as xs:integer
SummaryReturns the number of items in the sequence that is currently being processed.
Examples
('Kafka', 'Camus', 'Tawada')[last()]
Result: 'Tawada'

fn:position

Signature
fn:position() as xs:integer
SummaryReturns the position of the context item within the sequence that is currently being processed.
Examples
(1 to 5)[position() gt 3]
Result: 4, 5

fn:static-base-uri

Signature
fn:static-base-uri() as xs:anyURI?
SummaryReturns the static base URI of the query, or an empty sequence if no base URI is available.
Examples
static-base-uri()
Returns the URI of the executed query file.

Errors and Diagnostics

Raising Errors

fn:error

Signature
fn:error(  $code         as xs:QName?  := (),  $description  as xs:string?  := (),  $value        as item()*  := ()) as xs:error
SummaryRaises an error with the supplied $code, $description and $value. If no $code is supplied, FOER0000 is used.
Examples
error(xs:QName('err:FORG0001'), 'Wrong value')
Raises FORG0001 with the supplied description.

Tracing

fn:trace

Signature
fn:trace(  $input  as item()*,  $label  as xs:string?  := ()) as item()*
SummaryGenerates a serialized representation of $input, optionally prefixed with $label, and outputs it (see Debugging). In contrast to fn:message, the evaluated result is returned unchanged.

fn:message

Signature
fn:message(  $input  as item()*,  $label  as xs:string?  := ()) as empty-sequence()
SummaryGenerates a serialized representation of $input, optionally prefixed with $label, and outputs it (see Debugging). The function itself returns an empty sequence. In contrast to fn:trace, the evaluated result will be swallowed.
Examples
'Hello' => trace() => void()
Results can also be output and swallowed with fn:trace and fn:void.

Changelog

Version 13.0Version 12.0Version 11.0

⚡Generated with XQuery