Difference between revisions of "Utility Module"

From BaseX Documentation
Jump to navigation Jump to search
(25 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
All functions and errors in this module and errors are assigned to the <code><nowiki>http://basex.org/modules/util</nowiki></code> namespace, which is statically bound to the {{Code|util}} prefix.<br/>
 
All functions and errors in this module and errors are assigned to the <code><nowiki>http://basex.org/modules/util</nowiki></code> namespace, which is statically bound to the {{Code|util}} prefix.<br/>
  
=Functions=
+
=Conditions=
  
==util:item-at==
+
==util:if==
  
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|util:item-at|$sequence as item()*, $position as xs:double|item()?}}<br/>
+
|{{Func|util:if|$condition as item()*, $then as item()*|item()*}}<br/>{{Func|util:if|$condition as item()*, $then as item()*, $else as item()*|item()*}}<br/>
 +
|-
 +
| '''Summary'''
 +
|Alternative writing for the if/then/else expression:
 +
* If the ''effective boolean value'' of {{Code|$condition}} is true, the {{Code|$then}} branch will be evaluated.
 +
* Otherwise, {{Code|$else}} will be evaluated. If no third argument is supplied, an empty sequence will be returned.
 +
|-
 +
| '''Examples'''
 +
|
 +
* <code>util:if(true(), 123, 456)</code> returns {{Code|123}}.
 +
* <code>util:if(0, 'wrong!')</code> returns an empty sequence.
 +
|}
 +
 
 +
==util:or==
 +
 
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|util:or|$items as item()*, $default as item()*|item()*}}
 +
|-
 +
| '''Summary'''
 +
|Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. The function is equivalent to one of the following expressions:
 +
* <code>if(exists($items)) then $items else $default</code>
 +
* <code>$items ?: $default</code> (see [[XQuery Extensions#Elvis Operator|Elvis Operator]] for more details)
 +
|-
 +
| '''Examples'''
 +
|
 +
* <code>util:or(123, 456)</code> returns {{Code|123}}.
 +
* <code>util:or(1[. = 0], -1)</code> returns {{Code|-1}}.
 +
|}
 +
 
 +
=Positional Access=
 +
 
 +
==util:item==
 +
 
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|util:item|$sequence as item()*, $position as xs:double|item()?}}<br/>
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
Line 19: Line 57:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* <code>util:item-at(reverse(1 to 5), 1)</code> returns <code>5</code>.
+
* <code>util:item(reverse(1 to 5), 1)</code> returns <code>5</code>.
* <code>util:item-at(('a','b'), 0)</code> returns an empty sequence.
+
* <code>util:item(('a','b'), 0)</code> returns an empty sequence.
 
|}
 
|}
  
==util:item-range==
+
==util:range==
  
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|util:item-range|$sequence as item()*, $first as xs:double, $last as xs:double|item()*}}<br/>
+
|{{Func|util:range|$sequence as item()*, $first as xs:double, $last as xs:double|item()*}}<br/>
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
Line 35: Line 73:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* <code>util:item-range(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.
+
* <code>util:range(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.
 
|}
 
|}
  
==util:last-from==
+
==util:last==
  
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|util:last-from|$sequence as item()*|item()?}}<br/>
+
|{{Func|util:last|$sequence as item()*|item()?}}<br/>
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
Line 50: Line 88:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* <code>util:last-from(reverse(1 to 100))</code> returns <code>1</code>.
+
* <code>util:last(reverse(1 to 100))</code> returns <code>1</code>.
 
|}
 
|}
 +
 +
==util:init==
 +
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|util:init|$sequence as item()*|item()*}}<br/>
 +
|-
 +
| '''Summary'''
 +
|Returns all items of a {{Code|$sequence}} except for the last one. Equivalent to <code>$sequence[position() < last()]</code>.
 +
|-
 +
| '''Examples'''
 +
|
 +
* <code>util:init(1 to 4)</code> returns <code>1 2 3</code>.
 +
|}
 +
 +
=Node Functions=
 +
 +
==util:ddo==
 +
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|util:ddo|$nodes as node()*|node()*}}<br/>
 +
|-
 +
| '''Summary'''
 +
