为什么 $book 报错,而 $title 和 $author 不报错?
Why does $book give an error, but $title and $author don't?
我知道 $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 的 FOR 中绑定的变量仅在 FLWOR 中可见。
XQuery 不是过程语言;它的功能。执行系统并行或乱序 运行 你的表达式是完全没问题的。这是函数式语言的一个很酷的特性。
想象一个数学函数 (a*b) + (c*d)
。评估系统可能会并行执行 a*b
和 c*d
部分,而用户无法分辨。这与 XQuery 中的思想相同。许多工作可以并行进行,您不必管理它,您甚至不知道。
在您的示例中,您在语句中提供了 3 个表达式,每个表达式都是独立的。您不应该将您的程序视为 运行 完全自上而下地留下副作用变量变化。
突击测验:这 return 是什么?
for $i in (1 to 3)
return $i, 4
它是 1 2 3 4
因为它是一个 FLWOR 表达式 returning 1 2 3
后跟一个表达式 returning 4
。并且您不能在第二个表达式中引用 $i
。
for $i in (1 to 3)
return $i
,
4
我知道 $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 的 FOR 中绑定的变量仅在 FLWOR 中可见。
XQuery 不是过程语言;它的功能。执行系统并行或乱序 运行 你的表达式是完全没问题的。这是函数式语言的一个很酷的特性。
想象一个数学函数 (a*b) + (c*d)
。评估系统可能会并行执行 a*b
和 c*d
部分,而用户无法分辨。这与 XQuery 中的思想相同。许多工作可以并行进行,您不必管理它,您甚至不知道。
在您的示例中,您在语句中提供了 3 个表达式,每个表达式都是独立的。您不应该将您的程序视为 运行 完全自上而下地留下副作用变量变化。
突击测验:这 return 是什么?
for $i in (1 to 3)
return $i, 4
它是 1 2 3 4
因为它是一个 FLWOR 表达式 returning 1 2 3
后跟一个表达式 returning 4
。并且您不能在第二个表达式中引用 $i
。
for $i in (1 to 3)
return $i
,
4