[xquery-talk] XQuery and id()/idref(); Controlling the children of nodes in the result sequence

Michael Kay mike at saxonica.com
Wed Apr 23 19:53:01 PDT 2008

>   So if I am right, you want to not include an element if one 
> of its ancestors is included too?  If you want so, use the 
> following instead of the for:
>     let $e := $d/a:collection/a:entry/a:data
>                 // *[@a:span = $s/@xml:id]
>       return
>         $e[not(ancestor::* = $e)]

That's not quite right, because the "=" tests for equality rather than

You could do

      let $e := $d/a:collection/a:entry/a:data
                 //*[@a:span = $s/@xml:id]
         $e[not(ancestor::* intersect $e)]

or you could do a recursive function which stops searching any deeper when
it finds a match:

declare function local:s($e as element(), $id as xs:string) as element()* {
  $e/(if (@a:span eq $id)
      then .
      else */local:s(., $id))

Michael Kay

More information about the talk mailing list