本文介绍了错误"noteLine上的未定义变量";返回多个节点时,在FLWOR表达式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对FLWOR循环有一个非常奇怪的问题,该问题以一种方式起作用,但没有另一种方式.目标是采用任意长度的字符串,并将其分解为每个只能容纳80个字符的XML节点.因此,第一次重击,效果很好:

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()

所以,我觉得这不是很优雅.我尝试通过将第一个元素移出if来清理它,因为它应该总是被使用,对吗?这样更少的代码?所以这是我尝试过的:

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()

解析器现在告诉我"noteLine的未定义变量"指向第一行"if"行.我在这里想念什么? (请注意,是的,我的确有更多方法来解决此问题,但这是第一步,而当它不幸失败时,我感到非常恐慌).

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).

解决方案(感谢Jens Erat)返回块需要用括号括起来,以强制其在flwor循环的每次迭代中进行完全评估.因此:

Solution (thanks to Jens Erat)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:

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()
    )

推荐答案

这是一个较小的语法问题.您必须在return子句中创建的元素周围加上括号.为了进一步解释该问题,我提供了一个类似但简化的示例:

This is a minor syntax issue. You have to put parenthesis around the elements created in the return clause. To further explain the issue, I'm providing a similar, but simplified example:

for $i in 1 to 3
return
    <foo />,
    <bar />

您希望获得以下结果(两个元素重复了三遍):

You'd expect to get following result (the two elements repeated three times):

<foo/>
<bar/>
<foo/>
<bar/>
<foo/>
<bar/>

但是,相反,您得到

<foo/>
<foo/>
<foo/>
<bar/>

这是因为查询实际上被评估为

This is because the query is actually evaluated as

(
  for $i in 1 to 3
  return
      <foo />
),
<bar />

在您的代码中,不会得到意外的输出,而是一条错误消息.这是因为在第一个逗号,之后未定义$noteLine.对于以下查询,您将收到类似的错误消息:

In your code, you don't get unexpected output, but an error message. This is because $noteLine is not defined after the first comma ,. You would get a similar error message for following query:

for $i in 1 to 3
return
    <foo>{ $i }</foo>,
    <bar>{ $i }</foo>

在这里,$i没有绑定到<bar/>元素,因为查询被评估为

Here, $i is not bound for the <bar/> element, as the query is evaluated as

(
  for $i in 1 to 3
  return
      <foo>{ $i }</foo>
),
<bar>{ $i }</foo> (: obivously, $i is not defined here :)

这篇关于错误"noteLine上的未定义变量";返回多个节点时,在FLWOR表达式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:42