执行时:
let $country := doc("file.xml")/mondial/country
for $co in $country
for $c in $co/province
let $count := count($co/province/city)
where $count > 40
return(<country name="{($co/name)}">{($count)}</country>)
我们得到
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="France">41</country>
<country name="Spain">65</country>
<country name="Spain">65</country>
<country name="Spain">65</country>
<country name="Spain">65</country>
但是我们只想像这样返回一个国家及其$count
;
<country name="France">41</country>
<country name="Spain">65</country>
我们认为问题出在“为”。
我们如何返回一个唯一的行并跳过多个行?
评论:
一个小的XML输入示例
<country car_code="E"
area="504750"
capital="cty-Spain-Madrid"
memberships="org-AfDB org-CAN ">
<name>Spain</name>
<localname>el Reino de España</localname>
<population year="1991" measured="census">38871331</population>
<population measured="census" year="2001">40847371</population>
<population measured="census" year="2011">46815916</population>
<population_growth>0.78</population_growth>
<infant_mortality>2.7</infant_mortality>
<gdp_total>1356000</gdp_total>
<gdp_agri>3.1</gdp_agri>
<gdp_ind>26</gdp_ind>
<gdp_serv>70.8</gdp_serv>
<inflation>1.8</inflation>
<unemployment>26.3</unemployment>
<indep_date>1492-01-01</indep_date>
<government>parliamentary monarchy</government>
<encompassed continent="europe" percentage="100"/>
<ethnicgroup percentage="100">Mediterranean Nordic</ethnicgroup>
<religion percentage="68">Roman Catholic</religion>
<religion percentage="4.1">Muslim</religion>
<religion percentage="0.3">Protestant</religion>
<religion percentage="0.25">Jehovas Witnesses</religion>
<language percentage="74">Spanish</language>
<language percentage="19">Catalan</language>
<language percentage="5">Galician</language>
<language percentage="2">Basque</language>
<border country="AND" length="65"/>
<border country="F" length="623"/>
<border country="GBZ" length="1.2"/>
<border country="P" length="1214"/>
<province id="prov-Spain-2" country="E" capital="cty-Spain-5">
<name>Andalucía</name>
<area>87585</area>
<population measured="census" year="1991">6940105</population>
<population year="1994">7053043</population>
<population measured="census" year="2001">7357558</population>
<population measured="census" year="2011">8371271</population>
<city id="cty-Spain-5" country="E" province="prov-Spain-2">
<name>Sevilla</name>
<name>Seville</name>
<latitude>37.38</latitude>
<longitude>-5.99</longitude>
<elevation>12</elevation>
<population year="1991" measured="census">701927</population>
<population year="1994">714148</population>
<population year="2001" measured="census">684633</population>
<population year="2011" measured="census">698042</population>
<located_at watertype="river" river="river-Guadalquivir"/>
</city>
<city id="cty-Spain-34" country="E" province="prov-Spain-2">
<name>Almería</name>
<latitude>36.84</latitude>
<longitude>-2.47</longitude>
<elevation>27</elevation>
<population year="1991" measured="census">168025</population>
<population year="1994">167361</population>
<population year="2001" measured="census">166328</population>
<population year="2011" measured="census">189680</population>
<located_at watertype="sea" sea="sea-Mittelmeer"/>
</city>
<city id="cty-Spain-39" country="E" province="prov-Spain-2">
<name>Cádiz</name>
<latitude>36.53</latitude>
<longitude>-6.28</longitude>
<elevation>11</elevation>
<population year="1991" measured="census">143129</population>
<population year="1994">155438</population>
<population year="2001" measured="census">133363</population>
<population year="2011" measured="census">124014</population>
<located_at watertype="sea" sea="sea-Atlantic"/>
</city>
<city id="cty-Spain-13" country="E" province="prov-Spain-2">
<name>Córdoba</name>
<latitude>37.88</latitude>
<longitude>-4.77</longitude>
<elevation>120</elevation>
<population year="1991" measured="census">309961</population>
<population year="1994">315948</population>
<population year="2001" measured="census">308072</population>
<population year="2011" measured="census">328326</population>
<located_at watertype="river" river="river-Guadalquivir"/>
</city>
<city id="cty-Spain-16" country="E" province="prov-Spain-2">
<name>Granada</name>
<latitude>37.18</latitude>
<longitude>-3.6</longitude>
<elevation>738</elevation>
<population year="1991" measured="census">241471</population>
<population year="1994">271180</population>
<population year="2001" measured="census">240661</population>
<population year="2011" measured="census">241003</population>
</city>
我们的输入文件非常大,很难将其全部复制到此处。但这是一个省份中西班牙的一个很小的样本。我们有一个针对所有国家,省和城市的xml文件。因此,想象一下多种xml输入示例,但适用于其他国家和城市。
基本上,我们计算每个国家/地区拥有多少座城市,并以国家/地区的名称返回该数字。但是我们得到了多个相同的行。
答案 0 :(得分:1)
我认为,假设世界上每个国家只有一个country
元素,那么您只需要消除一个for
子表达式(无论如何您似乎都不会使用):>
let $country := doc("file.xml")/mondial/country
for $co in $country
let $count := count($co/province/city)
where $count > 40
return <country name="{($co/name)}">{($count)}</country>