[xquery-talk] reproducing nested structure

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


Hello,

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)
  return
    typeswitch ($node)
    case element() return
      if ($node intersect $matching-nodes)
      then element {$node/name(.)} {$node/@*, $filtered-children}
      else $filtered-children
    default return
      $node
};

let $document := (: the document :) ()
let $matching-nodes := util:eval(concat("$document/",$query))
return
  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"/>
   </four>
</two>

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,
Ghislain




More information about the talk mailing list