On 06/10/2023 09:16, Alexander Krasnogolowy wrote:
Given the following XQuery: let $input := <root> <item><key>a</key><value>1</value></item> <item><key>b</key><value>2</value></item> <item><key>b</key><value>3</value></item> </root>
let $map := map:merge(( map:entry(1, "A"), map:entry(2, "B"), map:entry(3, "C") ))
for $result in $input/* let $key := $result/key/text() let $value := $result/value/text() group by $key return map:get($map, $key)
When trying to execute that query, I get the error "Item expected, sequence found: (map {1:"A",2:"B",3:"C"}, map {1:"A",2:"B",3:"C"}). But why does it group the map? It isn't part of that for-loop but rather some kind of global variable. Or do I misunderstand the "group by" clause? I only expect the $value to be grouped which results in a sequence.
Use
let $input := <root> <item><key>a</key><value>1</value></item> <item><key>b</key><value>2</value></item> <item><key>b</key><value>3</value></item> </root>,
$map := map:merge(( map:entry(1, "A"), map:entry(2, "B"), map:entry(3, "C") )) return for $result in $input/*
to ensure the `group by` clause doesn't change the variable bindings for $input and $map. I am not sure the rest of the code makes sense but you might know better what you want to achieve.