Utility Module

From BaseX Documentation
Revision as of 17:16, 9 March 2023 by CG (talk | contribs)
Jump to navigation Jump to search

This XQuery Module contains various utility and helper functions.

For all listed functions, equivalent expressions exist in standard XQuery, but code may be better readable with function calls:

<syntaxhighlight lang="xquery"> (: standard XQuery :) let $result := if(exists($sequence)) then $sequence else ('default', 'values') return $result[last()]

(: XQuery with functions of this module :) $sequence => util:or(('default', 'values')) => util:last() </syntaxhighlight>

In addition, various query optimizations create calls to the utility functions.

Conventions

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.

Conditions

util:if

Signature
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.
Examples
  • util:if(true(), 123, 456) returns 123.
  • util:if(0, 'wrong!') returns an empty sequence.

util:or

Signature
util:or(
  $items    as item()*,
  $default  as item()*
) as item()*
Summary Returns $items if it is a non-empty sequence. Otherwise, returns $default. Equivalent to the following expressions:

<syntaxhighlight lang="xquery"> if(exists($items)) then $items else $default, (: Elvis operator :) $items ?: $default </syntaxhighlight>

Examples
  • util:or(123, 456) returns 123.
  • util:or(1[. = 0], -1) returns -1.

util:count-within

Updated with BaseX 10: Renamed from util:within

Signature
util:count-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:

<syntaxhighlight lang="xquery"> let $count := count($sequence) return $count >= $min and $count <= $max </syntaxhighlight>

Examples
  • util:count-within(('a', 'b', 'c'), 2) returns true.
  • util:count-within((1 to 1000000000)[. < 10], 3, 6) returns true.

Positional Access

util:item

Signature
util:item(
  $sequence  as item()*,
  $position  as xs:double
) as item()?
Summary Returns the item from $sequence at the specified $position. Equivalent to:

<syntaxhighlight lang="xquery"> $sequence[$position] </syntaxhighlight>

Examples
  • util:item(reverse(1 to 5), 1) returns 5.
  • util:item(('a','b'), 0) returns an empty sequence.

util:range

Signature
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:

<syntaxhighlight lang="xquery"> subsequence($sequence, $first, $last - $first + 1) </syntaxhighlight>

Examples
  • util:range(//item, 11, 20) returns all path results from (if available) position 11 to 20.

util:last

Signature
util:last(
  $sequence  as item()*
) as item()?
Summary Returns last item of a $sequence. Equivalent to:

<syntaxhighlight lang="xquery"> $sequence[last()] </syntaxhighlight>

Examples
  • util:last(reverse(1 to 100)) returns 1.

util:init

Signature
util:init(
  $sequence  as item()*
) as item()*
Summary Returns all items of a $sequence except for the last one. Equivalent to:

<syntaxhighlight lang="xquery"> $sequence[position() < last()] </syntaxhighlight>

Examples
  • util:init(1 to 4) returns 1 2 3.

Node Functions

util:ddo

Signature
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. As results of path expressions are brought distinct document order before they are returned, the function is equivalent to:

<syntaxhighlight lang="xquery"> $nodes/self::node() </syntaxhighlight>

util:root

Signature
util:root(
  $nodes  as node()*
) as document-node()*
Summary Returns the document nodes of the specified $nodes. The path expression /abc is internally represented as util:root(.)/abc. Equivalent to:

<syntaxhighlight lang="xquery"> $nodes ! / </syntaxhighlight>

util:strip-namespaces

Signature
util:strip-namespaces(
  $node      as node(),
  $prefixes  as xs:string*  := ()
) as node()
Summary Removes namespaces with the specified $prefixes from the supplied $node. An empty string can be supplied to remove the default namespace. If no prefixes are specified, all namespaces will be removed.
Examples
  • Remove all namespaces from an element and its descendants:

<syntaxhighlight lang="xquery"> util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)

(: yields :) <xml name='value'><child/></xml> </syntaxhighlight>

  • Remove all default namespaces:

