[xquery-talk] distinct-values in Where Clauses

Andrew Welch andrew.j.welch at gmail.com
Fri Jan 18 10:33:41 PST 2008

On 18/01/2008, Ronald Bourret <rpbourret at rpbourret.com> wrote:
> I'm not entirely sure what it is you want, but I have a few comments:
> 1) Why are you using nested for loops? The following clause:
>     for $find in doc("resume.xml")//div, $head in $find/head, $desc in
> $find/p
>     do something
> is similar to:
>     for i=1 to 10
>        for j=1 to 20
>           for k=1 to 30
>             do something
> That is, the code finds each div. For each div, it finds all the head
> children. And for each head child, it finds all the p children. It then
> executes the return clause once for each p child.

Just to add to that description - it's called the Cartesian product
and generates all possible combinations of the input values, eg:

for $a in 1 to 2, $b in ('a', 'b')
return concat($a, $b)


1a 1b 2a 2b

It might be easier to visualise when its two simple sequences like
that.  Generally I don't think you want to use a Cartesian product
unless you know what you're doing...  Maybe a seasoned xquery'er can
provide a couple of common use cases where a Cartesian product is the
ideal solution?

Andrew Welch
Kernow: http://kernowforsaxon.sf.net/

More information about the talk mailing list