Difference between revisions of "Utility Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 19: Line 19:
 
| {{Code|util:duplicates}}
 
| {{Code|util:duplicates}}
 
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-duplicate-values <code>fn:duplicate-values</code>]
 
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-duplicate-values <code>fn:duplicate-values</code>]
|- valign="top"
 
| {{Code|util:if}}
 
| <code>if($test) { $then } else { $else }</code> (with the new syntax, the else branch is optional)
 
 
|- valign="top"
 
|- valign="top"
 
| {{Code|util:init}}
 
| {{Code|util:init}}
Line 52: Line 49:
 
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/>
  
=Range Conditions=
+
=Conditions and Ranges=
 +
 
 +
==util:if==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>util:if(
 +
  $condition  as item()*,
 +
  $then      as item()*,
 +
  $else      as item()*  := ()
 +
) as item()*</pre>
 +
|- valign="top"
 +
| '''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 the third argument is omitted, an empty sequence will be returned.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* <code>util:if(true(), 123, 456)</code> returns {{Code|123}}.
 +
* <code>util:if(0, 'wrong!')</code> returns an empty sequence.
 +
|}
  
 
==util:count-within==
 
==util:count-within==
Line 183: Line 202:
  
 
;Version 9.5
 
;Version 9.5
* Added: {{Function||util:intersperse}}, {{Function||util:within}}, {{Function||util:duplicates}}, {{Function||util:array-members}}, {{Function||util:array-values}}, {{Function||util:map-entries}}, {{Function||util:map-values}}
+
* Added: {{Code|util:intersperse}}, {{Code|util:within}}, {{Code|util:duplicates}}, {{Code|util:array-members}}, {{Code|util:array-values}}, {{Code|util:map-entries}}, {{Code|util:map-values}}
* Updated: {{Function||util:replicate}}: Third argument added.
+
* Updated: {{Code|util:replicate}}: Third argument added.
  
 
;Version 9.4
 
;Version 9.4
Line 193: Line 212:
  
 
;Version 9.2
 
;Version 9.2
* Added: {{Function||util:chars}}, {{Function||util:init}}
+
* Added: {{Code|util:chars}}, {{Code|util:init}}
* Updated: {{Function||util:item}}, {{Function||util:last}}, {{Function||util:range}} renamed (before: {{Code|util:item-at}}, {{Code|util:item-range}}, {{Code|util:last-from}})
+
* Updated: {{Code|util:item}}, {{Code|util:last}}, {{Function||util:range}} renamed (before: {{Code|util:item-at}}, {{Code|util:item-range}}, {{Code|util:last-from}})
  
 
;Version 9.1
 
;Version 9.1
* Added: {{Function||util:if}}, {{Function||util:or}}
+
* Added: {{Function||util:if}}, {{Code|util:or}}
  
 
;Version 9.0
 
;Version 9.0
* Added: {{Function||util:replicate}}
+
* Added: {{Code|util:replicate}}
  
 
The Module was introduced with Version 8.5.
 
The Module was introduced with Version 8.5.

Revision as of 13:15, 23 November 2023

This XQuery Module contains some utility and helper functions.

With Version 11, many functions have been removed in favor of new features of XQuery 4:

BaseX 10 XQuery 4
util:array-members array:members
util:array-values array:values
util:chars fn:characters
util:duplicates fn:duplicate-values
util:init fn:trunk
util:intersperse fn:intersperse
util:item fn:items-at
util:last fn:foot
util:map-entries map:pairs
util:map-values map:values
util:or $expr1 otherwise $expr2
util:replicate fn:replicate

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 and Ranges

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 the third argument is omitted, an empty sequence will be returned.
Examples
  • util:if(true(), 123, 456) returns 123.
  • util:if(0, 'wrong!') returns an empty sequence.

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

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.

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>

Errors

Code Description
negative The specified number is negative.

Changelog

Version 11.0
  • Removed: util:array-members, util:array-values, util:chars, util:duplicates, util:init, util:intersperse, util:item, util:last, util:map-entries, util:map-values, util:replicate
Version 9.7
Version 9.5
  • Added: util:intersperse, util:within, util:duplicates, util:array-members, util:array-values, util:map-entries, util:map-values
  • Updated: util:replicate: Third argument added.
Version 9.4
Version 9.3
Version 9.2
  • Added: util:chars, util:init
  • Updated: util:item, util:last, util:range renamed (before: util:item-at, util:item-range, util:last-from)
Version 9.1
Version 9.0
  • Added: util:replicate

The Module was introduced with Version 8.5.