Keith Ray Wyrich
Keith Ray Wyrich

Reputation: 21

Return mulitple values in for-loop with order by

I have two elements with same name in XML.
XQuery return gives error of sequence found

<baby>
    <name>america</name>
    <condtion>good</condtion>
    <style>4409</style>     
    <type>bear</type>
    <price>1.99</price>
    <birthday>none</birthday>
    <theme>inedependence day</theme>
    <theme>september 11</theme>
    <comment>none</comment>
</baby>

This is my XQuery expression:

for $x in collection("ty")/ty_babies/baby
where $x/theme!="none"
order by $x/theme
return concat($x/name,'     ',$x/theme)

I need XQuery to return both themes and maybe even more if more exist...

Upvotes: 2

Views: 53

Answers (1)

zx485
zx485

Reputation: 29042

Because you have two <theme> elements, you have to iterate over them like this (and put the order by in the second loop):

for $x in collection("ty")/ty_babies/baby
  where $x/theme!="none"
return 
  for $t in $x/theme 
    order by $t 
  return concat($x/name,'     ',$t,'&#xa;')

Output is:

america     inedependence day
america     september 11

To create some elements around your results, you can extend the above code to look like this:

for $x in collection("file:///home/kubuntu/Downloads/a.coll")/ty_babies/baby
  where $x/theme!="none"
return 
  <baby>
    {for $t in $x/theme 
       order by $t return 
    <theme>{concat($x/name,'     ',$t)}</theme>
    }
  </baby>

This returns

<baby><theme>america     inedependence day</theme><theme>america     september 11</theme></baby>

Upvotes: 2

Related Questions