project

This function iterates on the input sequence. It projects objects by filtering their pairs and leaves non-objects intact.

declare function project($seq as item*, $keys as string*) as item*
{
  for $item in $seq
  return typeswitch ($item)
         case $object as object return
         {|
           for $key in keys($object)
           where some $to-project in $keys satisfies $to-project eq $key
           let $value := $object.$key
           return { $key : $value }
         |}
         default return $item
};
      

Example 187. Projecting an object 1

let $o := {
  "Captain" : "Kirk",
  "First Officer" : "Spock",
  "Engineer" : "Scott"
  }
return project($o, ("Captain", "First Officer"))
        

Result (run with Zorba): { "Captain" : "Kirk", "First Officer" : "Spock" }


Example 188. Projecting an object 2

let $o := {
  "Captain" : "Kirk",
  "First Officer" : "Spock",
  "Engineer" : "Scott"
  }
return project($o, "XQuery Evangelist")
        

Result (run with Zorba): { }