You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XQuery作业求助:获取人口增幅最大及最小的大洲数据

解决XQuery中获取人口增幅极值大洲的问题

嘿,你这作业已经快收尾啦!既然已经把各洲的人口数据都存在$minAndMaxCont变量里了,那提取增幅最大和最小的大洲其实很容易,用XQuery的聚合函数就能搞定。

核心思路

注意@increase属性默认是字符串类型,所以第一步要把它转换成数值类型,再用max()和min()函数定位到极值,最后匹配对应的<Continent>元素即可。

具体代码实现

  1. 获取增幅最大的大洲:
let $max_increase_continent := 
    $minAndMaxCont/Continent[number(@increase) = max($minAndMaxCont/Continent/number(@increase))]
return $max_increase_continent
  1. 获取增幅最小的大洲:
let $min_increase_continent := 
    $minAndMaxCont/Continent[number(@increase) = min($minAndMaxCont/Continent/number(@increase))]
return $min_increase_continent
  1. 如果需要一次性输出两者的结构化信息,合并代码更高效:
let $max_cont := $minAndMaxCont/Continent[number(@increase) = max($minAndMaxCont/Continent/number(@increase))]
let $min_cont := $minAndMaxCont/Continent[number(@increase) = min($minAndMaxCont/Continent/number(@increase))]
return
    <PopulationResult>
        <HighestIncrease>
            <ContinentName>{$max_cont/@name}</ContinentName>
            <CurrentPopulation>{$max_cont/@pop}</CurrentPopulation>
            <FuturePopulation>{$max_cont/@futurePop}</FuturePopulation>
            <TotalIncrease>{$max_cont/@increase}</TotalIncrease>
            <IncreaseRatio>{$max_cont/@ratio}</IncreaseRatio>
        </HighestIncrease>
        <LowestIncrease>
            <ContinentName>{$min_cont/@name}</ContinentName>
            <CurrentPopulation>{$min_cont/@pop}</CurrentPopulation>
            <FuturePopulation>{$min_cont/@futurePop}</FuturePopulation>
            <TotalIncrease>{$min_cont/@increase}</TotalIncrease>
            <IncreaseRatio>{$min_cont/@ratio}</IncreaseRatio>
        </LowestIncrease>
    </PopulationResult>

这段代码不仅能精准筛选出目标大洲,还会把关键数据整理成更清晰的结构输出,完全贴合作业要求~

内容的提问来源于stack exchange,提问作者Augman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:56:12