Product SiteDocumentation Site

8.6. libjn:intersect

This function returns an object that only contains keys shared by all input objects, and associates to each of these keys the corresponding values in the input objects (wrapped in an array if more than one). Non-objects are ignored.
declare function libjn:intersect($sequence as item()*) as object()
{
  {|
    let $objects := $sequence[. instance of object()]
    for $key in head($objects)()
    where every $object in tail($objects)
          satisfies exists(index-of($object(), $key))
    return { $key : $objects($key) }
  |}
};
Example 8.6. Intersecting objects.
Query:
libjn:intersect(
  (
    { "foo" : { "bar" : [ 1, 2 ] } },
    [ 1, 2, 3, 4 ],
    { "foo" : "bar", "bar" : "foo" }
    true(),
    1,
    jn:null()
  )
)
Result:
{ "foo" : [ { "bar" : [ 1, 2 ] }, "bar" ] }