[xquery-talk] reproducing nested structure

Fourny Ghislain gfourny at inf.ethz.ch
Sun Mar 13 19:42:32 PST 2011


Would the following function correspond to what you are looking for? Note that it makes the simplifying assumption that only elements are filtered, and non-element nodes are kept.

declare function local:filter($nodes, $matching-nodes) {
  for $node in $nodes
  let $filtered-children := local:filter($node/node(), $matching-nodes)
    typeswitch ($node)
    case element() return
      if ($node intersect $matching-nodes)
      then element {$node/name(.)} {$node/@*, $filtered-children}
      else $filtered-children
    default return

let $document := (: the document :) ()
let $matching-nodes := util:eval(concat("$document/",$query))
  local:filter($document, $matching-nodes)

Saxon (with the appropriate eval function) returned 

<?xml version="1.0" encoding="UTF-8"?>
<two foo="bar">
   <three foo="bar"/>
   <four foo="bar">
      <five foo="bar"/>

I agree with Michael Kay who points out that XSLT would be a better solution - actually, the function above, in some way, emulates XSLT behavior.

Kind regards,

More information about the talk mailing list