Here is a short version of my question.
When using a nested for-loop in a FLOWR expression returning the variable from the inner loop behaves as expected but returning the variable from the outer loop unexpectedly returns only closing tags.
Here is a longer version of my question.
I have recently started learning XQuery. I've been using the XQilla command-line tool on Mac OS X 10.10.4 (Yosemite) to execute my XQuery scripts. In particular I've tried XQilla Version 2.3.0_2 which I installed with the MacPorts package manager and also XQilla version 2.3.2 which I manually installed from source.
While experimenting with various XPath and FLWOR expressions I came across an unexpected behavior which I've narrowed down to the simplified example which I am going to present here. The example consists of a single XML data file and two versions of a simple XQuery; the first query works as expected but the second query does not.
Here is my XML data file (test.xml
):
<elements> <element> element data </element> </elements>
Here is my first XQuery script (test_1.xq
):
for $e1 in doc("test.xml")//element for $e2 in doc("test.xml")//element return $e1
And here is my second XQuery script (test_2.xq
):
for $e1 in doc("test.xml")//element for $e2 in doc("test.xml")//element return $e2
Both queries consist of the same redundant, nested for-loops, but the first query returns the variable from the outer (i.e. first) for-loop and the second query returns the variable from the inner (i.e. second) for-loop. I expected the two queries to have similar output in general, and identical output for this specific data file, however that appears not to be the case.
Here is a sample shell session:
$ xqilla test_1.xq <element> element data </element> $ xqilla test_2.xq <element/>
So returning the variable from the outer loop only produces the closing tag; this was unexpected.
As a sanity-check I tried a third variation (test_3.xq
):
for $e1 in doc("test.xml")//element for $e2 in doc("test.xml")//element where $e1=$e2 return $e2
And when I execute this third query I get:
$ xqilla test_3.xq <element> element data </element>
I would have originally expected this query to work correctly, but in light of the unexpected behavior of test_2.xq
I would think that test_3.xq
would also fail. This only adds to my confusion.