On 12/19/2022 10:43 PM, Leo Studer wrote:
Hello Cracks ;-)

In the code below I encounter the problem, that I have to define the same xPath function variable $organizations twice and I do not understand why.
Running the code with the newest versions of BaseX or with Saxon EE, I get a runtime error when I leave the second definition of $organizations out.

Any explanation? Thanks in advance.

Happy Holidays
Always, Leo



xquery version "3.1" encoding "UTF-8";
declare variable $doc := doc("factbook.xml");

element Memberships {
let $membershipPotentcy := function($orgs){if (count($orgs)le 0) then 'none'
                                          
else if (count($orgs) le 5) then 'few'
                                          
else 'many'}
let $organizations := function($country){$doc//organization[members/@country = $country/@id]/@abbrev/string(.)}
for $countries in $doc//country
 
group by $mp:= $membershipPotentcy($organizations($countries))
 
order by $mp
return
 
element {$mp} {
 
for $country in $countries
 
let $organizations := function($country){$doc//organization[members/@country = $country/@id]/@abbrev/string(.)}
  
order by $country/@name
 
return
 
<country>{$country/@name, $organizations($country)}</country>
}}


Which runtime error do you get? I guess the group by binds your let variable for each country in the group so perhaps

  head($organizations)($country)

would suffice without redefining the variable.

It would be an odd way to solve it, I would prefer to have a


declare function local:organizations($country)


declared before.