<syntaxhighlight lang="xquery"> <xml xmlns='uri1'><child xmlns='uri2'/></xml> => util:strip-namespaces() </syntaxhighlight>

Array and Map Functions

util:array-members

Signature
util:array-members(
  $array  as array(*)
) as array(*)*
Summary Returns each member of an $array as a new array. Equivalent to:

<syntaxhighlight lang="xquery"> for $a in 1 to array:size($array) return [ $array($a) ] </syntaxhighlight>

Examples
  • Returns three elements with the member values as concatenated text node.

<syntaxhighlight lang="xquery"> let $array := [ (), 2, (3, 4) ] for $member in util:array-members($array) return element numbers { $member } </syntaxhighlight>

util:array-values

Signature
util:array-values(
  $array  as array(*)
) as item()*
Summary Returns all members of an $array as a sequence. Equivalent to:

<syntaxhighlight lang="xquery"> $array ? * </syntaxhighlight>

Examples
  • Returns the array members as two items:

<syntaxhighlight lang="xquery"> let $array := [ (), 2, [ 3, 4 ] ] return util:array-values($array) </syntaxhighlight>

util:map-entries

Signature
util:map-entries(
  $map  as map(*)
) as map(xs:string, item()*)*
Summary Returns each entry of a $map as a new map, each with a key and value entry. Equivalent to:

<syntaxhighlight lang="xquery"> map:for-each($map, function($key, $value) {

 map { "key": $key, "value": $value }

}) </syntaxhighlight>

Examples
  • Returns three elements named by the key of the map, and with the entries as concatenated text node.

<syntaxhighlight lang="xquery"> let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] } for $entry in util:map-entries($map) return element { $entry?key } { string-join($entry?value) } </syntaxhighlight>

util:map-values

Signature
util:map-values(
  $map  as map(*)
) as item()*
Summary Returns all values of a $map as a sequence. Equivalent to:

<syntaxhighlight lang="xquery"> $map ? * </syntaxhighlight>

Examples
  • Returns the map values as two items:

<syntaxhighlight lang="xquery"> let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] } return util:map-values($map) </syntaxhighlight>

Helper Functions

util:replicate

Signature
util:replicate(
  $input   as item()*,
  $count   as xs:integer,
  $repeat  as xs:boolean?  := false()
) as item()*
Summary Evaluates $input and returns the result $count times. Unless $repeat is set to true, the input expression is evaluated multiple times. Equivalent expressions:

<syntaxhighlight lang="xquery"> util:replicate($input, $count, true()), (1 to $count) ! $input </syntaxhighlight>

Errors negative: The specified number is negative.
Examples
  • util:replicate('A', 3) returns A A A.
  • In the following query, a single new element node is constructed, and true is returned:

<syntaxhighlight lang="xquery"> let $nodes := util:replicate(<node/>, 2) return $nodes[1] is $nodes[2] </syntaxhighlight>

  • In this query, two nodes are constructed, and the result is false:

<syntaxhighlight lang="xquery"> let $nodes := util:replicate(<node/>, 2, true()) return $nodes[1] is $nodes[2] </syntaxhighlight>

util:intersperse

Signature
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. Equivalent to:

<syntaxhighlight lang="xquery"> head($items), for $item in tail($items) return ($separator, $item) </syntaxhighlight>

Examples Inserts semicolon strings between the three input items:

<syntaxhighlight lang="xquery"> fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), '; ') </syntaxhighlight>

util:duplicates

Signature
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.
Examples
  • util:duplicates((1, 2, 1, 1)) returns 1.

util:chars

Signature
util:chars(
  $string  as xs:string?
) as xs:string*
Summary Returns all characters of a $string as a sequence. Equivalent to:

<syntaxhighlight lang="xquery"> for $cp in string-to-codepoints($string) return codepoints-to-string($cp) </syntaxhighlight>

Examples
  • util:chars('AB') returns the two strings A and B.

Errors

Code Description
negative The specified number is negative.

Changelog

Version 9.7
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.