Difference between revisions of "Map Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Reverted edits by CG (talk) to last revision by LeoWoerteler)
Line 1: Line 1:
This module contains [[Querying#Functions|XQuery Functions]] for manipulating maps. All functions are preceded by the <code>map:</code> prefix, which is linked to the <code>http://www.w3.org/2005/xpath-functions/map</code> namespace. The following documentation is derived from a temporary [http://www.w3.org/TR/xpath-functions-30/ XQuery 3.0 Functions and Operators] working draft.
+
<p>This module contains [[Querying#Functions|XQuery Functions]] for manipulating maps.</p>
 
+
<p>All functions are preceded by the <code>map:</code> prefix, which is linked to the <code>http://www.w3.org/2005/xpath-functions/map</code> namespace.</p>
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>.
+
<p>A map is an additional kind of item.</p>
 
+
<p>A map 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>.</p> <p>The function call <code>map:get($map, $key)</code> can be used to retrieve the value associated with a given key.</p>
The function call <code>[[#get|map:get($map, $key)]]</code> can be used to retrieve the value associated with a given key.
+
<p>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>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.</p>
 
+
<p>Like all other values, <em>maps</em> are immutable. For example, the <code>map:remove</code> function creates a new map by removing an entry from an existing map, but the existing map is not changed by the operation.</p>
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.
+
<p>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.</p>
 
+
<p>Because a map is a function item, functions that apply to functions also apply to maps. A map is an anonymous function, so <code>fn:function-name</code> returns the empty sequence; <code>fn:arity</code> always returns <code>1</code>.</p>
Like all other values, <em>maps</em> are immutable. For example, the <code>map:remove</code> function creates a new map by removing an entry from an existing map, but the existing map is not changed by the operation.
+
<p>Maps may be compared using the <code>fn: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.</p>
 
+
<p>There is no operation to atomize a map or convert it to a string.</p>
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.
 
 
 
Because a map is a function item, functions that apply to functions also apply to maps. A map is an anonymous function, so <code>fn:function-name</code> returns the empty sequence; <code>fn:arity</code> always returns <code>1</code>.
 
 
 
Maps may be compared using the <code>fn: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.
 
 
 
There is no operation to atomize a map or convert it to a string.
 
  
 
==map:collation==
 
==map:collation==
Line 23: Line 16:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| valign='top' | '''Summary'''
| Returns the collation URI of the <em>map</em> supplied as <code>$map</code>.
+
| <p>Returns the URI of the supplied map's collation.</p>
 +
|-
 +
| valign='top' | '''Rules'''
 +
| The function <code>map:collation</code> returns the collation URI of the <em>map</em> supplied as <code>$input</code>.
 
|}
 
|}
 
 
==map:contains==
 
==map:contains==
 
{|
 
{|
Line 32: Line 27:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| valign='top' | '''Summary'''
| Returns true if the <em>map</em> supplied as <code>$map</code> contains an entry with a key equal to the supplied value of <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>.
+
| <p>Tests whether a supplied map contains an entry for a given key.</p>
 +
|-
 +
| valign='top' | '''Rules'''
 +
| The function <code>map:contains</code> returns true if the <em>map</em> supplied as <code>$map</code> contains an entry with a key equal to the supplied value of <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>.
 
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 function returns false.
 
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 function returns false.
<pre class="brush:xquery">let $week := map{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch",
+
<pre class="brush:xquery">let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch",
                 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>
+
                 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}</pre>
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:contains($week, 2)</code> returns <code>true()</code>.<br/><code>map:contains($week, 9)</code> returns <code>false()</code>.<br/><code>map:contains(map{}, "xyz")</code> returns <code>false()</code>.<br/><code>map:contains(map{ "xyz":=23 }, "xyz")</code> returns <code>true()</code>.<br/>
+
|
 +
The expression <code>map:contains($week, 2)</code> returns <code>true()</code>.<br/>
 +
The expression <code>map:contains($week, 9)</code> returns <code>false()</code>.<br/>
 +
The expression <code>map:contains(map{}, "xyz")</code> returns <code>false()</code>.<br/>
 +
The expression <code>map:contains(map{"xyz":=23}, "xyz")</code> returns <code>true()</code>.<br/>
 
|}
 
|}
 
 
==map:entry==
 
==map:entry==
 
{|
 
{|
Line 47: Line 48:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| 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.
+
| <p>The function <code>map:entry</code> returns a new <em>map</em> containing a single entry. The collation of the new map is the 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>.</p>
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:
+
|-
 +
| valign='top' | '''Rules'''
 +
| 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</code>. For example, a map containing seven entries may be constructed like this:
  
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
Line 62: Line 66:
 
</pre>
 
</pre>
  
Unlike the <code>map{ ... }</code> expression, this technique can be used to construct a map with a variable number of entries, for example:
+
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>
 
<pre class="brush:xquery">map:new(for $b in //book return map:entry($b/isbn, $b))</pre>
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:entry("M", "Monday")</code> creates a map with the values <code>{ "M":="Monday" }</code>.
+
| The expression <code>map:entry("M", "Monday")</code> returns <code>map{"M":="Monday"}</code>.
 
|}
 
|}
 
 
==map:get==
 
==map:get==
 
{|
 
{|
Line 75: Line 78:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| 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>$map</code> that has a key equal to the supplied value of <code>$key</code>. If there is such  an entry, it returns the associated value; otherwise it returns an empty sequence. 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>. 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 function returns an empty sequence.
+
| <p>Returns the value associated with a supplied key in a given map.</p>
 +
|-
 +
| valign='top' | '''Rules'''
 +
| The function <code>map:get</code> attempts to find an entry within the <em>map</em> supplied as <code>$input</code> that has a key equal to the supplied value of <code>$key</code>. If there is such  an entry, it returns the associated value; otherwise it returns an empty sequence. 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>. 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 function returns an empty sequence.
 
A return value of <code>()</code> from <code>map:get</code> could indicate that the key is present in the map with an associated value of <code>()</code>, or it could indicate that the key is not present in the map. The two cases can be distinguished by calling <code>map:contains</code>.
 
A return value of <code>()</code> from <code>map:get</code> could indicate that the key is present in the map with an associated value of <code>()</code>, or it could indicate that the key is not present in the map. The two cases can be distinguished by calling <code>map:contains</code>.
 
Invoking the <em>map</em> as a function item has the same effect as calling <code>get</code>: that is, when <code>$map</code> is a map, the expression <code>$map($K)</code> is equivalent to <code>get($map, $K)</code>. Similarly, the expression <code>get(get(get($map, 'employee'), 'name'), 'first')</code> can be written as <code>$map('employee')('name')('first')</code>.
 
Invoking the <em>map</em> as a function item has the same effect as calling <code>get</code>: that is, when <code>$map</code> is a map, the expression <code>$map($K)</code> is equivalent to <code>get($map, $K)</code>. Similarly, the expression <code>get(get(get($map, 'employee'), 'name'), 'first')</code> can be written as <code>$map('employee')('name')('first')</code>.
<pre class="brush:xquery">let $week := map{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch",
+
<pre class="brush:xquery">let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch",
                 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>
+
                 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}</pre>
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:get($week, 4)</code> returns <code>"Donnerstag"</code>.<br/><code>map:get($week, 9)</code> returns <code>()</code>. <em>(When the key is not present, the function returns an empty sequence.).</em><br/><code>map:get(map:entry(7,())), 7)</code> returns <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/>
+
|  
 +
The expression <code>map:get($week, 4)</code> returns <code>"Donnerstag"</code>.<br/>
 +
The expression <code>map:get($week, 9)</code> returns <code>()</code>. <em>(When the key is not present, the function returns an empty sequence.).</em><br/>
 +
The expression <code>map:get(map:entry(7,())), 7)</code> returns <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==
 
==map:keys==
 
{|
 
{|
Line 91: Line 99:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| 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>$map</code> argument and returns the keys that are present in the map as a sequence of atomic values, in implementation-dependent order.
+
| <p>Returns a sequence containing all the key values present in a map.</p>
 +
|-
 +
| valign='top' | '''Rules'''
 +
| The function <code>map:keys</code> takes any <em>map</em> as its <code>$input</code> argument and returns the keys that are present in the map as a sequence of atomic values, in implementation-dependent order.
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:keys(map{ 1:="yes", 2:="no" })</code> returns some permutation of <code>(1,2)</code> <em>(the result is in implementation-dependent order).</em><br/>
+
| The expression <code>map:keys(map{1:="yes", 2:="no"})</code> returns some permutation of <code>(1,2)</code>. <em>(The result is in implementation-dependent order.).</em><br/>
 
|}
 
|}
 
 
==map:new==
 
==map:new==
 
{|
 
{|
Line 103: Line 113:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| 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.
+
| <p>Creates a new map: either an empty map, or a map that combines entries from a number of existing maps.</p>
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>$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:
+
| valign='top' | '''Rules'''
 +
| The function <code>map:new</code> 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>$input</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>$input</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>
 
<ol>
 
   <li>
 
   <li>
Line 118: Line 131:
 
     <p>
 
     <p>
 
       The associated value for each such key is taken from the last
 
       The associated value for each such key is taken from the last
       map in the input sequence <code>$maps</code> that contains an
+
       map in the input sequence <code>$input</code> that contains an
 
       entry with this key. If this map contains more than one entry with
 
       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
 
       this key (which can happen if its collation is different from that
Line 127: Line 140:
 
</ol>
 
</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 contains, but is not a constraint on how the map may be combined with other maps.
 
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 contains, but is not a constraint on how the map may be combined with other maps.
<pre class="brush:xquery">let $week := map{ 0:="Sonntag", 1:="Montag", 2:="Dienstag",
+
<pre class="brush:xquery">let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag",
             3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>
+
             3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}</pre>
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:new()</code> creates an empty map.<br/><code>map:new(())</code> creates an empty map.<br/><code>map:new(map:entry(0, "no"), map:entry(1, "yes"))</code> creates a map with the values <code>{ 0:="no", 1:="yes" }</code>.<br/><code>map:new(map:entry(0, "no"), map:entry(1, "yes"))</code> creates a map with the values <code>{ 0:="no", 1:="yes" }</code>.<br/><code>map:new(($week, map{ 7:="Unbekannt" }))</code> creates a map with the values <code>{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag", 7:="Unbekannt" }</code>.<br/><code>map:new(($week, map{ 6:="Sonnabend" }))</code> creates a map with the values  <code>{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Sonnabend" }</code>.<br/>
+
|  
 +
The expression <code>map:new()</code> returns <code>map{}</code>. <em>(Returns an empty map, whose collation is the default collation from the static context).</em><br/>
 +
The expression <code>map:new(())</code> returns <code>map{}</code>. <em>(Returns an empty map, whose collation is the default collation from the static context).</em><br/>
 +
The expression <code>map:new(map:entry(0, "no"), map:entry(1, "yes"))</code> returns <code>map{0:="no", 1:="yes"}</code>. <em>(Returns a map with two entries; the collation of the map is the default collation from the static context).</em><br/>
 +
The expression <code>map:new(map:entry(0, "no"), map:entry(1, "yes"))</code> returns <code>map{0:="no", 1:="yes"}</code>. <em>(Returns a map with two entries; the collation of the map is the default collation from the static context).</em><br/>
 +
The expression <code>map:new(($week, map{7:="Unbekannt"}))</code> returns <code>map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag", 7:="Unbekannt"}</code>. <em>(The value of the existing map is unchanged; a new map is created containing all the entries from <code>$week</code>, supplemented with a new entry.).</em><br/>
 +
The expression <code>map:new(($week, map{6:="Sonnabend"}))</code> returns <code>map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Sonnabend"}</code>. <em>(The value of the existing map is unchanged; a new map is created containing all the entries from <code>$week</code>, with one entry replaced by a new entry. Both input maps contain an entry with the key value <code>6</code>; the one used in the result is the one that comes last in the input sequence.).</em><br/>
 +
The expression <code>map:new((map{"A":=1}, map{"a:=2"}), "http://collation.example.com/caseblind")</code> returns <code>map{"a":=2}</code>. <em>(Assuming that the keys of the two entries are equal under the rules of the chosen collation, only one of the entries can appear in the result; the one that is chosen is the one from the last map in the input sequence. If both entries were in the same map, it would be implementation-dependent which was chosen.).</em><br/>
 
|}
 
|}
 
 
==map:remove==
 
==map:remove==
 
{|
 
{|
Line 140: Line 159:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| valign='top' | '''Summary'''
| Constructs a new map by removing an entry 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>$map</code>, excluding any entry whose key is equal to <code>$key</code>.
+
| <p>Constructs a new map by removing an entry from an existing map.</p>
 +
|-
 +
| valign='top' | '''Rules'''
 +
| The function <code>map:remove</code> returns a new <em>map</em>. 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>$map</code>, excluding any entry whose key is equal to <code>$key</code>.
 
No failure occurs if the input map contains no entry with the supplied key; the input map is returned unchanged
 
No failure occurs if the input map contains no entry with the supplied key; the input map is returned unchanged
<pre class="brush:xquery">let $week := map{ 0:="Sonntag", 1:="Montag", 2:="Dienstag",
+
<pre class="brush:xquery">let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag",
             3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</pre>
+
             3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}</pre>
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:remove($week, 4)</code> creates a map with the values <code>{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 5:="Freitag", 6:="Samstag" }</code>.<br/><code>map:remove($week, 23)</code> creates a map with the values <code>{ 0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag" }</code>.<br/>
+
| The expression <code>map:remove($week, 4)</code> returns <code>map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 5:="Freitag", 6:="Samstag"}</code>.<br/>
 +
The expression <code>map:remove($week, 23)</code> returns <code>map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}</code>.<br/>
 
|}
 
|}
 
 
==map:size==
 
==map:size==
 
{|
 
{|
Line 155: Line 177:
 
|-
 
|-
 
| valign='top' | '''Summary'''
 
| valign='top' | '''Summary'''
| Returns a the number of entries in the supplied map. The function takes any <em>map</em> as its <code>$map</code> argument and returns the number of entries that are present in the map.
+
| <p>Returns a the number of entries in the supplied map.</p>
 +
|-
 +
| valign='top' | '''Rules'''
 +
| The function <code>map:size</code> takes any <em>map</em> as its <code>$input</code> argument and returns the number of entries that are present in the map.
 
|-
 
|-
 
| valign='top' | '''Examples'''
 
| valign='top' | '''Examples'''
|<code>map:size(map:new())</code> returns <code>0</code>.<br/><code>map:size(map{ "true":=1, "false":=0 })</code> returns <code>2</code>.<br/>
+
| The expression <code>map:size(map:new())</code> returns <code>0</code>.<br/> The expression <code>map:size(map{"true":=1, "false":=0})</code> returns <code>2</code>.<br/>
 
|}
 
|}

Revision as of 12:27, 23 April 2011

This module contains XQuery Functions for manipulating maps.

All functions are preceded by the map: prefix, which is linked to the http://www.w3.org/2005/xpath-functions/map namespace.

A map is an additional kind of item.

A map 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 eq 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 xs:untypedAtomic, and they will never be the xs:float or xs:double value NaN.

The function call map:get($map, $key) can be used to retrieve the value associated with a given key.

A map 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 function($key as xs:anyAtomicType) as item()*. Calling the function has the same effect as calling the get function: the expression $map($key) returns the same result as get($map, $key). For example, if $books-by-isbn is a map whose keys are ISBNs and whose assocated values are book elements, then the expression $books-by-isbn("0470192747") returns the book 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.

Like all other values, maps are immutable. For example, the map:remove 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, maps 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.

Because a map is a function item, functions that apply to functions also apply to maps. A map is an anonymous function, so fn:function-name returns the empty sequence; fn:arity always returns 1.

Maps may be compared using the fn:deep-equal 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 eq 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 fn:deep-equal function.

There is no operation to atomize a map or convert it to a string.

map:collation

Signatures map:collation($map as map(*)) as xs:string
Summary

Returns the URI of the supplied map's collation.

Rules The function map:collation returns the collation URI of the map supplied as $input.

map:contains

Signatures map:contains($map as map(*), $key as item()) as xs:boolean
Summary

Tests whether a supplied map contains an entry for a given key.

Rules The function map:contains returns true if the map supplied as $map contains an entry with a key equal to the supplied value of $key; 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 $key.

If the supplied key is xs:untypedAtomic, it is converted to xs:string. If the supplied key is the xs:float or xs:double value NaN, the function returns false.

let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch",
                 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}
Examples

The expression map:contains($week, 2) returns true().
The expression map:contains($week, 9) returns false().
The expression map:contains(map{}, "xyz") returns false().
The expression map:contains(map{"xyz":=23}, "xyz") returns true().

map:entry

Signatures map:entry($key as item(), $value as item()*) as map(*)
Summary

The function map:entry returns a new map containing a single entry. The collation of the new map is the the default collation from the static context. The key of the entry in the new map is $key, and its associated value is $value.

Rules If the supplied key is xs:untypedAtomic, it is converted to xs:string. If the supplied key is the xs:float or xs:double value NaN, the supplied $map is returned unchanged.

The function map:entry is intended primarily for use in conjunction with the function map:new. For example, a map containing seven entries may be constructed like this:

map:new((
  map:entry("Su", "Sunday"),
  map:entry("Mo", "Monday"),
  map:entry("Tu", "Tuesday"),
  map:entry("We", "Wednesday"),
  map:entry("Th", "Thursday"),
  map:entry("Fr", "Friday"),
  map:entry("Sa", "Saturday")
))

Unlike the map{...} expression, this technique can be used to construct a map with a variable number of entries, for example:

map:new(for $b in //book return map:entry($b/isbn, $b))
Examples The expression map:entry("M", "Monday") returns map{"M":="Monday"}.

map:get

Signatures map:get($map as map(*), $key as item()) as item()*
Summary

Returns the value associated with a supplied key in a given map.

Rules The function map:get attempts to find an entry within the map supplied as $input that has a key equal to the supplied value of $key. If there is such an entry, it returns the associated value; otherwise it returns an empty sequence. The equality comparison uses the map's collation; no error occurs if the map contains keys that are not comparable with the supplied $key. If the supplied key is xs:untypedAtomic, it is converted to xs:string. If the supplied key is the xs:float or xs:double value NaN, the function returns an empty sequence.

A return value of () from map:get could indicate that the key is present in the map with an associated value of (), or it could indicate that the key is not present in the map. The two cases can be distinguished by calling map:contains. Invoking the map as a function item has the same effect as calling get: that is, when $map is a map, the expression $map($K) is equivalent to get($map, $K). Similarly, the expression get(get(get($map, 'employee'), 'name'), 'first') can be written as $map('employee')('name')('first').

let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch",
                 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}
Examples

The expression map:get($week, 4) returns "Donnerstag".
The expression map:get($week, 9) returns (). (When the key is not present, the function returns an empty sequence.).
The expression map:get(map:entry(7,())), 7) returns (). (An empty sequence as the result can also signify that the key is present and the associated value is an empty sequence.).

map:keys

Signatures map:keys($map as map(*)) as xs:anyAtomicType*
Summary

Returns a sequence containing all the key values present in a map.

Rules The function map:keys takes any map as its $input argument and returns the keys that are present in the map as a sequence of atomic values, in implementation-dependent order.
Examples The expression map:keys(map{1:="yes", 2:="no"}) returns some permutation of (1,2). (The result is in implementation-dependent order.).

map:new

Signatures map:new() as map(*)
map:new($maps as map(*)*) as map(*)
map:new($maps as map(*)*, $coll as xs:string) as map(*)
Summary

Creates a new map: either an empty map, or a map that combines entries from a number of existing maps.

Rules The function map:new constructs and returns a new map. The zero-argument form of the function returns an empty map 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 map that is formed by combining the contents of the maps supplied in the $input 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 map that is formed by combining the contents of the maps supplied in the $input argument. The collation of the new map is the value of the $collation argument. The supplied maps are combined as follows:

  1. 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 distinct-values function with $collation as the collation.

  2. The associated value for each such key is taken from the last map in the input sequence $input 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 implementation-dependent which of them is selected.

There is no requirement that the supplied input maps should have the same or compatible types. The type of a map (for example map(xs:integer, xs:string)) is descriptive of the entries it currently contains, but is not a constraint on how the map may be combined with other maps.

let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag",
            3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}
Examples

The expression map:new() returns map{}. (Returns an empty map, whose collation is the default collation from the static context).
The expression map:new(()) returns map{}. (Returns an empty map, whose collation is the default collation from the static context).
The expression map:new(map:entry(0, "no"), map:entry(1, "yes")) returns map{0:="no", 1:="yes"}. (Returns a map with two entries; the collation of the map is the default collation from the static context).
The expression map:new(map:entry(0, "no"), map:entry(1, "yes")) returns map{0:="no", 1:="yes"}. (Returns a map with two entries; the collation of the map is the default collation from the static context).
The expression map:new(($week, map{7:="Unbekannt"})) returns map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag", 7:="Unbekannt"}. (The value of the existing map is unchanged; a new map is created containing all the entries from $week, supplemented with a new entry.).
The expression map:new(($week, map{6:="Sonnabend"})) returns map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Sonnabend"}. (The value of the existing map is unchanged; a new map is created containing all the entries from $week, with one entry replaced by a new entry. Both input maps contain an entry with the key value 6; the one used in the result is the one that comes last in the input sequence.).
The expression map:new((map{"A":=1}, map{"a:=2"}), "http://collation.example.com/caseblind") returns map{"a":=2}. (Assuming that the keys of the two entries are equal under the rules of the chosen collation, only one of the entries can appear in the result; the one that is chosen is the one from the last map in the input sequence. If both entries were in the same map, it would be implementation-dependent which was chosen.).

map:remove

Signatures map:remove($map as map(*), $key as item()) as map(*)
Summary

Constructs a new map by removing an entry from an existing map.

Rules The function map:remove returns a new map. The collation of the new map is the same as the collation of the map supplied as $map. The entries in the new map correspond to the entries of $map, excluding any entry whose key is equal to $key.

No failure occurs if the input map contains no entry with the supplied key; the input map is returned unchanged

let $week := map{0:="Sonntag", 1:="Montag", 2:="Dienstag",
            3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}
Examples The expression map:remove($week, 4) returns map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 5:="Freitag", 6:="Samstag"}.

The expression map:remove($week, 23) returns map{0:="Sonntag", 1:="Montag", 2:="Dienstag", 3:="Mittwoch", 4:="Donnerstag", 5:="Freitag", 6:="Samstag"}.

map:size

Signatures map:size($map as map(*)) as xs:integer
Summary

Returns a the number of entries in the supplied map.

Rules The function map:size takes any map as its $input argument and returns the number of entries that are present in the map.
Examples The expression map:size(map:new()) returns 0.
The expression map:size(map{"true":=1, "false":=0}) returns 2.