Changes

Jump to navigation Jump to search
3,588 bytes removed ,  18:33, 1 December 2023
m
Text replacement - "</syntaxhighlight>" to "</pre>"
This module contains [[Querying#FunctionsModule Library|XQuery FunctionsModule]] contains functions for manipulating maps. All functions are preceded by the <code>map:</code> prefix, which is linked to the <code>http://www[[XQuery 3.w3.org/2005/xpath-functions/map</code> namespace. The following documentation is derived from a temporary 1#Maps|Maps]] have been introduced with [[http://www.w3.org/TR/xpath-functions-30/ XQuery 3.0 Functions and Operators1]] working draft.
A map is an additional kind of item. It comprises a collation and a set of entries. Each entry comprises a key which is an arbitrary atomic value, and an arbitrary sequence called the associated value. Within a map, no two entries have the same key, when compared using the <code>eq</code> operator under the map's collation. It is not necessary that all the keys should be mutually comparable (for example, they can include a mixture of integers and strings). Key values will never be of type <code>xs:untypedAtomic</code>, and they will never be the <code>xs:float</code> or <code>xs:double</code> value <code>NaN</code>.=Conventions=
The function call All functions in this module are assigned to the <code>[[#get|map<nowiki>http:get($//www.w3.org/2005/xpath-functions/map, $key)]]</nowiki></code> can be used namespace, which is statically bound to retrieve the value associated with a given key{{Code|map}} prefix.<br/>
A <em>map</em> can also be viewed as a function from keys to associated values. To achieve this, a map is also a function item. The function corresponding to the map has the signature <code>function($key as xs:anyAtomicType) as item()*</code>. Calling the function has the same effect as calling the <code>get</code> function: the expression <code>$map($key)</code> returns the same result as <code>map:get($map, $key)</code>. For example, if <code>$books-by-isbn</code> is a map whose keys are ISBNs and whose assocated values are <code>book</code> elements, then the expression <code>$books-by-isbn("0470192747")</code> returns the <code>book</code> element with the given ISBN. The fact that a map is a function item allows it to be passed as an argument to higher-order functions that expect a function item as one of their arguments.=Functions=
Like all other values, <em>maps</em> are immutable. For example, Some examples use the ''map'' {{Code|$week}} defined as:<codepre lang='xquery'>declare variable $week := map{ 0: "Sun", 1: "Mon", 2: "Tue", 3: "Wed", 4: "Thu", 5: "Fri", 6:remove"Sat"};</codepre> function creates a new map by removing an entry from an existing map, but the existing map is not changed by the operation.
Like sequences, <em>maps</em> have no identity. It is meaningful to compare the contents of two maps, but there is no way of asking whether they are "the same ==map": two maps with the same content are indistinguishable.contains==
Because a {| width='100%'| width='120' | '''Signature'''|<pre>map:contains( $map as map(*), $key as xs:anyAtomicType) as xs:boolean</pre>|- valign="top"| '''Summary'''| Returns true if the supplied {{Code|$map is }} contains an entry with a function item, functions that apply key equal to functions also apply to mapsthe supplied value of {{Code|$key}}; otherwise it returns false. A No error is raised if the map contains keys that are not comparable with the supplied {{Code|$key}}.If the supplied key is {{Code|xs:untypedAtomic}}, it is compared as an anonymous instance of {{Code|xs:string}}. If the supplied key is the {{Code|xs:float}} or {{Code|xs:double}} value {{Code|NaN}}, the functionreturns true if there is an entry whose key is {{Code|NaN}}, so or false otherwise.|- valign="top"| '''Examples'''| * {{Code|map:contains($week, 2)}} returns {{Code|true()}}.* {{Code|map:contains($week, 9)}} returns {{Code|false()}}.* <code>fnmap:function-namecontains(map {}, "xyz")</code> returns the empty sequence; {{Code|false()}}.* <code>fnmap:aritycontains(map { "xyz": 23 }, "xyz")</code> always returns <code>1</code>{{Code|true()}}.|}
Maps may be compared using the <code>fn==map:deep-equal</code> function. The semantics for this function are extended so that when two items are compared, at any level of recursion, the items compare equal if they are both maps, if both use the same collation, if both contain the same set of keys (compared using the <code>eq</code> operator), without regard to ordering, and if for each key that is present in both maps, the associated values are deep-equal. When comparing maps, the maps' collation is used rather than the collation supplied as an argument to the <code>fn:deep-equal</code> function.entry==
There is no operation to atomize {| width='100%'| width='120' | '''Signature'''|<pre>map:entry( $key as xs:anyAtomicType, $value as item()*) as map(*)</pre>|- valign="top"| '''Summary'''| Creates a new ''map or convert it to '' containing a stringsingle entry.The key of the entry in the new map is {{Code|$key}}, and its associated value is {{Code|$value}}.The function {{Code|map:entry}} is intended primarily for use in conjunction with the function <code>{{Function||map:merge}}</code>. For example, a map containing seven entries may be constructed like this: <pre lang='xquery'>map:merge(( map:entry("Sun", "Sunday"), map:entry("Mon", "Monday"), map:entry("Tue", "Tuesday"), map:entry("Wed", "Wednesday"), map:entry("Thu", "Thursday"), map:entry("Fri", "Friday"), map:entry("Sat", "Saturday")))</pre>
==Unlike the <code>map { ... }</code> expression, this technique can be used to construct a mapwith a variable number of entries, for example:collation=={|| width='90' valign<pre lang='topxquery' | '''Signatures'''| <code><strong>map:collation</strong>merge(for $b in //book return map as map:entry(*$b/isbn, $b)) as xs:string</code><br/pre>|-| valign='"top' "| '''SummaryExamples'''| Returns the collation URI of the <em>{{Code|map</em> supplied as :entry("M", "Monday")}} creates <code>$map{ "M": "Monday" }</code>.
|}
==map:containsfind== {|| width='90100%' valign| width='top120' | '''SignaturesSignature'''| <code><strongpre>map:contains</strong>find( $map input as mapitem()*), $key as xs:anyAtomicType) as itemarray(*)) as xs:boolean</code><br/pre>|-| valign='"top' "| '''Summary'''| Returns true if all values of maps in the <em>map</em> supplied as <code>{{Code|$map</code> contains an entry input}} with a key equal to the supplied value of <code>specified {{Code|$key</code>; otherwise it returns false}}. The equality comparison uses the map's collation; no error occurs if the map contains keys that are not comparable with the supplied <code>$key</code>found values will be returned in an array.Arbitrary input will be processed recursively as follows:If the supplied key is <code>xs:untypedAtomic</code>* In a sequence, it is converted to <code>xs:string</code>each item will be processed in order. If the supplied key is the <code>xs:float</code> or <code>xs:double</code> value <code>NaN</code>* In an array, the function returns falseall array members will be processed as sequence.<pre class="brush:xquery">let $week := * In a map{ 0:="Sonntag", 1:="Montag"all entries whose keys match the specified key. Moreover, 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>all values of the map will be processed as sequence.|-| valign='"top' "| '''Examples'''|* <code>map:containsfind($weekmap { 1:2 }, 21)</code> returns <code>true()[ 2 ]</code>.<br/>* <code>map:containsfind($week, 9)</code> returns <code>false()</code>.<br/><code>map{ 1: map { 2:contains(map{3: 4 } } }, "xyz"3)</code> returns <code>false()[ 4 ]</code>.<br/>* <code>map:containsfind(map{ "xyz":=23 }(1, 'b', true#0), "xyz"1)</code> returns <code>true()</code>an empty array.<br/>
|}
==map:entry=for-each={|| width='90' valign='top' | '''Signatures'''| <code><strong>map:entry</strong>($key as item(), $value as item()*) as map(*)</code><br/>|-| valign='top' | '''Summary'''| Creates a new <em>map</em> containing a single entry. The collation of the new map is the default collation from the static context. The key of the entry in the new map is <code>$key</code>, and its associated value is <code>$value</code>. If the supplied key is <code>xs:untypedAtomic</code>, it is converted to <code>xs:string</code>. If the supplied key is the <code>xs:float</code> or <code>xs:double</code> value <code>NaN</code>, the supplied <code>$map</code> is returned unchanged.The function <code>map:entry</code> is intended primarily for use in conjunction with the function <code>[[#map:new|map:new]]</code>. For example, a map containing seven entries may be constructed like this:
{| width='100%'| width='120' | '''Signature'''|<pre class="brush:xquery">map:new(for-each( $map as map:entry("Su", "Sunday"*), map$action as function(xs:entryanyAtomicType, item()*) as item("Mo", "Monday"),* map:entry) as item()*</pre>|- valign="Tutop", | '''Summary'''|Applies the specified {{Code|$action}} to every key/value pair of the supplied {{Code|$map}} and returns the results as a sequence.|- valign="Tuesdaytop"), | '''Examples'''|The following query adds the keys and values of all map:entryentries and returns {{Code|("We"3, "Wednesday"7),}}:<pre lang='xquery'> map:entryfor-each("Th", "Thursday"), map{ 1:entry("Fr"2, "Friday")3: 4 }, map:entryfunction("Sa"$key, "Saturday"$value){ $key + $value }))
</pre>
 
Unlike the <code>map{ ... }</code> expression, this technique can be used to construct a map with a variable number of entries, for example:
<pre class="brush:xquery">map:new(for $b in //book return map:entry($b/isbn, $b))</pre>
|-
| valign='top' | '''Examples'''
|<code>map:entry("M", "Monday")</code> creates a map <code>{ "M":="Monday" }</code> with a single entry.
|}
==map:get==
 {|| width='90100%' valign| width='top120' | '''SignaturesSignature'''| <code><strongpre>map:get</strong>( $map as map(*), $key as item()xs:anyAtomicType) as item()*</code><br/pre>|-| valign='"top' "| '''Summary'''|Returns the value associated with a supplied key in a given map. This function attempts to find an entry within the <em>map</em> supplied as <code>{{Code|$map</code> }} that has a key equal to the supplied value of <code>{{Code|$key</code>}}. If there is such an entry, it the function returns the associated value; otherwise it returns an empty sequence. The equality comparison uses the map's collation; no No error occurs is raised if the map contains keys that are not comparable with the supplied <code>{{Code|$key</code>}}. If the supplied key is <code>{{Code|xs:untypedAtomic</code>}}, it is converted to <code>{{Code|xs:string</code>. If the supplied key is the <code>xs:float</code> or <code>xs:double</code> value <code>NaN</code>, the function returns an empty sequence}}.A return value of <code>{{Code|()</code> }} from <code>{{Code|map:get</code> }} could indicate that the key is present in the map with an associated value of <code>{{Code|()</code>}}, or it could indicate that the key is not present in the map. The two cases can be distinguished by calling <code>{{Code|map:contains</code>}}.Invoking the <em>''map</em> '' as a function item has the same effect as calling <code>{{Code|get</code>}}: that is, when <code>{{Code|$map</code> }} is a map, the expression <code>{{Code|$map($K)</code> }} is equivalent to <code>{{Code|get($map, $K)</code>}}. Similarly, the expression <code>{{Code|get(get(get($map, 'employee'), 'name'), 'first')</code> }} can be written as <code>{{Code|$map('employee')('name')('first')</code>}}.<pre class="brush:xquery">let $week := map{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>|-| valign='"top' "| '''Examples'''|<code>* {{Code|map:get($week, 4)</code> }} returns <code>{{Code|"DonnerstagThu"</code>}}.<br/><code>* {{Code|map:get($week, 9)</code> }} returns <code>{{Code|()</code>}}. <em>''(When the key is not present, the function returns an empty sequence.).</em><br/>''<code>* {{Code|map:get(map:entry(7,())), 7)</code> }} returns <code>{{Code|()</code>}}. <em>''(An empty sequence as the result can also signify that the key is present and the associated value is an empty sequence.).</em><br/>''
|}
==map:keys==
 {|| width='90100%' valign| width='top120' | '''SignaturesSignature'''| <code><strongpre>map:keys</strong>( $map as map(*)) as xs:anyAtomicType*</code><br/pre>|-| valign='"top' "| '''Summary'''|Returns a sequence containing all the key values present in a map. The function takes any <em>map</em> as its <code>the supplied {{Code|$map</code> argument }} and returns the keys that are present in the map as a sequence of atomic values, . The order may differ from the order in which entries were inserted in implementation-dependent orderthe map.|-| valign='"top' "| '''Examples'''|* <code>map:keys(map{ 1:="yes", 2:="no" })</code> returns some permutation of <code>{{Code|(1,2)</code> <em>(the result is in implementation-dependent order)}}.</em><br/>
|}
==map:newmerge== {|| width='90100%' valign| width='top120' | '''SignaturesSignature'''| <code><strongpre>map:new</strong>() as map(*)</code><br/><code><strong>map:new</strong>merge( $maps as map(*)*) , $options as map(*)</code><br/><code><strong>map :new</strong>($maps as = map(*)*, $coll as xs:string{ }) as map(*)</code><br/pre>|-| valign='"top' "| '''Summary'''| Constructs and returns a new map. The zero-argument form of the function returns an empty <em>''map</em> whose collation is the default collation in the static context. It is equivalent to calling the one-argument form of the function with an empty sequence as the value of the first argument.The one-argument form of the function returns a <em>map</em> that '' is formed by combining the contents of the maps supplied in the <code>{{Code|$maps</code> argument. It is equivalent to calling the two-argument form of the function with the default collation from the static context as the second argument.The two-argument form of the function returns a <em>map</em> that is formed by combining the contents of the maps supplied in the <code>$maps</code> argument. The collation of the new map is the value of the <code>$collation</code> argument}}. The supplied maps are combined as follows:<ol> <li> <p> # There is one entry in the new map for each distinct key value present in the union of the input maps, where keys are considered distinct according to the rules of the <code>distinct-values</code> function with <code>$collation</code> as the collation. </p> </li> <li> <p> # The associated value for each such key is taken from the last map in the input sequence <code>{{Code|$maps</code> that contains an entry with this key. If this map contains more than one entry with this key (which can happen if its collation is different from that of the new map) then it is <em>implementation-dependent</em> which of them is selectedoptions}} argument defines how duplicate keys are handled. </p> </li></ol>There is no requirement that the supplied input maps should have the same or compatible types. The type of a map (for example <code>map(xs:integer, xs:string)</code>) is descriptive of the entries it currently containsCurrently, but is not a constraint on how the map may be combined with other maps.<pre class="brush:xquery">let $week := mapsingle option {{ 0:="Sonntag"Code|duplicates}} exists, 1:="Montag"and its allowed values are {{Code|use-first}}, 2:="Dienstag"{{Code|use-last}}, 3{{Code|combine}} and {{Code|reject}} (default:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" {{Code|use-first}}</pre>).|-| valign='"top' "| '''Examples'''|<code>map:new()</code> creates an empty map.<br/><code>* {{Code|map:newmerge(())</code> }} creates an empty map.<br/><code>* {{Code|map:newmerge((map:entry(0, "no"), map:entry(1, "yes")))}} creates </code> creates a map with the values <code>{ 0:="no", 1:="yes" }</code>.* The following function adds a seventh entry to an existing map:<br/pre lang='xquery'><code>map:newmerge(map:entry(0, "no")$week, map{ 7:entry(1, "yes---"}))</codepre> creates * In the following example, the values of all maps are combined, resulting in a map with the values a single key (<code>map { 0:="nokey": (1, 2, 1:="yes" 3) }</code>.):<br/pre lang='xquery'><code>map:new(merge( for $i in 1 to 3 return map { 'key': $weeki }, map{ 7'duplicates':="Unbekannt" 'combine' }))</codepre> creates a map with the values <code>{ 0:|} ="Sonntag", 1:="Montag", 2map:put="Dienstag", 3:="Mittwoch", 4: {| width="Donnerstag", 5:'100%'| width="Freitag", 6:="Samstag", 7:="Unbekannt" }</code>.<br/>'120' | '''Signature'''|<codepre>map:newput( $map as map(*), $weekkey as xs:anyAtomicType, $value as item()*) as map{ 6:="Sonnabend" })(*)</codepre> creates |- valign="top"| '''Summary'''| Creates a new ''map with '', containing the entries of the values supplied {{Code|$map}} and a new entry composed by {{Code|$key}} and {{Code|$value}}. The semantics of this function are equivalent to <code>map:merge((map { 0:="Sonntag"$key, 1:="Montag"$value }, 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Sonnabend" }$map))</code>.<br/>
|}
==map:remove==
 {|| width='90100%' valign| width='top120' | '''SignaturesSignature'''| <code><strongpre>map:remove</strong>( $map as map(*), $key keys as item()xs:anyAtomicType*) as map(*)</code><br/pre>|-| valign='"top' "| '''Summary'''| Constructs a new map by removing an entry entries from an existing map. The collation of the new map is the same as the collation of the map supplied as <code>$map</code>. The entries in the new map correspond to the entries of <code>{{Code|$map</code>}}, excluding any entry whose key is equal to <code>entries supplied via {{Code|$key</code>keys}}.No failure occurs if the input map contains no entry with the supplied keykeys; the input map is returned unchanged<pre class="brush:xquery">let $week := map{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>.|-| valign='"top' "| '''Examples'''| <code>* {{Code|map:remove($week, 4)}} creates </code> creates a map with the values <code>{ 0:="SonntagSun", 1:="MontagMon", 2:="DienstagTue", 3:="MittwochWed", 5:="FreitagFri", 6:="SamstagSat" }</code>.<br/><code>* {{Code|map:remove($week, 23)}} creates </code> creates a map with the values <code>{ 0:="SonntagSun", 1:="MontagMon", 2:="DienstagTue", 3:="MittwochWed", 4:="DonnerstagThu", 5:="FreitagFri", 6:="SamstagSat" }</code>.<br/>
|}
==map:size==
 {|| width='90100%' valign| width='top120' | '''SignaturesSignature'''| <code><strongpre>map:size</strong>( $map as map(*)) as xs:integer</code><br/pre>|-| valign='"top' "| '''Summary'''| Returns a the number of entries in the supplied map. The function takes any <em>map</em> as its <code>the supplied {{Code|$map</code> argument }} and returns the number of entries that are present in the map.|-| valign='"top' "| '''Examples'''| <code>* {{Code|map:size(map:newmerge(())</code> )}} returns <code>{{Code|0</code>}}.<br/>* <code>map:size(map{ "true":=1, "false":=0 })</code> returns <code>{{Code|2</code>}}.<br/>
|}
 
=Changelog=
 
;Version 8.6
* Added: {{Function||map:find}}
* Updated: {{Function||map:merge}}: Signature extended with options argument. By default, value of first key is now adopted (instead of last, as in previous versions).
 
;Version 8.4
* Removed: map:serialize (use fn:serialize instead)
 
;Version 8.0
* Added: {{Function||map:for-each}}, {{Function||map:merge}}, {{Function||map:put}}
* Removed: support for collations (in accordance with the XQuery 3.1 spec).
* Removed: {{Code|map:new}} (replaced with {{Code|map:merge}})
* Updated: aligned with latest specification: compare keys of type {{Code|xs:untypedAtomic}} as {{Code|xs:string}} instances, store {{Code|xs:float}} or {{Code|xs:double}} value {{Code|NaN}}.
* Introduction on maps is now found in the article on [[XQuery 3.1#Maps|XQuery 3.1]].
 
;Version 7.8
* Updated: map syntax <code>map { 'key': 'value' }</code>
* Added: {{Function||map:serialize}}
 
;Version 7.7.1
* Updated: alternative map syntax without {{Code|map}} keyword and {{Code|:}} as key/value delimiter (e.g.: <code>{ 'key': 'value' })</code>
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu