[xquery-talk] Building a tree from sequence of maps

Andreas Mixich mixich.andreas at gmail.com
Mon Dec 30 22:46:37 PST 2019

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...
This is the solution I came up with:

declare variable $local:xml :=
<item id="e1" />
  <item id="e2" refid="e1" />
  <item id="e3" refid="e2" />
  <item id="e4" refid="e2" />
  <item id="e5" refid="e4" />
  <item id="e6" />
  <item id="e7" />
  <item id="e8" refid="e7" />
  <item id="e9" refid="e7" />
  <item id="e10" refid="e9" />
  <item id="e11" />

declare function local:get-parents($item) { $local:xml/item[not(@refid !=
$item/@id)] };
declare function local:get-children($item) { $local:xml/item[@refid =
$item/@id] };

declare function local:process-item($item) {
  let $t := $item/(@*, local:get-children($item)/local:process-item(.))
    return $t

let $root := local:get-parents($local:xml/item)
for $item in $root
return local:process-item($item)

which produces:

<?xml version="1.0" encoding="UTF-8"?>
<section id="e1">
   <section id="e2" refid="e1">
      <section id="e3" refid="e2"/>
      <section id="e4" refid="e2">
         <section id="e5" refid="e4"/>
<section id="e6"/>
<section id="e7">
   <section id="e8" refid="e7"/>
   <section id="e9" refid="e7">
      <section id="e10" refid="e9"/>
<section id="e11"/>

and that seems to match the case. Thank you.

Minden jót, all the best, Alles Gute,
Andreas Mixich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://x-query.com/pipermail/talk/attachments/20191231/4e01c1bf/attachment.html>

More information about the talk mailing list