How do I control the order of selection from a for $x in (...) ? In the last line of XQuery below, I want the result to be in the order $retmax, $retmin, but it comes out in reverse order.
<result>
{
let $max := max(doc("countries.xml")//country/(@population div @area))
let $min := min(doc("countries.xml")//country/(@population div @area))
for $country in doc("countries.xml")//country
let $density := $country/(@population div @area)
let $ret_min := if ($density = $min)
then <lowest density="{$density}">{data($country/@name)}</lowest>
else ()
let $ret_max := if ($density = $max)
then <highest density="{$density}">{data($country/@name)}</highest>
else ()
for $r in ($ret_max, $ret_min) return $r
}
</result>
produces:
<result>
<lowest density="0.026752619966905682">Greenland</lowest>
<highest density="31052.3125">Macau</highest>
</result>
but I want:
<result>
<highest density="31052.3125">Macau</highest>
<lowest density="0.026752619966905682">Greenland</lowest>
</result>
Here’s how I’d write it…