XML 查询、排序导致 BaseX 错误 "Item expected, sequence found"
XML query, order by causing error "Item expected, sequence found" in BaseX
我正在使用 mondial database,它既可以作为 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 报错
Item expected, sequence found: (element population {....}, ...)
我似乎不明白为什么。
我还尝试在 order by 之后添加 /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())
我正在使用 mondial database,它既可以作为 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 报错
Item expected, sequence found: (element population {....}, ...)
我似乎不明白为什么。
我还尝试在 order by 之后添加 /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())