XQuery while using distinct-values()

20,810

Solution 1

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

Solution 2

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.

Share:
20,810
Admin
Author by

Admin

Updated on April 23, 2020

Comments

  • Admin
    Admin about 4 years

    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?

  • Admin
    Admin about 12 years
    @Simeon-I got it what you want to say. Thank You
  • Admin
    Admin about 12 years
    @John-It worked and exactly as I was expecting. Thank you very