Jedna uwaga: kolejności atrybutów się nie zgadzają.
xquery version "1.0"; declare function local:distance($from, $to, $doc, $dist) as node() { if (exists($from[name = $to/name]) ) then <country distance="{$dist}" name="{$to/name/text()}"/> else if ($dist > count($doc/country)) then <country distance="infinity" name="{$to/name/text()}"/> else let $neighbours := ( for $code in $from/border/@country return $doc/country[@car_code = $code] ) return local:distance($from union $neighbours, $to, $doc, $dist + 1) }; let $doc := doc("mondial-europe.xml")/mondial let $top_cities := ( for $city in $doc//city let $population := avg($city/population) order by number($population) descending return $city ) let $top30_cities := subsequence($top_cities, 1, 30) return <results author='Tomasz Maciejewski'> <result query='1'> { for $country in $doc/country let $name := $country/name/text() let $population := $country/population/text() let $gdp := $country/gdp_total/text() where exists($gdp) order by number($gdp) div number($population) descending return <country> <name>{$name}</name> <gdp>{$gdp}</gdp> <population>{$population}</population> </country> } </result> <result query='2'> { let $sea := $doc/sea[name="Mediterranean Sea"] for $code in tokenize($sea/@country, " ") return <country name="{$doc/country[@car_code=$code]/name}"/> } </result> <result query='3'> { for $city in $top30_cities let $country := $doc/country[province/city = $city] return <city> <name>{$city/name/text()}</name> <population>{xs:integer(avg($city/population))}</population> <country>{$country/name/text()}</country> </city> } </result> <result query='4'> { for $country in $doc/country let $cities := $country//city intersect $top30_cities where count($cities) gt 0 order by count($cities) descending return <country name="{$country/name/text()}"> { for $city in $cities return <city>{$city/name/text()}</city> } </country> } </result> <result query='5'> { for $country in $doc/country let $religion := $country/religions[@percentage>50] for $code in $country/border/@country let $other_country := $doc/country[@car_code=$code] let $other_religion := $other_country/religions[@percentage>50] where $country/name < $other_country/name and $religion ne $other_religion return <border> <country name="{$country/name/text()}" religion="{$religion/text()}"/> <country name="{$other_country/name/text()}" religion="{$other_religion/text()}"/> </border> } </result> <result query='6'> { for $group in distinct-values($doc//ethnicgroups) order by $group return <ethnicgroup name="{$group}"> { for $country in $doc/country[ethnicgroups = $group] let $percentage := $country/ethnicgroups[text() = $group]/@percentage order by number($percentage) descending return <country name="{$country/name/text()}" percentage="{$percentage}"/> } </ethnicgroup> } </result> <result query='7'> { let $poland := $doc/country[name = "Poland"] for $country in $doc/country let $dist := local:distance($poland, $country, $doc, 0) where $dist/@distance ne "infinity" order by number($dist/@distance) return $dist } </result> </results>