The following code groups your dvd
elements by the studio name and outputs the studio name and all the dvd titles belonging to it. The XPath /dvdstore/review[title = $dvd/title]/comments
returns all all reviews belonging to any of the titles of the studio.
Please note, that instead of using all the time the doc()
function I bound the context item. The other one might have the drawback that the file is actually opened multiples times (although the optimizer should be able to detect that and just open it once).
declare context item := doc("dvdtitles.xml");
<Studios>
{
for $dvd in /dvdstore/dvd
let $studio := $dvd/studioproduction
group by $studio
return
<studio>
<studioproduction>{$studio}</studioproduction>
<dvdtitles>{$dvd/title}</dvdtitles>
<comments>{
/dvdstore/review[title = $dvd/title]/comments
}</comments>
</studio>
}
</Studios>