Changes

Jump to navigation Jump to search
6,055 bytes added ,  12:17, 25 February 2021
no edit summary
This [[Module Library|XQuery Module]] contains various small utility and helper functions. Please note that some of the  For all listed functions are used for internal query rewritings. They , equivalent expressions exist in standard XQuery, but code may be renamed 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 moved (('default', 'values'))=> util:last()</syntaxhighlight> In addition, various query optimizations create calls to other modules in future versions of BaseXthe utility functions.
=Conventions=
|-
| '''Summary'''
|Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. The function is equivalent Equivalent to one of the following expressions:* <codesyntaxhighlight lang="xquery">if(exists($items)) then $items else $default</code>,(: Elvis operator :)* <code>$items ?: $default</codesyntaxhighlight> (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}}.
|}
 
==util:within==
 
{{Mark|Introduced with Version 9.5:}}
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:within|$sequence as item()*, $min as xs:integer|xs:boolean}}<br/>{{Func|util:within|$sequence as item()*, $min as xs:integer, $max as xs:integer|xs:boolean}}
|-
| '''Summary'''
|Checks if the specified {{Code|$sequence}} has at least {{Code|$min}} and, optionally, at most {{Code|$max}} items. Equivalent to:
<syntaxhighlight lang="xquery">
let $count := count($sequence)
return $count >= $min and $count <= $max
</syntaxhighlight>
|-
| '''Examples'''
|
* <code>util:within(('a', 'b', 'c'), 2)</code> returns {{Code|true}}.
* <code>util:within((1 to 1000000000)[. < 10], 3, 6)</code> returns {{Code|true}}.
|}
==util:item==
 
