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:
for $noteLine in $noteLineArr where $noteLine != '' return if (fn:string-length(fn:normalize-space($noteLine)) < 80) then ( <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 1, 80)}</Descr> </NTE> ) else if (fn:string-length(fn:normalize-space($noteLine)) > 80 and fn:string-length(fn:normalize-space($noteLine)) <= 160) then ( <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 1, 80)}</Descr> </NTE>, <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr> </NTE> ) else if (fn:string-length(fn:normalize-space($noteLine)) > 160 and fn:string-length(fn:normalize-space($noteLine)) <= 240) then ( <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 1, 80)}</Descr> </NTE>, <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr> </NTE>, <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 161, 80)}</Descr> </NTE> ) else() 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:
for $noteLine in $noteLineArr where $noteLine != '' return <NTE> <NoteRefCd>WHI</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>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr> </NTE> ) else if (fn:string-length(fn:normalize-space($noteLine)) > 160 and fn:string-length(fn:normalize-space($noteLine)) <= 240) then ( <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 81, 80)}</Descr> </NTE>, <NTE> <NoteRefCd>WHI</NoteRefCd> <Descr>{fn:substring(fn:normalize-space($noteLine), 161, 80)}</Descr> </NTE> ) else() 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).
No comments:
Post a Comment