Xquery 控制序列中的选择顺序

Xquery 控制序列中的选择顺序,xquery,Xquery,如何控制(…)中$x的选择顺序?在下面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 $countr

如何控制(…)中$x的
选择顺序
?在下面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>

{
设$max:=max(doc(“countries.xml”)//country/(@population div@area))
设$min:=min(doc(“countries.xml”)//country/(@population div@area))
对于doc中的$country(“countries.xml”)//country
让$density:=$country/(@population div@area)
设$ret_min:=if($density=$min)
然后{data($country/@name)}
else()
设$ret_max:=if($density=$max)
然后{data($country/@name)}
else()
对于$r in($ret_max,$ret_min)返回$r
}   
产生:

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

澳门
格陵兰岛

以下是我的写作方法

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>
)
let$ordered\u countries:=对于文档中的$country(“countries.xml”)//country
让$density:=$country/(@population div@area)
按$密度订购
返回$country
let$low:=$ordered_国家[1]
let$high:=$ordered_countries[fn:last()]
返回(
{data($low/@name)},
{data($high/@name)}
)

这样复杂的代码必然会有一个逻辑错误——它不在
中,$r in($ret_max,$ret_min)返回$r
,顺便说一句,这是一种不自然的方式来编写
($ret_max,$ret_min)
——问题在于复杂的周围表达式。因此,这个问题的标题不恰当——更好的标题应该是:“请找出我逻辑中的错误”——这不符合XQuery相关问题的要求。我试图
返回($ret\u max,$ret\u min)
,但没有起作用。Rose Perrone,尝试这个查询:
对于$r in(7,2)返回$r
结果是
7 2
。尝试相同的方法,但将
7
2
替换为节点值——同样,结果是按照节点在表达式中出现的顺序生成节点。这并不令人惊讶,它遵循Xpath数据模型中的序列定义。这意味着,在您的问题表达式中提供的中,围绕$r in($ret\u max,$ret\u min)的子表达式的逻辑存在一些问题。