This version preserves the original attributes:
declare function local:link($entries as node()*, $id as xs:string) { let $entry := $entries[@id eq $id], $children := $entries[@parentId eq $id] return element entry { $entry/@*, for $child in $children return local:link($entries, $child/@id) } };
Leo
Am 21.01.2011 14:26, schrieb Leonard Wörteler:
Hi Erdal,
try this one (closely following Jan's idea):
declare function local:link($entries as node()*, $id as xs:string) { let $children := $entries[@parentId eq $id] return <entry id='{$id}'>{ for $child in $children return local:link($entries, $child/@id) }</entry> };
let $entries :=
<entries> <entry id="entry1" /> <entry id="entry2" parentId="entry1" /> <entry id="entry3" parentId="entry1" /> <entry id="entry4" parentId="entry2" /> <entry id="entry5" parentId="entry2" /> <entry id="entry6" parentId="entry3" /> <entry id="entry7" parentId="entry3" /> </entries> return local:link($entries/entry, 'entry1')
It should be noted that circular references would lead to non-termination. To detect and prevent that, you could pass a sequence of already linked IDs in another argument.
Hope that helps, cheers Leo :-)