{{Mark|Updated with Version 9.2}}: Renamed (before: {{Code|util:item-at}}).
{| width='100%'
|-
| '''Summary'''
|Returns the item from {{Code|$sequence}} at the specified {{Code|$position}}. Equivalent to :<codesyntaxhighlight lang="xquery">$sequence[$position]</codesyntaxhighlight>.
|-
| '''Examples'''
==util:range==
 
{{Mark|Updated with Version 9.2}}: Renamed (before: {{Code|util:item-range}}).
{| width='100%'
|-
| '''Summary'''
|Returns items from {{Code|$sequence}}, starting at position {{Code|$first}} and ending at {{Code|$last}}. Equivalent to :<codesyntaxhighlight lang="xquery">subsequence($sequence, $first, $last - $first + 1)</codesyntaxhighlight>.
|-
| '''Examples'''
==util:last==
 
{{Mark|Updated with Version 9.2}}: Renamed (before: {{Code|util:last-from}}).
{| width='100%'
|-
| '''Summary'''
|Returns last item of a {{Code|$sequence}}. Equivalent to :<codesyntaxhighlight lang="xquery">$sequence[last()]</codesyntaxhighlight>.
|-
| '''Examples'''
==util:init==
 
{{Mark|Introduced with Version 9.2:}}
{| width='100%'
|-
| '''Summary'''
|Returns all items of a {{Code|$sequence}} except for the last one. Equivalent to :<codesyntaxhighlight lang="xquery">$sequence[position() < last()]</codesyntaxhighlight>.
|-
| '''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]. 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==
 
{| 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 path expression <code>/abc</code> is internally represented as <code>util:root(.)/abc</code. Equivalent to:
<syntaxhighlight lang="xquery">
$nodes ! /
</syntaxhighlight>
|}
 
=Array and Map Functions=
 
==util:array-members==
 
{{Mark|Introduced with Version 9.5.}}
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:array-members|$array as array(*)|array(*)*}}
|-
| '''Summary'''
|Returns each member of an {{Code|$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 array:members($array)
return element numbers { $member }
</syntaxhighlight>
|}
 
==util:array-values==
 
{{Mark|Introduced with Version 9.5.}}
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:array-values|$array as array(*)|item()*}}
|-
| '''Summary'''
|Returns all members of an {{Code|$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 array:values($array)
</syntaxhighlight>
|}
 
==util:map-entries==
 
{{Mark|Introduced with Version 9.5.}}
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:map-entries|$map as map(*)|map(xs:string, item()*)*}}
|-
| '''Summary'''
|Returns each entry of a {{Code|$map}} as a new map, each with a {{Code|key}} and {{Code|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 map:entries($map)
return element { $entry?key } { string-join($entry?value) }
</syntaxhighlight>
|}
 
==util:map-values==
 
{{Mark|Introduced with Version 9.5.}}
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:map-values|$map as map(*)|item()*}}
|-
| '''Summary'''
|Returns all values of a {{Code|$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 map:values($map)
</syntaxhighlight>
|}
==util:replicate==
 
{{Mark|Updated with Version 9.5:}} Third argument added.
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:replicate|$sequence input as item()*, $count as xs:integer|item()*}}<br/>{{Func|util:replicate|$input as item()*, $count as xs:integer, $multiple as xs:boolean|item()*}}
|-
| '''Summary'''
|Returns Evaluates {{Code|$input}} and returns the result {{Code|$count}} instances of the specified times. Unless {{Code|$sequencemultiple}}is enabled, the input expression is only evaluated once. A similar result can be generated with Equivalent expressions:<codesyntaxhighlight lang="xquery">util:replicate($input, $count, true()),(1 to $count) ! $sequenceinput</codesyntaxhighlight>, but in the latter case, the right-hand expression will be evaluated multiple times.
|-
| '''Errors'''
|
* <code>util:replicate('A', 3)</code> returns <code>A A A</code>.
* In the following query, a single new element node is constructed, and {{Code|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 {{Code|false}}:
<syntaxhighlight lang="xquery">
let $nodes := util:replicate(<node/>, 2, true())
return $nodes[1] is $nodes[2]
</syntaxhighlight>
|}
 
==util:intersperse==
 
{{Mark|Introduced with Version 9.5.}}
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:intersperse|$items as item()*, $separator as item()*|item()*}}
|-
| '''Summary'''
|Inserts the defined {{Code|$separator}} between the {{Code|$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:charsduplicates==
{{Mark|Introduced with Version 9.2:5.}}
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:charsduplicates|$string sequence as xs:stringitem()*|xs:stringanyAtomicType*}}<br/>{{Func|util:duplicates|$sequence as item()*, $collation as xs:string|xs:anyAtomicType*}}
|-
| '''Summary'''
|Returns all characters of duplicate values in a {{Code|$stringsequence}} as a sequence. Equivalent to <code>stringSee [https://www.w3.org/TR/xpath-tofunctions-codepoints($string) ! codepoints31/#func-distinct-tovalues fn:distinct-string(.)</code>values] for the applied equality rules and the usage of the {{Code|$collation}} argument.
|-
| '''Examples'''
|
* <code>util:charsduplicates('AB'(1, 2, 1, 1))</code> returns the two strings <code>A</code> and <code>B1</code>.
|}
==util:ddochars== {{Mark|Introduced with Version 9.3:}}
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:ddochars|$nodes string as node()*xs:string|node()xs:string*}}<br/>
|-
| '''Summary'''
|Returns nodes all characters of a {{Code|$string}} as a sequence. Equivalent to:<syntaxhighlight lang="xquery">for $cp in string-to-codepoints($string)return codepoints-to-string($cp)</syntaxhighlight>|-| ''distinct document order'Examples'''|* <code>util: duplicate nodes will be removed, and the remaining nodes will be returned in [https:chars('AB')<//www.w3.org/TR/xquery-31/#dt-document-order document order]. All results of path expression are in distinct document order, so code> returns the function is equivalent to the expression two strings <code>$nodesA</self::node()code> and <code>B</code>.
|}
=Changelog=
 
;Version 9.5
* Added: [[#util:intersperse|util:intersperse]], [[#util:within|util:within]], [[#util:duplicates|util:duplicates]], [[#util:array-members|util:array-members]], [[#util:array-values|util:array-values]], [[#util:map-entries|util:map-entries]], [[#util:map-values|util:map-values]]
* Updated: [[#util:replicate|util:replicate]]: Third argument added.
 
;Version 9.4
* Added: [[#util:root|util:root]]
;Version 9.3
;Version 9.2
* Added: [[#util:chars|util:chars]], [[#util:init|util:init]]
* UpdatesUpdated: [[#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
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu