(: title: religion.xq author: parke godfrey creation: 2017/11/24 last version: 2021/03/23 dataset: Mondial III XML -------------------------------------------------------------------------- List each religion; order by name. Within each religion node, list the countries for which the leading religion by percentage is that religion. Order the countries then by percentage descending, country name (ascending). Notes * skip any country that does not report any religion * all religion nodes in the doc have a percentage attribute :) declare variable $mondial as xs:string := "https://www.eecs.yorku.ca" || "/course_archive/2020-21/W/4415" || "/assignment/xquery" || "/dataset/mondial-2015.xml"; { let $religions := { for $country in doc($mondial)//country[religion] let $high := max($country//religion/@percentage) let $rel := $country//religion[@percentage=$high][1] return {$country/name[1]/data()} {$rel} } for $rel in $religions//religion/data() group by $rel order by $rel return { for $country in $religions/country[religion/data()=$rel] let $percentage := xs:float($country/religion/@percentage/data()) let $cname := $country/name/data() order by $percentage descending, $cname ascending return } }