Trouble with wildcard lookup operator on maps?
Saturday, 15 August 2015 · 3 messages
MG
Marc van Grootel15 Aug 2015 15:47 #
Hi,
Unless I'm not reading the spec[1] correctly then the following two snippets should have the same result:
Example 1:
let $map := map { 'a': (1,2,3), 'b': (4,5,6)}
for $k in map:keys($map)
return array { $map($k) }Example 2:
let $map := map { 'a': (1,2,3), 'b': (4,5,6)}
for $k in $map?*
return array { $k }However, this is not the case. The first outputs, as I expected:
([1,2,3], [4,5,6])
The second, counter intuitively returns (on 8.2.3) this:
([1],[2],[3],[4],[5],[6])
The spec indicates that both examples result should be identical. Didn't check if the same is going on with arrays.
Hmmm?
[1] http://www.w3.org/TR/xquery-31/#id-lookup
CG
Christian Grün15 Aug 2015 15:59 #
Hi Marc,
The result is correct: The FOR clause of the FLWOR expression always binds one item at a time. If you want to bind sequences, you should use arrays in your map constructor:
let $map := map { 'a': [1,2,3], 'b': [4,5,6] }
return $map?*Hope this helps,
Christian
Quoted text · Marc van Grootel · 34 lines
On Sat, Aug 15, 2015 at 3:47 PM, Marc van Grootel <marc.van.grootel at gmail.com> wrote: > Hi, > > Unless I'm not reading the spec[1] correctly then the following two > snippets should have the same result: > > Example 1: > > > let $map := map { 'a': (1,2,3), 'b': (4,5,6)} > for $k in map:keys($map) > return array { $map($k) } > > Example 2: > > let $map := map { 'a': (1,2,3), 'b': (4,5,6)} > for $k in $map?* > return array { $k } > > However, this is not the case. The first outputs, as I expected: > > ([1,2,3], [4,5,6]) > > The second, counter intuitively returns (on 8.2.3) this: > > ([1],[2],[3],[4],[5],[6]) > > The spec indicates that both examples result should be identical. > Didn't check if the same is going on with arrays. > > Hmmm? > > [1] http://www.w3.org/TR/xquery-31/#id-lookup
MG
Marc van Grootel15 Aug 2015 16:16 #
Hi Christian,
Ok, that clears it up. Somehow I suspected this, but I was thrown by the statement in the spec (and my faulty intuition) that says:
If the KeySpecifier is a wildcard ("*") and the context item is a map, unary lookup is equivalent to the following expression:
for $k in map:keys(.)
return .($k)I realize that if I didn't put an array {} around the return value then this statement would be correct.
Just thought I caught a loophole there. I stand corrected. ;-)
Quoted text · Christian Grün · 50 lines
On Sat, Aug 15, 2015 at 3:59 PM, Christian Grün <christian.gruen at gmail.com> wrote: > Hi Marc, > > The result is correct: The FOR clause of the FLWOR expression always > binds one item at a time. If you want to bind sequences, you should > use arrays in your map constructor: > > let $map := map { 'a': [1,2,3], 'b': [4,5,6] } > return $map?* > > Hope this helps, > Christian > > > > On Sat, Aug 15, 2015 at 3:47 PM, Marc van Grootel > <marc.van.grootel at gmail.com> wrote: >> Hi, >> >> Unless I'm not reading the spec[1] correctly then the following two >> snippets should have the same result: >> >> Example 1: >> >> >> let $map := map { 'a': (1,2,3), 'b': (4,5,6)} >> for $k in map:keys($map) >> return array { $map($k) } >> >> Example 2: >> >> let $map := map { 'a': (1,2,3), 'b': (4,5,6)} >> for $k in $map?* >> return array { $k } >> >> However, this is not the case. The first outputs, as I expected: >> >> ([1,2,3], [4,5,6]) >> >> The second, counter intuitively returns (on 8.2.3) this: >> >> ([1],[2],[3],[4],[5],[6]) >> >> The spec indicates that both examples result should be identical. >> Didn't check if the same is going on with arrays. >> >> Hmmm? >> >> [1] http://www.w3.org/TR/xquery-31/#id-lookup
--
--Marc
Written for BaseX 8.2. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery