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”.
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>) )
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.
By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.