recursive join??

I’ve got a collection of documents like the following:


etc. The chain of connections via the has-part elements can be arbitrarily long (20 - 30 in reality). I’d like to get back all the has-part documents related to my root document, something like




,
which I can do easily enough with a script, but am having no luck formulating an xquery to do so. Even a flat result (e.g. ) would be okay as long as it contains all the linked documents. Any suggestions?

Thanks in advance for anyone’s help.

I would try something like this (just off the top of my head; I haven’t tested it or anything):

declare function local:get-parts($id as xs:string) as node()
{
    let $doc := input()/doc[@id = $id]
    for $has-part in $doc/has-part
    let $next-id := $has-part/@id
    return <has-part id="{$next-id}">
        {local:get-parts($next-id)}
    </has-part>
}

let $doc := input()/doc[@id = 'x']
return <doc id="x">
    {for $part in $doc/has-parts
      return local:get-parts($part/@id)}
</doc>

(I should note that this requires Tamino 4.4; Tamino didn’t support user defined functions like this in earlier versions.)

Thankfully, we are running 4.4. And your query works beautifully. Thanks for the help.