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

2019-12-30 Thread Andreas Mixich
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 :=


  
  
  
  
  
  
  
  
  
  
;

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:



   
  
  
 
  
   



   
   
  
   



and that seems to match the case. Thank you.

-- 
Minden jót, all the best, Alles Gute,
Andreas Mixich
___
talk@x-query.com
http://x-query.com/mailman/listinfo/talk

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

2019-12-21 Thread Andreas Mixich
Hello,

thank you for the example.

I am totally fine with the HOF style. I have access to XQ3.1 (BaseX and
Saxon-PE/EE with oXygen)

There is still a thing, that I need to solve, namely how to get   aligned,
that's why I will follow up to this in the next days, since I have drained
out for now. :-)

Thanks to Mr. Hager, was well.


-- 
Minden jót, all the best, Alles Gute,
Andreas Mixich
___
talk@x-query.com
http://x-query.com/mailman/listinfo/talk

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

2019-12-21 Thread Michael Kay
Start with a function that gets the children of an item:

declare variable $children := function($item) {return $xml / item[@refid = 
$item / @id ]};

Decide where to start:

declare variable $root := $xml / item[1];

Now process the items recursively:

declare function local:process-item($item, $get-children) {
   {
 $item / (@*,  $get-children($item) / process-item(.))
  }
};

and put it together like this:

local:process-item($root, $children);

I've deliberately written it this way using XQuery 3.1 higher-order functions 
to keep the recursion logic separate from the details of how you find the 
logical children of an item. But in XQuery 1.0 the same logic would work using 
a fixed function instead of a dynamic one.

A version that detects cycles in the data is a little bit trickier, but still 
quite doable.

Michael Kay
Saxonica

> On 21 Dec 2019, at 18:14, Andreas Mixich  wrote:
> 
> Hi,
> 
> I feel like I try to get a hold on a piece of wet soap with this...
> 
> Background: Atom Syndication has an extension[1], which allows threading of 
> entries. These entries are ordered in a flat sequence, one by one.
> As a result we end up with an Atom feed, that has a bunch of entries, where 
> each entry could have a reference to the ID of another entry, which would 
> then be it's parent.
> No nesting is done.
> 
> A simplified input could look like this:
> 
> declare variable $local:example :=  
> let $xml := 
>   
>   
>   
>   
>   
>   
> 
> 
> The task I want to accomplish is to create an output tree of nested sections, 
> resembling the natural flow of replies:
> 
> 
>   
> 
>   
>   
> 
>   
> 
>   
>   
> 
> 
> One of the many queries I tried is:
> 
> declare function local:rec($data) {
>   if (empty($data))
>   then ()
>   else (
>  let $current := head($data)
>  let $children := tail($data)[@refid = $current/@id]
>  return (
>   
>   { 
> $current/*
> (:  , prof:dump("current: " || $current/@id/data() || " 
> children: " || $children/@id/data() => string-join())
> :)
>   , for $child in $children
> return local:rec($children)
>   }
>   
>   , local:rec(tail($data))
> )
>)
> };
> 
>  
>   { local:rec($local:example/item) } 
> 
> 
> Of course, this has not yet any logic, to keep out the already processed 
> items (besides other issues). 
> When I tried that, however, by removing them from the return sequence, I 
> found no way to break out 
> of scope and have that modified return sequence go back to the next recursion.
> 
> Previous example results in this, btw.:
> 
> 
>   
> 
> 
>   
>   
>   
>   
> 
> 
> 
>   
> 
> 
> 
> 
> I can't believe, that there is no super easy way to do it. Any help would be 
> greatly appreciated!
> 
> -- 
> Minden jót, all the best, Alles Gute,
> Andreas Mixich
> ___
> talk@x-query.com
> http://x-query.com/mailman/listinfo/talk

___
talk@x-query.com
http://x-query.com/mailman/listinfo/talk