11
votes

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?

4

4 Answers

14
votes

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 :)

6
votes

You are now calling distinct-values on each of the values separately. distinct-values returns the distinct values in a sequence but the sequence now only consists of one element. You should call distinct-values(...) where ... is the sequence of city names.

0
votes

The distinct-values function

let $items := (1,2,4,4,5,5,9,9,9,9,3,3,2)
let $unique-items-by := distinct-values($items)
return
   <result>   

      <items>
      {
         for $item in $unique-items-by
         return <item>{$item}</item>
      }
      </items>

   </result>
0
votes

The distinct-values function is used
unique-items

 let  $x:=doc("/db/my.xml")
let $unique-items := distinct-values($x)
 for $x in $unique-items

return (
 $unique-items
)