我刚刚开始使用xQuery作为数据库课程的一部分。对于本课程,我们使用此处找到的mondial数据库(https://www.dbis.informatik.uni-goettingen.de/Mondial/)。基本上,该数据库包含有关世界,国家,湖泊,河流等的信息。
我们的一项任务是占领所有拥有岛屿的湖泊区域,然后将其划分为非洲大陆。到目前为止,我已经这样做了:
let $lakes :=
for $lakeList in doc("mondial.xml")/mondial/lake[@island]
for $country in doc("mondial.xml")/mondial/country
where $lakeList/located/@country = $country/@car_code
return <lake area = "{$lakeList/area}" country = "{$country/name}" continent = "{$country/encompassed/@continent}" continentPerc = "{$country/encompassed/@percentage}"></lake>
return ($lakes)
此查询会生成以下结果:
<lake area="40.2" country="Denmark" continent="europe" continentPerc="100"/>
<lake area="56" country="United Kingdom" continent="europe" continentPerc="100"/>
<lake area="71" country="United Kingdom" continent="europe" continentPerc="100"/>
<lake area="10" country="Russia" continent="europe asia" continentPerc="25 75"/>
<lake area="670" country="Japan" continent="asia" continentPerc="100"/>
<lake area="1.8" country="Philippines" continent="asia" continentPerc="100"/>
<lake area="234.2" country="Philippines" continent="asia" continentPerc="100"/>
<lake area="0.3" country="Philippines" continent="asia" continentPerc="100"/>
<lake area="911" country="Philippines" continent="asia" continentPerc="100"/>
<lake area="354.6" country="Philippines" continent="asia" continentPerc="100"/>
<lake area="1103" country="Indonesia" continent="asia australia" continentPerc="78 22"/>
<lake area="0.04" country="Indonesia" continent="asia australia" continentPerc="78 22"/>
<lake area="11.3" country="Indonesia" continent="asia australia" continentPerc="78 22"/>
<lake area="537.5" country="Canada" continent="america" continentPerc="100"/>
<lake area="5542" country="Canada" continent="america" continentPerc="100"/>
<lake area="104" country="Canada" continent="america" continentPerc="100"/>
<lake area="38.7" country="Canada" continent="america" continentPerc="100"/>
现在求和的问题似乎是某些属性包含多个值。所以分组大陆是不可能的。因此,我希望拆分具有多个大陆和 continentPerc 的序列,以便该区域与相应的大陆相对应。
我的意思是:
<lake area="1103" country="Indonesia" continent="asia australia" continentPerc="78 22"/>
应该成为
<lake area="1103" country="Indonesia" continent="asia" continentPerc="78"/>
<lake area="1103" country="Indonesia" continent="australia" continentPerc="22"/>
这可以通过某种方式实现,还是应该选择另一种策略?
我非常感谢能得到的任何帮助和指示,因为我在Xquery非常糟糕。提前谢谢。
答案 0 :(得分:2)
我不熟悉那个数据库结构,我不确定我是否已经理解了这个问题(仍然想知道一个湖是否属于亚洲和澳大利亚),但是如果你直接提取大陆并将大陆上的元组流分组,在
let $countries := doc('mondial.xml')/mondial/country
for $lake in doc("mondial.xml")/mondial/lake[@island]
for $continent in $countries[@car_code = tokenize($lake/@country, '\s+')]/encompassed/@continent
group by $continent
order by $continent
return <continent name="{$continent}">{$lake/<lake name="{@id}" area="{area}"/>}</continent>
然后你得到
<continent name="america">
<lake name="lake-Hazen" area="537.5"/>
<lake name="lake-Nettilling" area="5542"/>
<lake name="lake-Lake_Manitou" area="104"/>
<lake name="lake-Mindemoya" area="38.7"/>
</continent>
<continent name="asia">
<lake name="lake-KoltsevoyeLake" area="10"/>
<lake name="lake-Biwa" area="670"/>
<lake name="lake-Pinatubo" area="1.8"/>
<lake name="lake-Taal" area="234.2"/>
<lake name="lake-TaalCrater" area="0.3"/>
<lake name="lake-LagunaDeBay" area="911"/>
<lake name="lake-Lanao" area="354.6"/>
<lake name="lake-Toba" area="1103"/>
<lake name="lake-DanauKumbang" area="0.04"/>
<lake name="lake-SegaraAnak" area="11.3"/>
</continent>
<continent name="australia">
<lake name="lake-Toba" area="1103"/>
<lake name="lake-DanauKumbang" area="0.04"/>
<lake name="lake-SegaraAnak" area="11.3"/>
<lake name="lake-LakeTaupo" area="622"/>
<lake name="lake-Wanaka" area="192"/>
</continent>
<continent name="europe">
<lake name="lake-Arresoe" area="40.2"/>
<lake name="lake-LoughNeagh" area="392"/>
<lake name="lake-LochNess" area="56"/>
<lake name="lake-LochLomond" area="71"/>
<lake name="lake-KoltsevoyeLake" area="10"/>
</continent>
尝试合并我提出的国家和百分比数据
let $countries := doc('mondial.xml')/mondial/country
for $lake in doc("mondial.xml")/mondial/lake[@island]
for $lake-country in $countries[@car_code = $lake/located/@country]
for $continent in $lake-country/encompassed
group by $continent-name := $continent/@continent
order by $continent-name
return
<continent name="{$continent-name}">{
$lake!<lake name="{@id}"
area="{area}"
country="{let $l := . return $countries[@car_code = $l/located/@country and encompassed[@continent = $continent-name]]/name}"
percentage="{let $l := . return $countries[@car_code = $l/located/@country]/encompassed[@continent = $continent-name]/@percentage}"/>
}</continent>
我希望有更优雅和紧凑的方式表达,但它似乎可以完成这项工作。