如何从for $x in (...)
控制选择顺序?在下面的XQuery的最后一行中,我希望结果符合$retmax
,$retmin
的顺序,但它的顺序相反。
<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>
产生
<result>
<lowest density="0.026752619966905682">Greenland</lowest>
<highest density="31052.3125">Macau</highest>
</result>
但我想:
<result>
<highest density="31052.3125">Macau</highest>
<lowest density="0.026752619966905682">Greenland</lowest>
</result>
答案 0 :(得分:1)
以下是我写它的方式......
let $ordered_countries := for $country in doc("countries.xml")//country
let $density := $country/(@population div @area)
order by $density
return $country
let $low := $ordered_countries[1]
let $high := $ordered_countries[fn:last()]
return (
<lowest density="{$low/(@population div @area)}">{data($low/@name)}</lowest>,
<highest density="{$high/(@population div @area)}">{data($high/@name)}</highest>
)