|Returns nodes in ''distinct document order'': duplicate nodes will be removed, and the remaining nodes will be returned in [https://www.w3.org/TR/xquery-31/#dt-document-order document order]. All results of path expression are in distinct document order, so the function is equivalent to the expression <code>$nodes/self::node()</code>.
 +
|}
 +
 +
{{Mark|Introduced with Version 9.4:}}
 +
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|util:root|$nodes as node()*|document-node()*}}<br/>
 +
|-
 +
| '''Summary'''
 +
|Returns the document nodes of the specified {{Code|$nodes}}. The function is equivalent to the expression <code>$nodes ! /</code>. The path expression <code>/abc</code>
 +
is internally represented as <code>util:root(.)/abc</code.
 +
 +
|}
 +
 +
=Helper Functions=
  
 
==util:replicate==
 
==util:replicate==
 
{{Mark|Introduced with Version 9.0}}.
 
  
 
{| width='100%'
 
{| width='100%'
Line 63: Line 142:
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns {{Code|$count}} instances of the specified {{Code|$sequence}}. Equivalent to <code>(1 to $count) ! $sequence</code>.
+
|Returns {{Code|$count}} instances of the specified {{Code|$sequence}}. A similar result can be generated with <code>(1 to $count) ! $sequence</code>, but in the latter case, the right-hand expression will be evaluated multiple times.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 71: Line 150:
 
|
 
|
 
* <code>util:replicate('A', 3)</code> returns <code>A A A</code>.
 
* <code>util:replicate('A', 3)</code> returns <code>A A A</code>.
 +
|}
 +
 +
==util:chars==
 +
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|util:chars|$string as xs:string|xs:string*}}<br/>
 +
|-
 +
| '''Summary'''
 +
|Returns all characters of a {{Code|$string}} as a sequence. Equivalent to <code>string-to-codepoints($string) ! codepoints-to-string(.)</code>.
 +
|-
 +
| '''Examples'''
 +
|
 +
* <code>util:chars('AB')</code> returns the two strings <code>A</code> and <code>B</code>.
 
|}
 
|}
  
Line 84: Line 178:
  
 
=Changelog=
 
=Changelog=
 +
 +
;Version 9.4
 +
* Added: [[#util:root|util:root]]
 +
 +
;Version 9.3
 +
* Added: [[#util:ddo|util:ddo]]
 +
 +
;Version 9.2
 +
* Added: [[#util:chars|util:chars]], [[#util:init|util:init]]
 +
* Updates: [[#util:item|util:item]], [[#util:last|util:last]], [[#util:range|util:range]] renamed (before: {{Code|util:item-at}}, {{Code|util:item-range}}, {{Code|util:last-from}})
 +
 +
;Version 9.1
 +
* Added: [[#util:if|util:if]], [[#util:or|util:or]]
  
 
;Version 9.0
 
;Version 9.0

Revision as of 11:50, 8 July 2020

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.

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

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

util:or

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)
Examples
  • util:or(123, 456) returns 123.
  • util:or(1[. = 0], -1) returns -1.

Positional Access

util:item

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].
Examples
  • util:item(reverse(1 to 5), 1) returns 5.
  • util:item(('a','b'), 0) returns an empty sequence.

util:range

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).
Examples
  • util:range(//item, 11, 20) returns all path results from (if available) position 11 to 20.

util:last

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

util:init

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()].
Examples
  • util:init(1 to 4) returns 1 2 3.

Node Functions

util:ddo

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().

Template:Mark

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

util:replicate

Signatures util:replicate($sequence as item()*, $count as xs:integer) as item()*
Summary Returns $count instances of the specified $sequence. A similar result can be generated with (1 to $count) ! $sequence, but in the latter case, the right-hand expression will be evaluated multiple times.
Errors negative: The specified number is negative.
Examples
  • util:replicate('A', 3) returns A A A.

util:chars

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(.).
Examples
  • util:chars('AB') returns the two strings A and B.

Errors

Code Description
negative The specified number is negative.

Changelog

Version 9.4
Version 9.3
Version 9.2
Version 9.1
Version 9.0

The Module was introduced with Version 8.5.