对于下面的XML数据,我试图得到这里显示的输出:即我想看到人口超过20000的国家的名称,条件是显示的城市数量应仅适用于那些人口超过3500.此外,对于一些国家,这个城市在一个省内。
<result>
<country name="B">
<num_cities>3</num_cities>
</country>
<country name="C">
<num_cities>2</num_cities>
</country>
</result>
---------------------------这是XML数据--------------- -------
<country id="1" name="A" population="12000">
<name>A</name>
<city id="c1" country="1">
<name>T1</name>
<population>5000</population>
</city>
<city id="c2" country="1">
<name>T2</name>
<population>3000</population>
</city>
<city id="c3" country="1">
<name>T3</name>
<population>4000</population>
</city>
</country>
<country id="3" name="B" population="80000">
<name>B</name>
<city id="c4" country="2">
<name>T4</name>
<population>6000</population>
</city>
<city id="c5" country="2">
<name>T5</name>
<population>2000</population>
</city>
<city id="c6" country="2">
<name>T6</name>
<population>60000</population>
</city>
<city id="c7" country="2">
<name>T7</name>
<population>12000</population>
</city>
</country>
<country id="3" name="C" population="68000">
<name>C</name>
<province>P1</province>
<city id="c8" country="3">
<name>T8</name>
<population>51000</population>
</city>
<city id="c9" country="3">
<name>T9</name>
<population>3000</population>
</city>
<city id="c10" country="3">
<name>T10</name>
<population>14000</population>
</city>
</country>
我写了这个xquery,但我不知道如何排除有人口的城市&gt; 3500.我可能没有正确编写代码......请协助。
for $c in doc("abc")//country
let $city:= count($c/city/name)
let $citypr:= count($c/province/city/name)
where $c/@population>1000000
return
<result>
<country name='{data($c/name) }'></country>
<num_of_cities>
{
if (exists ($c/city/name)) then
$city
else
$citypr
}
</num_of_cities>
</result>
答案 0 :(得分:0)
一些提示:
请勿在检索内容的说明符中尽早使用where
条款进行过滤。
如果你想只有一个<result>
,而不是每个数据一个,你需要在之前启动FLWOR表达式;每件物品都会退回一次。
这是一个更接近的例子:
<result>{
for $large-country in doc("abc")//country[@population > 20000]
let $large-cities := $country/city[population > 3500]
return
<country name="{$large-country/@name}">
<num_cities>{count($large-cities)}</num_cities>
</country>
}</result>