Changes

Jump to navigation Jump to search
3,485 bytes added ,  12:50, 8 July 2020
no edit summary
=Conventions=
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/>
=Function ItemsConditions=
==util:function-annotationsif==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:function-annotationsif|$function condition as functionitem()*, $then as item()?*|mapitem(xs)*}}<br/>{{Func|util:QNameif|$condition as item()*, xs:anyAtomicType$then as item()*, $else as item()*|item()*}}<br/>
|-
| '''Summary'''
|Returns Alternative writing for the annotations if/then/else expression:* If the ''effective boolean value'' of {{Code|$condition}} is true, the specified {{Code|$functionthen}} branch will be evaluated.* Otherwise, {{Code|$else}} in a mapwill be evaluated. If no third argument is supplied, an empty sequence will be returned.
|-
| '''Examples'''
|
* Returns an empty map:<pre class="brush:xquery"code>util:function-annotationsif(true#0(), 123, 456)</pre>* Returns a map with a single key <code><nowiki>Qreturns {{http://wwwCode|123}}.w3.org/2012/xquery}private* </nowiki></code> and an empty sequence as valueutil:<pre class="brush:xquery">declare %private function local:fif() { 0, 'well hiddenwrong!' };util:function-annotations(local:f#0)</precode>returns an empty sequence.
|}
=Sequences=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-atempty 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-at|$sequence as item()*, $position as xs:double|item()?}}<br/>
|-
| '''Summary'''
| '''Examples'''
|
* <code>util:item-at(reverse(1 to 5), 1)</code> returns <code>5</code>.* <code>util:item-at(('a','b'), 0)</code> returns an empty sequence.
|}
==util:item-range==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:item-range|$sequence as item()*, $first as xs:double, $last as xs:double|item()*}}<br/>
|-
| '''Summary'''
| '''Examples'''
|
* <code>util:item-range(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.
|}
==util:last-from==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:last-from|$sequence as item()*|item()?}}<br/>
|-
| '''Summary'''
| '''Examples'''
|
* <code>util:last-from(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== {| width='100%'|-| width='120' | '''Signatures'''|{{Func|util:replicate|$sequence as item()*, $count as xs:integer|item()*}}<br/>|-| '''Summary'''|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'''|{{Error|negative|#Errors}} The specified number is negative.|-| '''Examples'''|* <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>.|} =Errors= {| class="wikitable" width="100%"! width="110"|Code|Description|-|{{Code|negative}}|The specified number is negative.
|}
=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
* Added: [[#util:replicate|util:replicate]]
The Module was introduced with Version 8.5.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu