title: XML query, order by causing error "Item expected, sequence found" in BaseX
body: <p>I'm using the <a href="https://www.kth.se/social/files/54f4817bf27654358032133f/mondial.xml" rel="nofollow">mondial database</a> that is available both as SQL and as XML, but the bit I'm interested in for my question is following:</p>
  
  <pre><code>&lt;mondial&gt;
    &lt;country car_code="S" area="449964" capital="cty-Sweden-Stockholm" 
             memberships="org-AfDB org-AG org-AsDB org-BIS org-CE
                          org-CBSS org-CCC org-ECE org-EBRD org-EIB 
                          org-CERN org-ESA org-EU org-FAO org-G-6 org-G-9 
                          org-G-10 org-IADB org-IAEA org-IBRD org-ICC 
                          org-ICAO org-ICFTU org-Interpol org-IDA 
                          org-IEA org-IFRCS org-IFC org-IFAD org-ILO  
                          org-IMO org-Inmarsat org-IMF org-IOC org-IOM  
                          org-ISO org-ICRM org-ITU org-Intelsat org-MTCR  
                          org-NAM org-NC org-NIB org-ANC org-EN org-NSG  
                          org-OECD org-OSCE org-PFP org-PCA org-UN  
                          org-UNAVEM-III org-UNCRO org-UNESCO org-UNIDO  
                          org-UNITAR org-UNIKOM org-UNMOGIP org-UNOMIG  
                          org-UNHCR org-UNPREDEP org-UNPROFOR org-UNTSO  
                          org-UPU org-WEU org-WFTU org-WHO org-WIPO  
                          org-WMO org-WTrO org-ZC"&gt;
      &lt;name&gt;Sweden&lt;/name&gt;
      &lt;population&gt;8900954&lt;/population&gt;
      &lt;population_growth&gt;0.56&lt;/population_growth&gt;
      &lt;infant_mortality&gt;4.5&lt;/infant_mortality&gt;
      &lt;gdp_total&gt;177300&lt;/gdp_total&gt;
      &lt;gdp_agri&gt;2&lt;/gdp_agri&gt;
      &lt;gdp_ind&gt;27&lt;/gdp_ind&gt;
      &lt;gdp_serv&gt;71&lt;/gdp_serv&gt;
      &lt;inflation&gt;2.6&lt;/inflation&gt;
      &lt;government&gt;constitutional monarchy&lt;/government&gt;
      &lt;encompassed continent="europe" percentage="100"/&gt;
      &lt;ethnicgroup percentage="83"&gt;Swede&lt;/ethnicgroup&gt;
      &lt;ethnicgroup percentage="5"&gt;Finn&lt;/ethnicgroup&gt;
      &lt;religion percentage="87"&gt;Protestant&lt;/religion&gt;
      &lt;religion percentage="1.5"&gt;Roman Catholic&lt;/religion&gt;
      &lt;language percentage="96"&gt;Swedish&lt;/language&gt;
      &lt;language percentage="2.6"&gt;Finnish&lt;/language&gt;
      &lt;border country="SF" length="586"/&gt;
      &lt;border country="N" length="1619"/&gt;
      &lt;name&gt;Sweden&lt;/name&gt;
      &lt;population&gt;8900954&lt;/population&gt;
      &lt;population_growth&gt;0.56&lt;/population_growth&gt;
      &lt;infant_mortality&gt;4.5&lt;/infant_mortality&gt;
      &lt;gdp_total&gt;177300&lt;/gdp_total&gt;
      &lt;gdp_agri&gt;2&lt;/gdp_agri&gt;
      &lt;gdp_ind&gt;27&lt;/gdp_ind&gt;
      &lt;gdp_serv&gt;71&lt;/gdp_serv&gt;
      &lt;inflation&gt;2.6&lt;/inflation&gt;
      &lt;government&gt;constitutional monarchy&lt;/government&gt;
      &lt;encompassed continent="europe" percentage="100"/&gt;
      &lt;ethnicgroup percentage="83"&gt;Swede&lt;/ethnicgroup&gt;
      &lt;ethnicgroup percentage="5"&gt;Finn&lt;/ethnicgroup&gt;
      &lt;religion percentage="87"&gt;Protestant&lt;/religion&gt;
      &lt;religion percentage="1.5"&gt;Roman Catholic&lt;/religion&gt;
      &lt;language percentage="96"&gt;Swedish&lt;/language&gt;
      &lt;language percentage="2.6"&gt;Finnish&lt;/language&gt;
      &lt;border country="SF" length="586"/&gt;
      &lt;border country="N" length="1619"/&gt;
      &lt;province id="prov-Sweden-5" capital="cty-Sweden-3" country="S"&gt;
        &lt;name&gt;Vastra Gotaland&lt;/name&gt;
        &lt;area&gt;24473&lt;/area&gt;
        &lt;population&gt;1464971&lt;/population&gt;
        &lt;city id="cty-Sweden-3" is_state_cap="yes" country="S" 
              province="prov-Sweden-5"&gt;
          &lt;name&gt;Goteborg&lt;/name&gt;
          &lt;longitude&gt;12&lt;/longitude&gt;
          &lt;latitude&gt;57.4&lt;/latitude&gt;
          &lt;population year="95"&gt;449189&lt;/population&gt;
          &lt;located_at watertype="sea" sea="sea-Kattegat"/&gt;
          &lt;located_at watertype="river" river="river-Goetaaelv"/&gt;
        &lt;/city&gt;
        &lt;city id="cty-Sweden-Vanersborg" country="S" 
              province="prov-Sweden-5"&gt;
          &lt;name&gt;Vanersborg&lt;/name&gt;
          &lt;longitude&gt;12.2&lt;/longitude&gt;
          &lt;latitude&gt;58.2&lt;/latitude&gt;
          &lt;population year="87"&gt;35804&lt;/population&gt;
          &lt;located_at watertype="river" river="river-Goetaaelv"/&gt;
          &lt;located_at watertype="lake" lake="lake-Vaenersee"/&gt;
        &lt;/city&gt;
        &lt;city id="cty-Sweden-Mariestad" country="S" 
              province="prov-Sweden-5"&gt;
          &lt;name&gt;Mariestad&lt;/name&gt;
          &lt;longitude&gt;13.5&lt;/longitude&gt;
          &lt;latitude&gt;58.4&lt;/latitude&gt;
          &lt;population year="87"&gt;24255&lt;/population&gt;
          &lt;located_at watertype="lake" lake="lake-Vaenersee"/&gt;
        &lt;/city&gt;
      &lt;/province&gt;
      &lt;province id="prov-Sweden-3" capital="cty-Sweden-Karlskrona" country="S"&gt;
        &lt;name&gt;Blekinge&lt;/name&gt;
        &lt;area&gt;2941&lt;/area&gt;
        &lt;population&gt;151168&lt;/population&gt;
        &lt;city id="cty-Sweden-Karlskrona" is_state_cap="yes" country="S" 
              province="prov-Sweden-3"&gt;
          &lt;name&gt;Karlskrona&lt;/name&gt;
          &lt;longitude&gt;15.3&lt;/longitude&gt;
          &lt;latitude&gt;56.1&lt;/latitude&gt;
          &lt;population year="87"&gt;59007&lt;/population&gt;
          &lt;located_at watertype="sea" sea="sea-Ostsee"/&gt;
        &lt;/city&gt;
      &lt;/province&gt;
    &lt;/country&gt;
  &lt;/mondial&gt;
  </code></pre>
  
  <p>The thing is that I want to write a query for the following question:
  Show the names, populations and capitals of the provinces (län) of Sweden order by population with largest first.</p>
  
  <p>So what I have done so far is that I wrote the following in XQuery:</p>
  
  <pre><code>let $mondial := db:open('mondial')
  for $x in $mondial/mondial/country[name="Sweden"]
  let $pop := $x/province/population
  return $pop/string()
  </code></pre>
  
  <p>that gives me all the provinces of Sweden´s population which work fine, then I added the following to the query:</p>
  
  <pre><code>let $mondial := db:open('mondial')
  for $x in $mondial/mondial/country[name="Sweden"]
  let $pop := $x/province/population
  let $cname := $x/province/name
  let $cap := $x/province/city[@is_state_cap="yes"]/name
  return ($cname/string(), $pop/string(), $cap/string())
  </code></pre>
  
  <p>which also works fine and gives me the provinces name, population and capital name but when I then try to add the order by population this way:</p>
  
  <pre><code>let $mondial := db:open('mondial')
  for $x in $mondial/mondial/country[name="Sweden"]
  let $pop := $x/province/population
  let $cname := $x/province/name
  let $cap := $x/province/city[@is_state_cap="yes"]/name
  order by $pop 
  return ($cname/string(), $pop/string(), $cap/string())
  </code></pre>
  
  <p>BaseX give me the error </p>
  
  <blockquote>
    <p>Item expected, sequence found: (element population {....}, ...)</p>
  </blockquote>
  
  <p>and I can't seem to figure out why.
  I also tried adding <code>/string()</code> after the order by, and tried adding descending but with no luck so far.</p>