title: Error "undefined variable at noteLine" in FLWOR expression when returning multiple nodes
body: <p>I have a very strange problem with a FLWOR loop, that works one way but not another.  The goal is to take a string of any length, and break it into XML nodes that can only hold 80 chars each.  So, first whack, this works great:</p>
  
  <pre><code>    for $noteLine in $noteLineArr
      where $noteLine != ''
  
      return
      if (fn:string-length(fn:normalize-space($noteLine)) &lt; 80) then (
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 1, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;  
      ) else if (fn:string-length(fn:normalize-space($noteLine)) &gt; 80 and fn:string-length(fn:normalize-space($noteLine)) &lt;= 160) then (
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 1, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;,
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 81, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;
      ) else if (fn:string-length(fn:normalize-space($noteLine)) &gt; 160 and fn:string-length(fn:normalize-space($noteLine)) &lt;= 240) then (
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 1, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;,
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 81, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;,
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 161, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;
      ) else()            
  </code></pre>
  
  <p>So, I get it in my head that this isn't very elegant.  I try to clean it up by moving the first element out of the if, since it should always get used, right?  Less code that way?  So here is what I tried:</p>
  
  <pre><code>for $noteLine in $noteLineArr
  where $noteLine != ''
  
  return
  
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 1, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;, 
      if (fn:string-length(fn:normalize-space($noteLine)) &gt; 80 and fn:string-length(fn:normalize-space($noteLine)) &lt;= 160) then (
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 81, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;
      ) else if (fn:string-length(fn:normalize-space($noteLine)) &gt; 160 and fn:string-length(fn:normalize-space($noteLine)) &lt;= 240) then (
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 81, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;,
      &lt;NTE&gt;
          &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
          &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 161, 80)}&lt;/Descr&gt;
      &lt;/NTE&gt;
      ) else()    
  </code></pre>
  
  <p>And the parser is now telling me "undefined variable at noteLine" pointing to the first "if" line. What am I missing here? (note that yes, I do have other ideas of how to clean this up even more, but this was the first simple step and when it failed miserably, I panicked). </p>
  
  <p><strong>Solution (thanks to Jens Erat)</strong>
  The return block needs to be wrapped in parentheses in order to force it to evaluate fully on each iteration of the flwor loop.  Thusly:</p>
  
  <pre><code>for $noteLine in $noteLineArr
  where $noteLine != ''
  
  return
      (
          &lt;NTE&gt;
              &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
              &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 1, 80)}&lt;/Descr&gt;
          &lt;/NTE&gt;, 
          if (fn:string-length(fn:normalize-space($noteLine)) &gt; 80 and fn:string-length(fn:normalize-space($noteLine)) &lt;= 160) then (
          &lt;NTE&gt;
              &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
              &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 81, 80)}&lt;/Descr&gt;
          &lt;/NTE&gt;
          ) else if (fn:string-length(fn:normalize-space($noteLine)) &gt; 160 and fn:string-length(fn:normalize-space($noteLine)) &lt;= 240) then (
          &lt;NTE&gt;
              &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
              &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 81, 80)}&lt;/Descr&gt;
          &lt;/NTE&gt;,
          &lt;NTE&gt;
              &lt;NoteRefCd&gt;WHI&lt;/NoteRefCd&gt;
              &lt;Descr&gt;{fn:substring(fn:normalize-space($noteLine), 161, 80)}&lt;/Descr&gt;
          &lt;/NTE&gt;
          ) else()
      )
  </code></pre>