[xquery-talk] xquery distinct-values

Jane Doe becauseur at hotmail.com
Mon Apr 10 20:56:51 PDT 2006


No it's not Jane Doe :)  I forgot that I setup the hotmail account this way.
Thanks for your rapid replies, I will try the query you suggested.  Let me 
give you more information.  It was late last night and I'd spent the whole 
day on various queries.

Here's a better sample set:
file1.xml:
<people>
<driver>
  <name>John</name>
  <bus>1</bus>
  <bus>3</bus>
  <bus>4</bus>
  <bus>5</bus>
</driver>
<driver>
  <name>Sally</name>
  <bus>1</bus>
  <bus>3</bus>
  <bus>5</bus>
  <bus>6</bus>
</driver>
<driver>
  <name>Alex</name>
  <bus>3</bus>
  <bus>6</bus>
</driver>
<driver>
  <name>Bob</name>
  <bus>1</bus>
  <bus>2</bus>
  <bus>5</bus>
</driver>
</people>

file2.xml:
<buses>
  <vehicle id='1'>
    <type>van</type>
  </vehicle>
  <vehicle id='2'>
    <type>bus</type>
  </vehicle>
  <vehicleid='3'>
    <type>van</type>
  </vehicle>
  <vehicle id='4'>
    <type>van</type>
  </vehicle>
  <vehicle id='5'>
    <type>car</type>
  </vehicle>
  <vehicle id='6'>
    <type>car</type>
  </vehicle>
</buses>

"id" is just an attribute of element vehicle, not specifically an ID type 
(that I know of, I'm still learning xquery).  IDs are unique (defined as 
keys in xsd).  Names are also unique.  More than one driver can be assigned 
to the same vehicle.

My xquery:
for $x in doc("file1.xml")/people/driver
let $y := doc("file2.xml")/buses/vehicle[@id=$x/bus]
for $value in ($y/type)
where count($y/type[.=$value]) ge 2
order by ($x/name) descending
return
	<answer>
	<name>{data($x/name)}</name>

The results I get are as follows:
John is listed three times (3 of the same type), Sally is listed four times 
(2 for van, 2 for car) and the other two are not listed at all.  So I'm 
getting the correct results, just too many of the correct results!

I will try your suggestion, and let you know how it goes.

Thanks again for your quick response and your help.
D

>From: "Michael Kay" <mhk at mhk.me.uk>
>To: "'Jane Doe'" <becauseur at hotmail.com>,<talk at xquery.com>
>Subject: RE: [xquery-talk] xquery distinct-values
>Date: Mon, 10 Apr 2006 08:18:37 +0100
>
> > First, please accept my apologies if this is the wrong place to post.
>
>It's the right place to post. Is your name really Jane Doe?
>
>I think the following works:
>
>declare variable $f1 := doc("file1.xml");
>declare variable $f2 := doc("file2.xml");
>$f1/namelist/person
>   [count(distinct-values(for $x in carids return $f2/id($x)/style)) gt 1]
>   /name
>
>assuming that @id is an ID attribute
>
>Michael Kay
>http://www.saxonica.com/
>
> >
> > I have two xml files.
> >
> > file1.xml has
> > <namelist><person>
> > <name>John</name>
> > <carids>1</carids>
> > <carids>3</carids>
> > <carids>4</carids>
> > </person></namelist>
> >
> > file2.xml has
> > <carlist>
> > <car id='1'>
> > <style>sedan</style>
> > <car id='3'>
> > <style>coupe</style>
> > <car id='4'>
> > <style>sedan</style>
> > </carlist
> >
> > I am trying to write an xquery that will return the names
> > with more than 2
> > of any style:
> >
> > for $x in doc("file1.xml")/namelist/person
> > let $y := doc("file2.xml")/carlist/car[@id=$x/carids]
> > for $value in ($y/style)
> > where count($y/style[.=$value]) ge 2
> > order by ($x/name) descending
> > return <name>{data($x/name)}</name>
> >
> > This returns the names of everyone with more than 2 of any
> > style, but it
> > returns that same name multiple times (in relation to the number of
> > duplicate styles).
> >
> > I hope it's not too confusing, and I appreciate any help you
> > can give me.
> >
> > Thanks,
> > D
> >
> > _________________________________________________________________
> > Express yourself instantly with MSN Messenger! Download today
> > - it's FREE!
> > http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
> >
> > _______________________________________________
> > talk at xquery.com
> > http://xquery.com/mailman/listinfo/talk
> >
>

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/



More information about the talk mailing list