I am trying to clean up some XQuery code and ran into another problem (last question was this one: Error "undefined variable at noteLine" in FLWOR expression when returning multiple nodes). I am further cleaning up the following working code, to minimize reuse:
for $noteLine in $noteLineArr
where $noteLine != ''
return (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 1, 80)}</Descr>
</NTE>,
if (fn:string-length(fn:normalize-space($noteLine)) > 80 and fn:string-length(fn:normalize-space($noteLine)) <= 160) then (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr>
</NTE>
) else if (fn:string-length(fn:normalize-space($noteLine)) > 160) then (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr>
</NTE>,
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 161, 80)}</Descr>
</NTE>
) else ()
)
Now that I moved the first NTE out, I want to basically do the same with the rest (run an IF and only output another using exactly the code that's needed) so I pictured a solution like this:
for $noteLine in $noteLineArr
where $noteLine != ''
return (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 1, 80)}</Descr>
</NTE>,
if (fn:string-length(fn:normalize-space($noteLine)) > 80) then (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr>
</NTE>
) else()
if (fn:string-length(fn:normalize-space($noteLine)) > 160) then (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 161, 80)}</Descr>
</NTE>
) else ()
)
This fails in the parser with the error "Unexpected Token: ' (fn:string-len...'" pointing at the second if statement. I have concluded that XQuery does not like recurring if sections unless they are within a node, like this:
for $noteLine in $noteLineArr
where $noteLine != ''
return (
<NTE>
<NoteRefCd>BOL</NoteRefCd>
<Descr>{fn:substring(fn:normalize-space($noteLine), 1, 80)}</Descr>
</NTE>,
<NTE>
if (fn:string-length(fn:normalize-space($noteLine)) > 80) then (
<NoteRefCd>BOL</NoteRefCd>,
<Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr>
) else()
</NTE>,
<NTE>
if (fn:string-length(fn:normalize-space($noteLine)) > 160) then (
<NoteRefCd>BOL</NoteRefCd>,
<Descr>{fn:substring(fn:normalize-space($noteLine), 161, 80)}</Descr>
) else ()
</NTE>
)
This is not workable, because I can't let any of those extra nodes get in the output if they are empty. Is there a way in XQuery to achieve the desired output a similar way (I worked out a solution by putting some of the code into a local function but it is not as readable imo)? And possibly as a follow up, why is XQuery so sensitive to multiple consecutive if statements?
Aucun commentaire:
Enregistrer un commentaire