[xquery-talk] Grouping without Duplicates Issues

Michael Kay mike at saxonica.com
Mon Jan 28 22:30:39 PST 2008

> where distinct-values(contains($para,'hey') or 
> contains($note,'hey')) 

I've tried to explain this to you before (several times) but the message
clearly hasn't sunk in, so let's try again.

distinct-values() is a function. It expects to get a sequence of atomic
values as its input (supplied in the parameter), and it returns a sequence
of atomic values as its output (supplied as the result). The result of the
function depends only on the value supplied as its argument. If you write an
expression as the argument of a function, the expression is evaluated to
produce a value, and that value is passed to the function.

In this case the expression that you have passed is

contains($para,'hey') or contains($note,'hey')

The result of contains() is always a single boolean value, the value true or
false. The result of combining two boolean values using "and" or "or" is
always a single boolean value. So the argument you are passing to
distinct-values() is either the value true, or the value false. In XDM a
single atomic value (in this case a boolean value) is the same as a sequence
containing that one value.

distinct-values() looks at the input sequence and eliminates duplicates,
returning those items from the input sequence that aren't duplicated. If the
input sequence is a singleton then it can't contain any duplicates, so
distinct-values() returns the singleton sequence unchanged.

Now, writing a function that does nothing might do no harm. But at the very
least, it suggests that you're confused about the logic. Your first step
should be to get rid of it. Then try to work out the right way of doing what
you thought this function might be doing.

I've spent five or ten minutes writing this response in the hope that you
will learn from it and not make the same mistake again, which will save
everyone time in the future. If you come back with another query showing the
same error in a week's time, I shall give up.

Michael Kay

More information about the talk mailing list