I've seen that it's possible to do a cartesian product in XPath using a For Expression. For instance, given the following input:
<people>
<group>
<her>Alex</her>
<him>Bert</him>
<her>Cora</her>
<him>Drew</him>
</group>
<group>
<her>Edna</her>
<him>Fred</him>
<him>Gary</him>
</group>
<group>
<her>Hope</her>
</group>
</people>
I can pair every her with every him using:
for $her in /people/group/her,
$him in /people/group/him
return ($her/text(), $him/text())
Since both binding sequences share (unique) root, I thought that the following expression should be equivalent to the previous one:
/people/(for $her in group/her,
$him in group/him
return ($her/text(), $him/text()))
Although this expression is parsed nicely, it produces (in my understanding) an unexpected result when executed on an online XPath tester. I've been skimming the specification and it seems that this expression shouldn't be valid.
Now, I'd like to be able to produce the cartesian product relative to a <group>
tag. I mean, I'd like to do the cartesian product between hers and hims living in the very same group. If nested for expressions were viable, it should be something like this:
/people/group/(for $her in her,
$him in him
return ($her/text(), $him/text()))
But again, this isn't producing the expected result.
Having said so, I have a few questions:
Is it valid to nest a for expression in a path? If so, why are the first and the second expressions producing different results?
If 1) isn't possible, what's the standard way of producing the cartesian product relative to the
<group>
tag?
Thank you!