This page was generated
January  28,  2016
4:04  AM
XQuery & XSLT Built-In & Modules Function Reference

Built-In: map

The map built-in functions are used to create maps. Maps store name-value pairs in an in-memory data structure. You can also persist a map to disk by storing it in a document. Some programming languages implement maps using hash tables, but these map functions make it convenient for you to create and update your own maps.

Maps are represented using the map:map XQuery primitive type. When you serialize an object of map:map type, it serializes to an XML node in the http://marklogic.com/xdmp/map namespace.

Function Summary
map:clear Clear a map.
map:contains Returns true if the key exists in the map.
map:count Returns the number of keys used in the map.
map:delete Delete a value from a map.
map:entry Constructs a new map with a single entry consisting of the key and value specified as arguments.
map:get Get a value from a map.
map:keys Get the keys used in the map.
map:map Creates a map.
map:new Constructs a new map by combining the keys from the maps given as an argument.
map:put Put a value into a map at the given key.
Function Detail
map:clear(
$map as map:map
)  as   empty-sequence()
Summary:

Clear a map.

Parameters:
$map : A map.

Example:
  map:clear($table)
  => ()

map:contains(
$map as map:map,
$key as xs:string
)  as   xs:boolean
Summary:

Returns true if the key exists in the map.

Parameters:
$map : A map.
$key : A key.

Example:
  map:contains($table, "some-key")
  => fn:true()

map:count(
$map as map:map
)  as   xs:unsignedInt
Summary:

Returns the number of keys used in the map.

Parameters:
$map : A map.

Example:
  (: assume $table is bound to a map with 15 keys :)
  map:count($table)
  => 15
Example:
let $map := map:map()
let $_map := map:put($map, "a", 1)
let $_map := map:put($map, "a", 2)
let $_map := map:put($map, "b", 1)
let $_map := map:put($map, "b", 2)
return 
($map, map:count($map))
=>
map:map(
  <map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:map="http://marklogic.com/xdmp/map">
    <map:entry key="b">
      <map:value xsi:type="xs:integer">2</map:value>
    </map:entry>
    <map:entry key="a">
      <map:value xsi:type="xs:integer">2</map:value>
    </map:entry>
  </map:map>)
2


map:delete(
$map as map:map,
$key as xs:string
)  as   empty-sequence()
Summary:

Delete a value from a map.

Parameters:
$map : A map.
$key : A key.

Example:
  map:delete($table, "some-key")
  => ()

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

Constructs a new map with a single entry consisting of the key and value specified as arguments. This is particularly helpful when constructing used as part of an argument to map:new().

Parameters:
$key : The map key.
$value : The map value.

Example:
<result>{ map:entry("a","aardvark") }</result>

=>
<result>
  <map:map xmlns:map="http://marklogic.com/xdmp/map" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <map:entry key="a">
      <map:value xsi:type="xs:string">aardvark</map:value>
    </map:entry>
  </map:map>
</result>

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

Get a value from a map.

Parameters:
$map : A map.
$key : A key.

Example:
let $table := map:map()
let $_ := map:put($table, "some-key", <info>45683</info>)
return map:get($table, "some-key")
=> <info>45683</info>

map:keys(
$map as map:map
)  as   xs:string*
Summary:

Get the keys used in the map.

Parameters:
$map : A map.

Example:
  map:keys($table)
  => ("some-key", "another key", "and another one")

map:map(
[$map as element(map:map)]
)  as   map:map
Summary:

Creates a map.

Parameters:
$map (optional): A serialized map element.

Example:
  map:map()
  => map:map(<map xmlns="http://marklogic.com/xdmp/map"/>)
Example:
xquery version "1.0-ml";

let $map :=
<map:map xmlns:map="http://marklogic.com/xdmp/map">
 <map:entry key="2">
   <map:value xsi:type="xs:string"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    >world</map:value>
 </map:entry>
 <map:entry key="1">
   <map:value xsi:type="xs:string"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    >hello</map:value>
 </map:entry>
</map:map>
return
map:get(map:map($map), "2")
=>
world

map:new(
[$maps as map:map*]
)  as   map:map
Summary:

Constructs a new map by combining the keys from the maps given as an argument. If a given key exists in more than one argument map, the value from the last such map is used.

Parameters:
$maps (optional): The argument maps.

Example:
  map:new(())
  => map:map(<map xmlns="http://marklogic.com/xdmp/map"/>)
Example:
<result>{
  map:new((
    map:entry("a","aardvark"),
    map:entry("b","badger")
  ))
}</result>

=>
<result>
  <map:map xmlns:map="http://marklogic.com/xdmp/map" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <map:entry key="b">
      <map:value xsi:type="xs:string">badger</map:value>
    </map:entry>
    <map:entry key="a">
      <map:value xsi:type="xs:string">aardvark</map:value>
    </map:entry>
  </map:map>
</result>

map:put(
$map as map:map,
$key as xs:string,
$value as item()*
)  as   empty-sequence()
Summary:

Put a value into a map at the given key.

Parameters:
$map : A map.
$key : A key. If the key is not unique, it will overwrite the existing key.
$value : A value. If the value is the empty sequence, it will remove the key from the map.

Example:
let $map := map:map()
let $put := map:put($map, "some-key",
                  <info>45683</info>)
return
<result>{$map}</result>

=>
<result>
  <map:map xmlns:map="http://marklogic.com/xdmp/map" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <map:entry key="some-key">
      <map:value>
        <info>45683</info>
      </map:value>
    </map:entry>
  </map:map>
</result>