Question

New to XQuery and probably a noob q. I installed a BaseX db as my sandbox (which included a sample file etc/factbook.xml). I constructed a simple query which I thought would return all 'cities' with population > 10million.

for $x in doc("etc/factbook.xml")/mondial/country
  where $x/city/population > 10000000.0
return $x/city

but I'm getting cities with lower populations, any insight?

<city id="f0_1726" country="f0_553" longitude="126.967" latitude="37.5667">
  <name>Seoul</name>
  <population year="95">10229262</population>
</city>
<city id="f0_10300" country="f0_553">
  <name>Kunsan</name>
  <population year="95">266517</population>
</city>
 (I've only included first two but many more both < and > 10million)
Was it helpful?

Solution

You're returning all countries that have a city with population larger than 10 millions. Loop over the cities instead (and please, use meaningful variable names):

for $city in doc("etc/factbook.xml")/mondial/country/city
where $city/population > 10000000
return $city

Or just go for an XPath expression doing the same:

doc("etc/factbook.xml")/mondial/country/city[population > 10000000]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top