XQuery中变量的作用域?

XQuery中变量的作用域?,xquery,zorba,Xquery,Zorba,我有note.xml: <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 出于某些原因,我需要两个,以便处理note.x

我有
note.xml

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
出于某些原因,我需要两个
,以便处理
note.xml
。我不想把处理过的文件名写两次,所以我定义了一个变量。奇怪的是,变量没有在
的第二个
中定义:

$ zorba -i -f -q note.xqy
note.xqy>:5,15: static error [err:XPST0008]: "srcDoc": undeclared variable

XQuery中的
let
绑定变量仅在其定义的FLWOR表达式的范围内。在你的情况下,这就是逗号之前的所有内容

由于FLWOR表达式可以嵌套,一种解决方案是将
let
拆分为一个封闭表达式,并将两个循环放入
return

let $srcDoc:="note.xml"
return (
    for $x in doc($srcDoc)/note
    return (),

    for $x in doc($srcDoc)/note
    return ()
)
由于您在XQuery脚本的开头绑定了一个常量,因此还可以使用
声明变量
,它仅在这种情况下有效:

declare variable $srcDoc := "note.xml";

for $x in doc($srcDoc)/note
return (),

for $x in doc($srcDoc)/note
return ()
declare variable $srcDoc := "note.xml";

for $x in doc($srcDoc)/note
return (),

for $x in doc($srcDoc)/note
return ()