gpt4 book ai didi

xquery - 更新 XQuery 中的变量 - 可能与否?

转载 作者:行者123 更新时间:2023-12-04 10:02:12 28 4
gpt4 key购买 nike

我对 XQuery 中的变量更新有点困惑:
在 [1] 上说:

Variables can't be updated. This means you can't write something like let $x := $x+1. This rule might seem very strange if you're expecting XQuery to behave in the same way as procedural languages such as JavaScript. But XQuery isn't that kind of language, it's a declarative language and works at a higher level. There are no rules about the order in which different expressions are executed (which means that the little yellow triangle that shows the current execution point in the Stylus Studio XQuery debugger and XSLT debugger can sometimes behave in surprising ways), and this means that constructs whose result would depend on order of execution (like variable assignment) are banned.



我想知道是否真的没有办法可靠地更新变量?也许我只是习惯了其他语言中的那些东西,但我真的无法想象/相信它;-)

[1] http://www.stylusstudio.com/xquery_flwor.html ,“L代表LET”章节截图下方第二段

更新:
我必须为此添加一个问题:是否应该可以在 if 语句中更新现有变量,因为在这种情况下执行顺序很明确?我猜你只是不允许在循环中使用 $x = $x+1 之类的东西?

最佳答案

您正在描述不变性,这是 functional languages. 的一个特性这是真的;一旦变量被设置为一个值,它就不能被设置为其他东西。

不变性有很多好处。特别是concurrent programming变得容易多了。

在循环的情况下,每次循环都会创建一个新变量,替换原来的变量。所以不变性仍然成立。您链接的文章中对此进行了详细说明:

Isn't there a variable being updated when you write something like the following?

for $v in //video
let $x := xs:int($v/runtime) * xdt:dayTimeDuration("PT1M")
return concat($v/title, ": ",
hours-from-duration($x), " hour(s) ",
minutes-from-duration($x), " minutes")

(This query shows the running time of each video. It first converts the stored value from a string to an integer, then multiplies it by one minute (PT1M) to get the running time as a duration, so that it can extract the hours and minutes components of the duration. Try it.)

Here the variable $x has a different value each time around the XQuery for loop. This feels a bit like an update. Technically though, each time round the for loop you're creating a new variable with a new value, rather than assigning a new value to the old variable.

关于xquery - 更新 XQuery 中的变量 - 可能与否?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3782508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com