Simple code there-
let $sV2 :=''
for $i in (1 to 2)
let $sV1 := 'test1'
let $sV2 := if (fn:string-length($sV2) != 0) then fn:concat($sV2,'||',$sV1) else ($sV1)
return
<test>{$sV2}</test>
i get the same output
<test>test1</test>
<test>test1</test>
whereas i expect
<test>test1</test>
<test>test1||test1</test>
what am i doing wrong? i tried to debug but why does V2 initialize to null/'' for second iteration.
Note- I have to use xquery 1.0 do to some product limitations.
How do i get the expected output?
CodePudding user response:
XQuery is a functional language. As such, it does not allow you to change the value of a variable once it has been assigned.
There are many other ways to reach the same results, though. Your specific query can e.g. be rewritten as follows:
for $i in 1 to 2
return <test>{
string-join(for $n in 1 to $i return 'test1', '||')
}</test>