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

标签 xquery

我对 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/

相关文章:

java - 在 java、OSB、XQuery 中从 XML 获取不同的值

xml - 在 XQuery 1.0 中使用 for 创建列表

java - 如何将 XQuery 结果存储为 BaseX 中的新文档

java - undefined variable 上的 BaseX XQJ API 错误,而变量已定义

sql-server - T-SQL 2014 XQuery 使用子字符串的解决方案

sql-server - 如何在 xquery 中执行不区分大小写的属性选择器

javascript - 是否有与 XQuery/XPath 等效的 JSON?

xquery - 如何在 Marklogic XQuery 中复制/克隆节点

xml - "Variable has not been declared"满足子句中声明的变量错误

java - 用于在 Java 中执行 xqj 的 XML 处理器