title: XQuery FLWOR Expression with Multiple for-loops returns unexpected empty tags
body: <p>Here is a short version of my question.</p>
  
  <p>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 empty tags.</p>
  
  <hr>
  
  <p>Here is a longer version of my question.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is my XML data file (<code>test.xml</code>):</p>
  
  <pre><code>&lt;elements&gt;
  
      &lt;element&gt;
          element data
      &lt;/element&gt;
  
  &lt;/elements&gt;
  </code></pre>
  
  <p>Here is my first XQuery script (<code>test_1.xq</code>):</p>
  
  <pre><code>for $e1 in doc("test.xml")//element
      for $e2 in doc("test.xml")//element
          return $e1
  </code></pre>
  
  <p>And here is my second XQuery script (<code>test_2.xq</code>):</p>
  
  <pre><code>for $e1 in doc("test.xml")//element
      for $e2 in doc("test.xml")//element
          return $e2
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is a sample shell session:</p>
  
  <pre><code>$ xqilla test_1.xq
  &lt;element&gt;
          element data
      &lt;/element&gt;
  
  $ xqilla test_2.xq
  &lt;element/&gt;
  </code></pre>
  
  <p>So returning the variable from the outer loop only produces the empty tag; this was unexpected.</p>
  
  <p>As a sanity-check I tried a third variation (<code>test_3.xq</code>):</p>
  
  <pre><code>for $e1 in doc("test.xml")//element
      for $e2 in doc("test.xml")//element
          where $e1=$e2
          return $e2
  </code></pre>
  
  <p>And when I execute this third query I get:</p>
  
  <pre><code>$ xqilla test_3.xq
  &lt;element&gt;
          element data
      &lt;/element&gt;
  </code></pre>
  
  <p>I would have originally expected this query to work correctly, but in light of the unexpected behavior of <code>test_2.xq</code> I would think that <code>test_3.xq</code> would also fail. This only adds to my confusion.</p>