<div dir="ltr"><div dir="ltr"><br></div><div dir="ltr">Staying away from an issue for a few days can be quite clearing! When I revisited the task, I saw it right on! Or so I think...<div>This is the solution I came up with:</div><div><br></div><div><font face="monospace">declare variable $local:xml :=<br><xml><br>   <item id="e1" /><br>  <item id="e2" refid="e1" /><br>  <item id="e3" refid="e2" /><br>  <item id="e4" refid="e2" /><br>  <item id="e5" refid="e4" /><br>  <item id="e6" /><br>  <item id="e7" /><br>  <item id="e8" refid="e7" /><br>  <item id="e9" refid="e7" /><br>  <item id="e10" refid="e9" /><br>  <item id="e11" /><br></xml>;<br><br>declare function local:get-parents($item) { $local:xml/item[not(@refid != $item/@id)] };<br>declare function local:get-children($item) { $local:xml/item[@refid = $item/@id] };<br><br>declare function local:process-item($item) {<br>  <section><br>  {<br>    let $t := $item/(@*, local:get-children($item)/local:process-item(.))<br>    return $t<br>  }<br>  </section><br>};<br><br>let $root := local:get-parents($local:xml/item)<br>for $item in $root <br>return local:process-item($item)</font><br></div></div><br clear="all"><div>which produces:</div><div><br></div><div><font face="monospace"><?xml version="1.0" encoding="UTF-8"?><br><section id="e1"><br>   <section id="e2" refid="e1"><br>      <section id="e3" refid="e2"/><br>      <section id="e4" refid="e2"><br>         <section id="e5" refid="e4"/><br>      </section><br>   </section><br></section><br><section id="e6"/><br><section id="e7"><br>   <section id="e8" refid="e7"/><br>   <section id="e9" refid="e7"><br>      <section id="e10" refid="e9"/><br>   </section><br></section><br><section id="e11"/></font><br></div><div><br></div><div>and that seems to match the case. Thank you.</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature">Minden jót, all the best, Alles Gute,<br>Andreas Mixich</div></div>