Marklogic 为什么$book给出了一个错误,而$title和$author不';T

Marklogic 为什么$book给出了一个错误,而$title和$author不';T,marklogic,Marklogic,我理解$book、$title和$author在FLWOR中的作用域,但我不理解为什么$title和$author按顺序工作,而$book不按顺序工作 (: A note on variable scope. Variables in a FLWOR expression are scoped only to the FLWOR expression. Local variables declared in the prolog are scoped to the main mod

我理解$book、$title和$author在FLWOR中的作用域,但我不理解为什么$title和$author按顺序工作,而$book不按顺序工作

(: 
  A note on variable scope.
  Variables in a FLWOR expression are scoped only to the FLWOR expression.
  Local variables declared in the prolog are scoped to the main module.
:)

(: start of prolog :)
xquery version "1.0-ml";

declare namespace bks = "http://www.marklogic.com/bookstore";

declare variable $scope-example := "2005";
(: end of prolog :)

(: start of query body :)
(
"remember an XQuery module returns a sequence -- this text is the first item, and then the results of the FLWOR",

for $book in /bks:bookstore/bks:book
let $title := $book/bks:title/string()
let $author := $book/bks:author/string()
let $year := $book/bks:year/string()
let $price := xs:double($book/bks:price/string())
where $year = $scope-example (: we can do this because my local variable is scoped to the module :)
order by $price descending
return
  <summary>{($title, "by", $author)}</summary>
,
"and now another item, but I cant reference a variable from the FLWOR expression outside of the FLWOR, it will fail like this",
$book
)
(: end of query body :)
(:
关于变量作用域的注记。
FLWOR表达式中的变量的作用域仅限于FLWOR表达式。
prolog中声明的局部变量的作用域是主模块。
:)
(:序言的开始:)
xquery版本“1.0-ml”;
声明命名空间bks=”http://www.marklogic.com/bookstore";
声明变量$scope示例:=“2005”;
(:序言的结尾:)
(:查询主体的开始:)
(
“记住,XQuery模块返回一个序列——该文本是第一项,然后是FLWOR的结果”,
$book in/bks:bookstore/bks:book
让$title:=$book/bks:title/string()
让$author:=$book/bks:author/string()
让$year:=$book/bks:year/string()
让$price:=xs:double($book/bks:price/string())
其中$year=$scope示例(:我们可以这样做,因为我的局部变量的作用域是模块:)
按美元降价订购
返回
{($title,“by”,$author)}
,
“现在是另一项,但我不能引用FLWOR表达式中FLWOR之外的变量,它将像这样失败”,
$book
)
(:查询正文的结尾:)

FLWOR的FOR中绑定的变量仅在FLWOR中可见

XQuery不是一种过程语言;它是功能性的。对于一个执行系统来说,并行或无序运行表达式是非常好的。这是函数式语言的一个很酷的特性

想象一个数学函数
(a*b)+(c*d)
。评估系统可能会并行地执行
a*b
c*d
部分,而用户无法分辨。在XQuery中也是这样。很多工作可以并行进行,你不必管理它,你甚至不知道

在您的示例中,您在语句中提供了3个表达式,每个表达式都是独立的。您不应该认为您的程序完全是自上而下运行的,在运行过程中会留下副作用变量的变化

突击测验:这有什么回报

for $i in (1 to 3)
return $i, 4
它是
1234
,因为它是一个返回
1234
的FLWOR表达式,后跟一个返回
4
的表达式。在第二个表达式中不能引用
$i

for $i in (1 to 3)
return $i
,
4

简言之,您需要使用额外的括号使其工作。例如:
用于。。。返回({($title,“by”,$author)},$book)