Project

Profile

Help

variable scope in XQuery

Added by Anonymous almost 15 years ago

Legacy ID: #7460100 Legacy Poster: newbee (nakoned)

Hi, I am having an issue of the scope of internal variables in XQuery: when I define variable in one scope and then dynamically modify it in for loop, it does not seem to be reset, but rather with each loop iteration assumes initial assigned value. Here is the small example: let $lines := ('date_in(&apos;20090623&apos;, &apos;bla&apos;)', 'prefix_in()', 'date_in(&apos;20090624&apos;, &apos;bla&apos;)', 'prefix_in()') let $date := '' for $line at $i in $lines return let $date := if(contains($line, 'date_in')) then $line else $date return if(contains($line, 'prefix_in')) then <date>{$date}</date> else '' This code will output: <date/> <date/> though my expectation would be <date>date_in('20090623', 'bla')</date>, etc. Is there any way I can declare $date as global and re-assign it in the loop? Thanks, Ed


Replies (1)

RE: variable scope in XQuery - Added by Anonymous almost 15 years ago

Legacy ID: #7460355 Legacy Poster: Michael Kay (mhkay)

This is a pure XQuery question, nothing specific to the Saxon product. I'd recommend asking XQuery coding questions on the list . You've not grasped the fact that XQuery is a purely functional language, and therefore variables are not assignable. Your two "let $date" instructions in fact declare completely different variables. Also, the "for" expression is a mapping expression rather than a loop, you must think of it as computing the value of the return clause for each item in the input sequence independently, as if it were done in parallel. It's difficult to reverse-engineer your requirements from incorrect code, so I suggest you start again with a clear English description of what you are trying to achieve, showing the input and output and explaining how they are related.

    (1-1/1)

    Please register to reply