如何在 xquery 中对一个序列进行两两迭代?

发布于 2024-08-26 14:09:51 字数 50 浏览 3 评论 0原文

我想迭代 xquery 中的序列并一次获取 2 个元素。做到这一点最简单的方法是什么?

I want to iterate over a sequence in xquery and grab 2 elements at a time. What is the easiest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み格子的夏天 2024-09-02 14:09:51

XQuery 3.0 解决方案

对于此以及更复杂的分页用例,Window Clause 已在 XQuery 3.0 中创建。但是,许多 XQuery 处理器尚不支持它。

窗口示例

这是一个工作示例,您可以在 try.zorba :

for tumbling window $pair in (2, 4, 6, 8, 10, 12, 14)
    start at $s when fn:true()
    end at $e when $e - $s eq 1
return <window>{ $pair }</window>

结果

<window>2 4</window><window>6 8</window><window>10 12</window><window>14</window>

XQuery 3.0 Solution

For this and more complex paging use cases the Window Clause has been created in XQuery 3.0. But, it is not yet supported by many XQuery processors.

Windowing example

Here is a working example that you could execute for example on try.zorba :

for tumbling window $pair in (2, 4, 6, 8, 10, 12, 14)
    start at $s when fn:true()
    end at $e when $e - $s eq 1
return <window>{ $pair }</window>

Result

<window>2 4</window><window>6 8</window><window>10 12</window><window>14</window>
我们只是彼此的过ke 2024-09-02 14:09:51

一种选择是迭代所有项目,一旦项目达到除数(在本例中为 2),就取出这些项目。一个缺点是,如果项目甚至不是除数。例如,此方法不会返回具有奇数个元素的序列的最后一个元素。

for $item at $index in $items
  return
  if ($item mod 2 = 0) then
    ($items[$index - 1], $items[$index])
  else
    ()

另一种选择是使用 mod 和项目的索引。使用这种方法,您可以通过在计数中添加比组中项目数少的 1 来确保包含 $items 序列中的所有元素。

let $group-size := 2

return
for $index in (1 to fn:count($items)+($group-size - 1))[. mod $group-size = 0]
  return
  ($items[$index - 1] , $items[$index])

One option is to iterate over all items and just take the items once the items reach the divisor, in this case 2. The one downside is that you won't reach the last group of items if the items aren't even multiples of the divisor. For instance, the last element of a sequence with an odd number of elements will not be returned with this approach.

for $item at $index in $items
  return
  if ($item mod 2 = 0) then
    ($items[$index - 1], $items[$index])
  else
    ()

Another option is to use mod and the index of the item. Using this approach you can make certain to include all elements in the $items sequence by adding one less than the number of items in your group to the count.

let $group-size := 2

return
for $index in (1 to fn:count($items)+($group-size - 1))[. mod $group-size = 0]
  return
  ($items[$index - 1] , $items[$index])
情绪少女 2024-09-02 14:09:51
let $s := ("a","b","c","d","e","f")

for $i in 1 to xs:integer(count($s) div 2)
return
<pair>
   {($s[$i*2 - 1],$s[$i*2])} 
</pair>

回报

<pair>a b</pair>
<pair>c d</pair>
<pair>e f</pair>
let $s := ("a","b","c","d","e","f")

for $i in 1 to xs:integer(count($s) div 2)
return
<pair>
   {($s[$i*2 - 1],$s[$i*2])} 
</pair>

returns

<pair>a b</pair>
<pair>c d</pair>
<pair>e f</pair>
用心笑 2024-09-02 14:09:51
for $item at $index in $items
return
(
    if ($index mod 2 eq 0) then
    (
        $items[xs:integer(xs:integer($index) - 1)], $items[xs:integer($index)]
    )
    else
    ()
)
for $item at $index in $items
return
(
    if ($index mod 2 eq 0) then
    (
        $items[xs:integer(xs:integer($index) - 1)], $items[xs:integer($index)]
    )
    else
    ()
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文