[xquery-talk] Group by - problems!!!

Michael Kay mhk at mhk.me.uk
Mon Jan 17 20:45:53 PST 2005

> let $doc := doc("Big.xml")
> for $a in distinct-values($doc//OTY)
> let $uniquename := for $g in $a
>                       where $g = $a
>                       return $g

This is meaningless. $a is a single value (one of the distinct values), so
you only iterate over this for loop once: you assign $g to the value of $a
and then test whether it is equal to $a, which it always will be (except
pathologically when $a is NaN). So this is the same as

  let $uniquename := $a

You say you're trying to get unique element names, but there's nothing here
that gets an element name - $a selects atomic values, therefore $uniquename
is also an atomic value.

> let $occurences := for $g2 in $doc//OTY
>                        where $g2[text() eq $a]
>                        return $g2

This is a rather long-winded way of doing:

  let $occurrences := $doc//OTY[text() eq $a]

which would be better written

  let $occurrences := $doc//OTY[. eq $a]

(to allow for elements whose text is divided by comments or processing

This selects the OTY elements equal to one of the distinct values - i.e. the
members of one group.

> let $links := for $x in $doc
>                        where $x//OTY = $a
>                   return $x//AID[@id]

$doc is a single node, so there's not much point iterating over it. The
condition is always true: there is always an OTY somewhere in the document
whose value is equal to $a. So this is equivalent to:

let $links := $doc//AID[@id]

>     return
>       <result>
>         <name>{ $uniquename }</name>
>         <occurences>{ count($occurences) } </occurences>
>         <links>{ $links) } </links>
>       </result>
>   }
> Basically all i am trying to do is to get a unique element name along
> with its number of occurences , that all is perfect and works 100%.
> Then i want to find out which record that appeared in and get the
> corresponding record id($links, as of now it matches all records for
> all unique elements) , thats where i am failing, need a fresh set of
> eyes to see this, i am going absolutely mad!!!! thank you in advance!

I'm afraid without seeing your XML structure I can't really tell what you
are trying to achieve here.

More information about the talk mailing list