Problem

Sometimes you need to see if two maps are equal but don’t want to loop through all the keys and compare them. If you do an equals you’ll get an error called XDMP-COMPARE saying “Items not comparable”.

Solution

Applies to MarkLogic versions 7+

If you serialize the map into XML, then you can use fn:deep-equal(). Here is an example of how this can be done.

let $mapA := 
  map:new((
    map:entry("a", "aardvark"),
    map:entry("b", "badger")
  ))
let $mapB := 
  map:new((
    map:entry("a", "aardvark"),
    map:entry("b", "badger")
  ))
let $mapC := 
  map:new((
    map:entry("c","candidate") 
  ))
return
 (
   (: ($mapA eq $mapB), will cause the XDMP-COMPARE error :)
   fn:deep-equal(<x>{$mapA}</x>, <x>{$mapB}</x>),
   fn:deep-equal(<x>{$mapA}</x>, <x>{$mapC}</x>)
 )

Discussion

MarkLogic represents maps as XML, so

map:new((
  map:entry("a", "aardvark"),
  map:entry("b", "badger")
))

becomes…

<map:map xmlns:map="https://marklogic.com/xdmp/map" 
  xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xs="https://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>

With that XML representation, fn:deep-equal() is able to make the comparison.

Learn More

Application Developer's Guide

Read the methodologies, concepts, and use cases related to application development in MarkLogic Server, with additional resources.

MarkLogic Developer Learning Track

Want to build that awesome app? Get off the ground quickly with the developer track, with instructor-led and self-paced courses.

Getting Started Video Tutorials for Developers

This series of short videos tutorials takes developers who are new to MarkLogic from download to data hub, fast.

This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.