XML查询,通过导致错误排序"项目预期,序列找到"在BaseX

时间:2015-03-22 13:52:48

标签: xml xquery basex

我正在使用可用作SQL和XML的mondial database,但我对我的问题感兴趣的是:

<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(),并尝试添加降序但到目前为止没有运气。

1 个答案:

答案 0 :(得分:1)

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())