XQuery while using distinct-values()

user1388575 picture user1388575 · Jun 18, 2012 · Viewed 18.2k times · Source

XML File

<Cities>
  <Place>
    <City n="New Delhi"></City>
    <City n="Chandigarh"></City>
    <City n="Mumbai"></City>
  </Place>
  <Place>
    <City n="New Delhi"></City>
    <City n="Chandigarh"></City>
  </Place>
  <Place>
    <City n="New Delhi"></City>
    <City n="Mumbai"></City>
  </Place>
</Cities>

I am using following XQuery -

for $x in doc("sample")/Cities/Place/City
   order by $x/@n
   return distinct-values($x/@n)

The result I am expecting is - Chandigarh Mumbai New Delhi

but getting - Chandigarh Chandigarh Mumbai Mumbai New Delhi New Delhi New Delhi

Please tell me where am I going wrong?

Answer

John picture John · Jun 18, 2012

pls try this -

for $x in distinct-values(doc("sample")/Cities/Place/City/@n)
   order by $x
   return $x

I have checked the same with baseX 7.1 and working smoothly as expected by you :)