Question

Given:

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

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

I expect:

<result>baz</result>

But get:

</result>

Why?

Was it helpful?

Solution 2

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 ().

OTHER TIPS

All appearances to the contrary notwithstanding, XQuery is not a dialect of Lisp. (Yes, I know; some people in the world would be happier if it were.)

XQuery sequences do not nest, and when given expressions which seem to suggest nesting (like those in this query) XQuery evaluators flatten the sequences. So in your example the value of $seq is ('foo', 'bar', 'baz').

The expression $seq[(.)[1][1] = 'foo'] can be simplified to $seq[. = 'foo'] and evaluates to the sequence 'foo'.

The immediately following predicates [2] and [1] ask first for the second item in this sequence (which is guaranteed to produce a sequence of length zero or one) and then for the first item in the resulting sequence (which is here guaranteed to have no effect at all). Since in fact the sequence 'foo' is a singleton, there is no second member.

The value of ($seq[(.)[1][1] = 'foo'])[2][1] is thus the empty sequence, and the value of the query as a whole is an empty result element.

The simplest way to achieve nesting structures in XQuery is with XML; XML is moderately good at nesting structures, and XQuery is very good with XML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top