Utility Module

From BaseX Documentation
Jump to navigation Jump to search

This XQuery Module contains various small utility and helper functions. Please note that some of the functions are used for internal query rewritings. They may be renamed or moved to other modules in future versions of BaseX.


All functions and errors in this module and errors are assigned to the http://basex.org/modules/util namespace, which is statically bound to the util prefix.



Signatures util:if($condition as item()*, $then as item()*) as item()*
util:if($condition as item()*, $then as item()*, $else as item()*) as item()*
Summary Alternative writing for the if/then/else expression:
  • If the effective boolean value of $condition is true, the $then branch will be evaluated.
  • Otherwise, $else will be evaluated. If no third argument is supplied, an empty sequence will be returned.
  • util:if(true(), 123, 456) returns 123.
  • util:if(0, 'wrong!') returns an empty sequence.


Signatures util:or($items as item()*, $default as item()*) as item()*
Summary Returns $items if it is a non-empty sequence. Otherwise, returns $default. The function is equivalent to one of the following expressions:
  • if(exists($items)) then $items else $default
  • $items ?: $default (see Elvis Operator for more details)
  • util:or(123, 456) returns 123.
  • util:or(1[. = 0], -1) returns -1.


Introduced with Version 9.5:

Signatures util:within($sequence as item()*, $min as xs:integer) as xs:boolean
util:within($sequence as item()*, $min as xs:integer, $max as xs:integer) as xs:boolean
Summary Checks if the specified $sequence has at least $min and, optionally, at most $max items. Equivalent to let $count := count($sequence) return $count >= $min and $count <= $max.
  • util:within(('a', 'b', 'c'), 2) returns true.
  • util:within((1 to 1000000000)[. < 10], 3, 6) returns true.

Positional Access[edit]


Signatures util:item($sequence as item()*, $position as xs:double) as item()?
Summary Returns the item from $sequence at the specified $position. Equivalent to $sequence[$position].
  • util:item(reverse(1 to 5), 1) returns 5.
  • util:item(('a','b'), 0) returns an empty sequence.


Signatures util:range($sequence as item()*, $first as xs:double, $last as xs:double) as item()*
Summary Returns items from $sequence, starting at position $first and ending at $last. Equivalent to subsequence($sequence, $first, $last - $first + 1).
  • util:range(//item, 11, 20) returns all path results from (if available) position 11 to 20.


Signatures util:last($sequence as item()*) as item()?
Summary Returns last item of a $sequence. Equivalent to $sequence[last()].
  • util:last(reverse(1 to 100)) returns 1.


Signatures util:init($sequence as item()*) as item()*
Summary Returns all items of a $sequence except for the last one. Equivalent to $sequence[position() < last()].
  • util:init(1 to 4) returns 1 2 3.

Node Functions[edit]


Signatures util:ddo($nodes as node()*) as node()*
Summary Returns nodes in distinct document order: duplicate nodes will be removed, and the remaining nodes will be returned in document order. All results of path expression are in distinct document order, so the function is equivalent to the expression $nodes/self::node().
Signatures util:root($nodes as node()*) as document-node()*
Summary Returns the document nodes of the specified $nodes. The function is equivalent to the expression $nodes ! /. The path expression /abc

is internally represented as util:root(.)/abc</code.

Helper Functions[edit]


Updated with Version 9.5: Third argument added.

Signatures util:replicate($input as item()*, $count as xs:integer) as item()*
util:replicate($input as item()*, $count as xs:integer, $multiple as xs:boolean) as item()*
Summary Evaluates $input and returns the result $count times. Unless $multiple is enabled, the input expression is only evaluated once. The function call util:replicate($input, $count, true()) is equivalent to (1 to $count) ! $input.
Errors negative: The specified number is negative.
  • util:replicate('A', 3) returns A A A.
  • In the following query, a single new element node is constructed, and true is returned:
let $nodes := util:replicate(<node/>, 2)
return $nodes[1] is $nodes[2]
  • In this query, two nodes are constructed, and the result is false:
let $nodes := util:replicate(<node/>, 2, true())
return $nodes[1] is $nodes[2]


Introduced with Version 9.5.

Signatures util:intersperse($items as item()*, $separator as item()*) as item()*
Summary Inserts the defined $separator between the $items of a sequence and returns the resulting sequence. The function is equivalent to

head($items), for $item in tail($items) return ($separator, $item)

Examples Inserts semicolon strings between the three input items:
fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), '; ')


Introduced with Version 9.5.

Signatures util:duplicates($sequence as item()*) as xs:anyAtomicType*
util:duplicates($sequence as item()*, $collation as xs:string) as xs:anyAtomicType*
Summary Returns duplicate values in a $sequence. See fn:distinct-values for the applied equality rules and the usage of the $collation argument.
  • util:duplicates((1, 2, 1, 1)) returns 1.


Signatures util:chars($string as xs:string) as xs:string*
Summary Returns all characters of a $string as a sequence. Equivalent to string-to-codepoints($string) ! codepoints-to-string(.).
  • util:chars('AB') returns the two strings A and B.


Code Description
negative The specified number is negative.


Version 9.5
Version 9.4
Version 9.3
Version 9.2
Version 9.1
Version 9.0

The Module was introduced with Version 8.5.