On Fri, May 27, 2022 at 10:50:57AM +0200, Markus Elfring scripsit:
This code may for example be (most often is) a FLWOR expression. In this code, each entry is constructed by a call of function map:entry().
Now I stumble on the error message “[XPTY0004] Item expected, empty sequence found.” from my software test according to a special data source.
So either the key or the value isn't in the source data.
How would you determine where unexpected gaps occur?
let $simple as element(bucket) := <bucket> <thingy name="oleander"/> <thingy name="primrose">likes well-drained partial shade</thingy> <thingy/> <thingy>zone 7</thingy> <thingy name="tulip">marsh plant, not a swamp plant</thingy> </bucket>
let $thingMap as map(xs:string,xs:string) := map:merge( for $thing in $simple/descendant::thingy let $key as xs:string? := $thing/@name/string() let $value as xs:string? := $thing/string() where normalize-space($key) and normalize-space($value) return map:entry($key,$value) )
return $thingMap
The map entry can be constructed only when it has meaning. Which means the code has to allow for finding neither the key nor the value, which is why the ? (0 or 1) quantifier on the xs:string types.
(The map-constructing FLOWR expression should also consider that there might be duplicate keys, but one thing at a time.)
If you really want to know where in the data something is missing
let $simple as element(bucket) := <bucket> <thingy name="oleander"/> <thingy name="primrose">likes well-drained partial shade</thingy> <thingy/> <thingy>zone 7</thingy> <thingy name="tulip">marsh plant, not a swamp plant</thingy> </bucket>
let $thingProblems as xs:string* := for $thing in $simple/descendant::thingy let $key as xs:string? := $thing/@name/string() let $value as xs:string? := $thing/string() where not(normalize-space($key)) or not(normalize-space($value)) return $thing ! concat(base-uri(.),path(.))
return $thingProblems
will tell you which nodes in the input can't create map entries.
This approach can be made more robust but this is the general idea.