Suppose there is this XML file:
<Adam>
<JackSons>
<s1>Jack</s1>
<s2>Sara</s2>
</JackSons>
<PeterSons>
<s1>Peter</s1>
<n>
<s2>Anna</s2>
</n>
</PeterSons>
<DavidSons>
<q>
<s1>David</s1>
</q>
<n>
<s2>Suzann</s2>
</n>
</DavidSons>
</Adam>
I would like to get s2
which is the sibling or niece of each s1 to call a function on both of them inside the loop, please note we don't know anything about the structure of XML except that each s1
and s2
have a common ancestor, I tried
let $db := doc('test2.xq')
for $s1 in $db//s1
let $s2 := $db//*[//$s1]/(s2, */s2)
return <p> {$s1/text()} marry {$s2/text()} </p>
It returns
<p>Jack marry SaraAnnaSuzann</p>
<p>Peter marry SaraAnnaSuzann</p>
<p>David marry SaraAnnaSuzann</p>
I expect
<p>Jack marry Sara</p>
<p>Peter marry Anna</p>
<p>David marry Suzann</p>
Depending on the complexity of your text's within s1 and s2, something as simple as the following might be enough for you:
I would need to see more real data to know if you needed something more complex, however this should work for the examples you have posted.