for $country in db:open('factbook')//country
where $country/@population > 100000000
let $name := $country/name[1]
for $city in $country//city[population > 1000000]
group by $name:= $country/name[1]
count $id
return <country id='{ $id }' name='{ $name }'>{ $city/name }</country>