Search code examples
xquerysequences

XQuery Sequences


Given:

declare variable $seq :=  (
  (('foo', 'bar'), ('baz'))  
);

<result>{ ($seq[(.)[1][1] = 'foo'])[2][1] }</result>

I expect:

<result>baz</result>

But get:

</result>

Why?


Solution

  • In XQuery arrays don't have dimension, so (('foo', 'bar'), ('baz')) is no different from ('foo', 'bar', 'baz').

    ($seq[(.)[1][1] = 'foo']) is the same as $seq[. = 'foo'] => text{'foo'}. This is treated as a sequence of length 1. So (text{'foo'})[1] => text{'foo'}, but (text{'foo'})[2] obviously is ().