简体   繁体   English

XQuery 表达式求值的示例不是顺序的

[英]Example of XQuery expression evaluation is not sequential

I am looking for an example which shows that XQuery expression evaluation is not sequential.我正在寻找一个示例,它表明 XQuery 表达式评估不是连续的。 It is always mentioned when comparing the functional nature of XQuery with procedural languages.在将 XQuery 的功能特性与过程语言进行比较时,总是会提到它。

Eg in XQuery, 2nd edition , in the section below:例如在XQuery, 2nd edition中,在下面的部分:

The FLWOR expression with its for clause is similar to loops in procedural languages such as C.带有 for 子句的 FLWOR 表达式类似于过程语言中的循环,例如 C。 However, one key difference is that in XQuery, because it is a functional language, the iterations are considered to be in no particular order.但是,一个关键的区别在于,在 XQuery 中,因为它是一种函数式语言,所以认为迭代没有特定的顺序。 They do not necessarily occur sequentially, one after the other.它们不一定顺序发生,一个接一个。

In general, the language is designed so that you can't tell what the order of evaluation is, which makes it difficult to demonstrate that it's not what you expect.通常,该语言的设计使您无法分辨评估的顺序是什么,这使得很难证明它不是您所期望的。 To observe the actual order of evaluation, you need to do something that has side-effects, which generally means straying outside the language specification and using vendor extensions.要观察评估的实际顺序,您需要做一些有副作用的事情,这通常意味着偏离语言规范并使用供应商扩展。 For example you could use the EXPath file module and issue calls to file:append-text() , and then examine the order of entries added to the external text file.例如,您可以使用 EXPath 文件模块并调用file:append-text() ,然后检查添加到外部文本文件的条目的顺序。

Of course, it wouldn't prove anything if the entries in the file are in exactly the order you expect.当然,如果文件中的条目完全按照您期望的顺序,它不会证明任何事情。 Query processors aren't going to use a non-obvious order of execution just for the fun of it.查询处理器不会仅仅为了好玩而使用不明显的执行顺序。 They will only do so if there is something to be gained by changing the order.他们只会在通过更改顺序获得一些东西时才会这样做。 Saxon, for example, will delay evaluating variables until they are used, and will lift expressions out of a loop if it can.例如,Saxon 将延迟评估变量,直到它们被使用,并且如果可以的话,会将表达式从循环中取出。 But you then have the problem, that if you use functions with side-effects like file:append-text() to observe this behaviour, Saxon might detect that your code has side-effects and suppress the optimization.但是您会遇到问题,如果您使用具有副作用的函数(如file:append-text()来观察此行为,Saxon 可能会检测到您的代码具有副作用并抑制优化。

In some implementations you can experience that the order of the result in more complex FLOWR expressions is arbitrary, for instance taking an example from Priscilla Walmsley's book http://www.datypic.com/books/xquery/ a grouping of item elements ('order.xml' in http://www.datypic.com/books/xquery/chapter01.html ) with在某些实现中,您可以体验到更复杂的 FLOWR 表达式中结果的顺序是任意的,例如以 Priscilla Walmsley 的书http://www.datypic.com/books/xquery/ item元素的分组(' order.xml' 在http://www.datypic.com/books/xquery/chapter01.html

<order num="00299432" date="2015-09-15" cust="0221A">
  <item dept="WMN" num="557" quantity="1" color="navy"/>
  <item dept="ACC" num="563" quantity="1"/>
  <item dept="ACC" num="443" quantity="2"/>
  <item dept="MEN" num="784" quantity="1" color="white"/>
  <item dept="MEN" num="784" quantity="1" color="gray"/>
  <item dept="WMN" num="557" quantity="1" color="black"/>
</order>

in Saxon 9.8 HE with the code (7.11 in http://www.datypic.com/books/xquery/chapter07.html )在撒克逊 9.8 HE 中使用代码( http 中的 7.11://www.datypic.com/books/xquery/chapter07.html

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'xml';
declare option output:indent 'yes';

for $item in //item
group by $d:= $item/@dept, $n:= $item/@num
return <group dept="{$d}" num="{$n}" count="{count($item)}"/>

gives the output给出 output

<?xml version="1.0" encoding="UTF-8"?>
<group dept="ACC" num="563" count="1"/>
<group dept="MEN" num="784" count="2"/>
<group dept="WMN" num="557" count="2"/>
<group dept="ACC" num="443" count="1"/>

while for instance with BaseX you might get a different output order (I think based on the input order of items if I remember it correctly) for the group elements, for instance in BaseX 9 I get而例如使用 BaseX,您可能会得到不同的group顺序(如果我没记错的话,我认为是基于项目的输入顺序),例如在 BaseX 9 中我得到

<group dept="WMN" num="557" count="2"/>
<group dept="ACC" num="563" count="1"/>
<group dept="ACC" num="443" count="1"/>
<group dept="MEN" num="784" count="2"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM