xquery 1.0 版本中的 XQuery 3.0 等效 group by [英] XQuery 3.0 equivalent group by in xquery 1.0 version

查看:22
本文介绍了xquery 1.0 版本中的 XQuery 3.0 等效 group by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,而我拥有的 saxon jar 文件无法读取 xquery 3.0.

I have the following code, and the saxon jar file I have cannot read xquery 3.0.

for $item in doc("order.xml")//item
group by $d := $item/@dept
order by $d
return 
  <department totalQuantity="{sum($item/@quantity)}"
              code="{$d}"
              numItems="{count($item)}"/>

输出显示部门、totalQuantity 和项目数.应该是按代码分组的,上面的代码不会编译.

The output displays the department, totalQuantity, and the number of items. It's supposed to be grouped by the code, the code above doesn't compile.

我希望有一种方法可以与 xquery 1.0 一起使用

I was hoping theres a way that would work with xquery 1.0

推荐答案

通常与 XQuery 3.0group by"增强的 FLWOR 表达式等效的 XQuery 1.0 表达式依赖于 fn:distinct-values() 函数来查找将您的项目组合在一起的不同键.找到键后,您可以简单地使用 XQuery 1.0 FLWOR 表达式遍历键并选择具有匹配键的项.这是您的查询的 XQuery 1.0 等效项:

Typically the XQuery 1.0 equivalent of a XQuery 3.0 "group by"-enhanced FLWOR expression relies on the fn:distinct-values() function to find the distinct keys for grouping your items together. Having found the keys, you can simply iterate through the keys with an XQuery 1.0 FLWOR expression and select the items with the matching key. Here is the XQuery 1.0 equivalent of your query:

let $items := doc("order.xml")//item
let $depts := distinct-values($items/@dept)
for $dept in $depts
let $dept-items := $items[@dept eq $dept]
order by $dept
return
    <department totalQuantity="{sum($dept-items/@quantity)}"
                code="{$dept}"
                numItems="{count($dept-items)}"/>

这篇关于xquery 1.0 版本中的 XQuery 3.0 等效 group by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