XQuery - 列出每个大陆上人口最多的国家的名称和人口。

问题描述 投票:0回答:1

我必须做下面的练习,但我不知道做它。

列出各大洲人口最多的国家的名称和人口。在查询中禁止使用5大洲的id,即 "f0_119"、"f0_123"、"f0_126"、"f0_129"、"f0_132 "都不能出现在查询中。

这里有下载XML文件的链接。https:/drive.google.comfiled1a_YYqW-uWYtGNBuMqqAiMu4vElGJbYCxview?usp=共享。

这是我的代码。我有中国,但我需要显示其他大陆人口最多的国家... ...俄罗斯,美国... ...。谁能帮帮我,好吗?

for $var in /mondial/country
let $max:= max(/mondial/country/@population)
where $var/@population = $max
return concat(data($var/name), ' - ',data($var/@population))
xml xquery xquery-sql
1个回答
0
投票

@pepe123656,你没有说明你用的是什么XQuery版本。这里是XQuery 3.0及以后的解决方案,使用的是 group by 子句。

encompassed[...] 谓词的使用是因为一些国家,如俄罗斯,土耳其等,是在两个大陆上。

XQuery

<root>{
    for $country in doc('e:\temp\mon.xml')/mondial/country
    let $population := $country/@population
    (: Turkey, Russia, etc. are on two continents :)
    (: We select the highest land percentage continent :)
    group by $continentID := $country/encompassed[xs:double(@percentage) eq max($country/encompassed/@percentage)]/@continent
    order by $continentID
    return <row>
      {
        $country/../continent[@id = $continentID]/@name || ": " || $country[@population = max($population)]/@name || ": " || xs:integer(max($population))
      }
      </row>
}</root>

产量

<root>
  <row>Europe: Germany: 83536112</row>
  <row>Asia: China: 1210004992</row>
  <row>America: United States: 266476272</row>
  <row>Australia/Oceania: Australia: 18260864</row>
  <row>Africa: Nigeria: 103912488</row>
</root>
© www.soinside.com 2019 - 2024. All rights reserved.