[xquery-talk] Using Union Operator without Duplicate Records

Michael Kay mike at saxonica.com
Sun Dec 30 10:04:00 PST 2007


> 
>   This is what I have now:
> 
> {
> for $file in doc("resume_2.xml")//author 
    let  $author :=  distinct-values($file[./@type='co-author' ] 
                                       union
                                     $file[./@type='Faculty'])
> order by $author ascending
> return  <li>{data($author)}</li>
> }
> 

I wonder why you chose to call your variable $file when its value is an
<author> element? This poor choice of name may have led to your subsequent
mistakes. Because $file is declared in a "for" expression, its value is
always a single item, in this case a single author element. Applying a
predicate to $file (as in $file[...]) will either return that author element
(if the predicate is true), or an empty sequence (if it is false). Applying
the union operator ($file[C1] union $file[C2]) is equivalent to writing
($file[C1 or C2]), which again selects either the author element or nothing.
Applying distinct-values to a singleton author element achieves nothing.

I hope this helps you to understand why your code cannot possibly be right.
I'm afraid I haven't tried to understand the problem you are trying to
solve, so I can't tell you how to correct it.

Michael Kay
http://www.saxonica.com/




More information about the talk mailing list