Product SiteDocumentation Site

8.4. libjn:descendant-pairs

This function returns all descendant pairs within the supplied items.
        declare function libjn:descendant-pairs($sequence as item()*)
        {
          for $item in $sequence
          return typeswitch ($item)
          case object() return
            for $key in jn:keys($item)
            let $value := $item($key)
            return (
              { $key : $value },
              libjn:descendant-pairs($value)
            )
          case array() return
            libjn:descendant-pairs($item())
          default return ()
        };
Example 8.3. Accessing all descendant pairs
Query:
libjn:descendant-pairs(
  (
    { "foo" : { "bar" : [ 1, 2 ] } },
    [ [ { "foo" : "bar", "bar" : "foo" } ] ],
    true(),
    1,
    jn:null()
  )
)
Result:
{ "foo" : { "bar" : [ 1, 2 ] } }
{ "bar" : [ 1, 2 ] }
{ "foo" : "bar" }
{ "bar" : "foo" }

Example 8.4. Query all pairs with a given name.
Query:
libjn:descendant-pairs({
  "first" : 1,
  "second" : { 
    "first" : "a", 
    "second" : "b" 
  }
})("first")
Result:
        1 a