【问题标题】:XQuery - List name and population of the most populous country on each continentXQuery - 列出每个大陆上人口最多的国家的名称和人口
【发布时间】:2020-05-03 19:27:04
【问题描述】:
我必须做以下练习,但我不知道这样做。
列出每个大陆上人口最多的国家的名称和人口。查询中禁止使用5大洲的id,即查询中不能出现“f0_119”、“f0_123”、“f0_126”、“f0_129”、“f0_132”。
这里有下载 XML 文件的链接:https://drive.google.com/file/d/1a_YYqW-uWYtGNBuMqqAiMu4vElGJbYCx/view?usp=sharing
这是我的代码。我有中国,但我需要展示其他大陆人口最多的国家俄罗斯、美国...有人可以帮帮我吗?
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】:
@pepe123656,您没有指定您使用的 XQuery 版本。这是使用 group by 子句的 XQuery 3.0 及更高版本的解决方案。
使用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>