Title: Xquery nested map:merge from xml produces error "expected single value for key, got 0"
Tags: xpath;hashmap;xquery;exist-db
Question: In Xquery 3.1 I am trying to transform an XML document into a nested map. My xml document ```$keyworddoc``` has this structure :
```    <category xml:id=&quot;KW0003&quot;&gt;
        <desc xml:lang=&quot;fr&quot;&gt;évêque</desc&gt;
        <desc xml:lang=&quot;en&quot;&gt;bishop</desc&gt;
        <desc xml:lang=&quot;de&quot;&gt;Bischof</desc&gt;
        <desc xml:lang=&quot;es&quot;&gt;obispo</desc&gt;
        <desc xml:lang=&quot;it&quot;&gt;vescovo</desc&gt;
    </category&gt;
    <category xml:id=&quot;KW0004&quot;&gt;
        <desc xml:lang=&quot;fr&quot;&gt;sacrement</desc&gt;
        <desc xml:lang=&quot;en&quot;&gt;sacrament</desc&gt;
        <desc xml:lang=&quot;de&quot;&gt;Sakrament</desc&gt;
        <desc xml:lang=&quot;es&quot;&gt;sacramento</desc&gt;
        <desc xml:lang=&quot;it&quot;&gt;sacramento</desc&gt;
    </category&gt;
    <category xml:id=&quot;KW0005&quot;&gt;
        <desc xml:lang=&quot;fr&quot;&gt;messe</desc&gt;
        <desc xml:lang=&quot;en&quot;&gt;mass</desc&gt;
        <desc xml:lang=&quot;de&quot;&gt;Messe</desc&gt;
        <desc xml:lang=&quot;es&quot;&gt;misa</desc&gt;
        <desc xml:lang=&quot;it&quot;&gt;messa</desc&gt;
    </category&gt;
```
with the desired map output:
```map {
    &quot;KW0003&quot;: map {
                  &quot;fr&quot;: &quot;évêque&quot;,
                  &quot;en&quot;: &quot;bishop&quot;,
                  &quot;de&quot;: &quot;Bischof&quot;,
                  &quot;es&quot;: &quot;obispo&quot;,
                  &quot;it&quot;: &quot;vescovo&quot;},
    &quot;KW0004&quot;: map {
                  &quot;fr&quot;: &quot;sacrement&quot;,
                  &quot;en&quot;: &quot;sacrament&quot;,
                  &quot;de&quot;: &quot;Sakrament&quot;,
                  &quot;es&quot;: &quot;sacramento&quot;,
                  &quot;it&quot;: &quot;sacramento&quot;},
    &quot;KW0005&quot;: map {
                  &quot;fr&quot;: &quot;messe&quot;,
                  &quot;en&quot;: &quot;mass&quot;,
                  &quot;de&quot;: &quot;Messe&quot;,
                  &quot;es&quot;: &quot;misa&quot;,
                  &quot;it&quot;: &quot;messa&quot;},
    }
```
However, my function:
```   let $kwdoc :=  $keyworddoc//tei:category
   return map:merge(for $kw in $kwdoc 
                    return map{$kw/data(@xml:id) : 
                               map:merge(for $desc in $kw 
                                         return map{$desc/data(@xml:lang) : 
                                                    $desc/text()}
                                         )})
```
produces the following error which suggests that the nested for loop is not &quot;seeing&quot; the variable ```$kw```?:
```Expected single value for key, got 0```
Perhaps I am going about constructing my first nested map in the wrong way.
edit: Xquery within eXist 5x.
Many thanks in advance.


Here is the accepted answer: Since ```$kw``` is bound to a single ```tei:category``` element at a time, the clause ```for $desc in $kw``` iterates over a single-element sequence and just binds the same element to ```$desc```, so it is equivalent to ```let $desc := $kw``` in this case.
What you want is to iterate over the ```tei:desc``` children of ```$kw``` instead:
```let $kwdoc := $keyworddoc//tei:category
return map:merge(
  for $kw in $kwdoc 
  return map{
    $kw/data(@xml:id): map:merge(
      for $desc in $kw/tei:desc
      return map{ $desc/data(@xml:lang): $desc/text() }
    )
  }
)
```

Comment for this answer: Works fine for me on eXist-db 5.3.0-SNAPSHOT
Comment for this answer: Which XQuery processor are you using? It works fine for me in BaseX.
Comment for this answer: Bah, that was sloppy of me. But even so, when I copy-paste your changed code I still get the same error. I stripped out all the `map` and `map:merge` functions to assure the data is coming through and it is. It makes me think something else is not right with nesting these functions.
Comment for this answer: I'm using eXist 5x.
Comment for this answer: In fact, it works fine, it was a problem when I applied it to the production file which it turns out had certain missing values. It seems I wasn't handling that correctly.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:09/25/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xpath><hashmap><xquery><exist-db>', 'Title': 'Xquery nested map:merge from xml produces error "expected single value for key, got 0"', 'ViewCount': 129, 'id': 64065152, 'user_AboutMe': '<p>Medievalist and programmer.</p>\n', 'user_AccountId': 11786003.0, 'user_DisplayName': 'jbrehr', 'user_Reputation': 745.0, 'user_Views': 226.0, 'user_id': 8623722.0}}