我使用的mondial数据库既可以作为SQL也可以作为XML使用,但我对我的问题感兴趣的地方如下:
<mondial>
<country car_code="S" area="449964" capital="cty-Sweden-Stockholm"
memberships="org-AfDB org-AG org-AsDB org-BIS org-CE
org-CBSS org-CCC org-ECE org-EBRD org-EIB
org-CERN org-ESA org-EU org-FAO org-G-6 org-G-9
org-G-10 org-IADB org-IAEA org-IBRD org-ICC
org-ICAO org-ICFTU org-Interpol org-IDA
org-IEA org-IFRCS org-IFC org-IFAD org-ILO
org-IMO org-Inmarsat org-IMF org-IOC org-IOM
org-ISO org-ICRM org-ITU org-Intelsat org-MTCR
org-NAM org-NC org-NIB org-ANC org-EN org-NSG
org-OECD org-OSCE org-PFP org-PCA org-UN
org-UNAVEM-III org-UNCRO org-UNESCO org-UNIDO
org-UNITAR org-UNIKOM org-UNMOGIP org-UNOMIG
org-UNHCR org-UNPREDEP org-UNPROFOR org-UNTSO
org-UPU org-WEU org-WFTU org-WHO org-WIPO
org-WMO org-WTrO org-ZC">
<name>Sweden</name>
<population>8900954</population>
<population_growth>0.56</population_growth>
<infant_mortality>4.5</infant_mortality>
<gdp_total>177300</gdp_total>
<gdp_agri>2</gdp_agri>
<gdp_ind>27</gdp_ind>
<gdp_serv>71</gdp_serv>
<inflation>2.6</inflation>
<government>constitutional monarchy</government>
<encompassed continent="europe" percentage="100"/>
<ethnicgroup percentage="83">Swede</ethnicgroup>
<ethnicgroup percentage="5">Finn</ethnicgroup>
<religion percentage="87">Protestant</religion>
<religion percentage="1.5">Roman Catholic</religion>
<language percentage="96">Swedish</language>
<language percentage="2.6">Finnish</language>
<border country="SF" length="586"/>
<border country="N" length="1619"/>
<name>Sweden</name>
<population>8900954</population>
<population_growth>0.56</population_growth>
<infant_mortality>4.5</infant_mortality>
<gdp_total>177300</gdp_total>
<gdp_agri>2</gdp_agri>
<gdp_ind>27</gdp_ind>
<gdp_serv>71</gdp_serv>
<inflation>2.6</inflation>
<government>constitutional monarchy</government>
<encompassed continent="europe" percentage="100"/>
<ethnicgroup percentage="83">Swede</ethnicgroup>
<ethnicgroup percentage="5">Finn</ethnicgroup>
<religion percentage="87">Protestant</religion>
<religion percentage="1.5">Roman Catholic</religion>
<language percentage="96">Swedish</language>
<language percentage="2.6">Finnish</language>
<border country="SF" length="586"/>
<border country="N" length="1619"/>
<province id="prov-Sweden-5" capital="cty-Sweden-3" country="S">
<name>Vastra Gotaland</name>
<area>24473</area>
<population>1464971</population>
<city id="cty-Sweden-3" is_state_cap="yes" country="S"
province="prov-Sweden-5">
<name>Goteborg</name>
<longitude>12</longitude>
<latitude>57.4</latitude>
<population year="95">449189</population>
<located_at watertype="sea" sea="sea-Kattegat"/>
<located_at watertype="river" river="river-Goetaaelv"/>
</city>
<city id="cty-Sweden-Vanersborg" country="S"
province="prov-Sweden-5">
<name>Vanersborg</name>
<longitude>12.2</longitude>
<latitude>58.2</latitude>
<population year="87">35804</population>
<located_at watertype="river" river="river-Goetaaelv"/>
<located_at watertype="lake" lake="lake-Vaenersee"/>
</city>
<city id="cty-Sweden-Mariestad" country="S"
province="prov-Sweden-5">
<name>Mariestad</name>
<longitude>13.5</longitude>
<latitude>58.4</latitude>
<population year="87">24255</population>
<located_at watertype="lake" lake="lake-Vaenersee"/>
</city>
</province>
<province id="prov-Sweden-3" capital="cty-Sweden-Karlskrona" country="S">
<name>Blekinge</name>
<area>2941</area>
<population>151168</population>
<city id="cty-Sweden-Karlskrona" is_state_cap="yes" country="S"
province="prov-Sweden-3">
<name>Karlskrona</name>
<longitude>15.3</longitude>
<latitude>56.1</latitude>
<population year="87">59007</population>
<located_at watertype="sea" sea="sea-Ostsee"/>
</city>
</province>
</country>
</mondial>
问题是,我想为以下问题编写一个查询:显示瑞典各省(län)的名称、人口和首府,按人口最多的省份排列。
到目前为止,我已经在XQuery中编写了以下内容:
let $mondial := db:open('mondial')
for $x in $mondial/mondial/country[name="Sweden"]
let $pop := $x/province/population
return $pop/string()
这给了我瑞典人口中所有运行良好的省份,然后我在查询中添加了以下内容:
let $mondial := db:open('mondial')
for $x in $mondial/mondial/country[name="Sweden"]
let $pop := $x/province/population
let $cname := $x/province/name
let $cap := $x/province/city[@is_state_cap="yes"]/name
return ($cname/string(), $pop/string(), $cap/string())
它也很好,给了我省份名称、人口和首都名称,但当我尝试以这种方式添加人口顺序时:
let $mondial := db:open('mondial')
for $x in $mondial/mondial/country[name="Sweden"]
let $pop := $x/province/population
let $cname := $x/province/name
let $cap := $x/province/city[@is_state_cap="yes"]/name
order by $pop
return ($cname/string(), $pop/string(), $cap/string())
BaseX给我错误
应为项,找到序列:(元素填充{……},…)
我似乎不明白为什么。我还尝试在排序之后添加/string()
,并尝试添加降序,但到目前为止没有成功。
order by
每次循环迭代需要一个项目。当你在所有国家而不是瑞典的省份进行循环时,你会得到瑞典每个省份的人口序列。
而是在各省上空盘旋。在下面的代码中,我更改了两件事:
- 循环使用省份而不是国家,因此也调整了子元素的检索(删除
/province
轴步骤) - 更改为合理的变量名称。
$x
是一个省的最差的变量名之一。如果你使用了可变名称,你甚至可能会自己意识到这个问题,因为第一行表示将国家分配给省份
let $mondial := db:open('mondial')
for $province in $mondial/mondial/country[name="Sweden"]/province
let $population := $province/population
let $name := $province/name
let $capital := $province/city[@is_state_cap="yes"]/name
order by $population
return ($name/string(), $population/string(), $capital/string())