gpt4 book ai didi

xquery - 在 XQuery 中更新计数器

转载 作者:行者123 更新时间:2023-12-03 22:47:37 25 4
gpt4 key购买 nike

我想在 xquery 中创建一个计数器。我最初的尝试如下所示:

let $count := 0
for $prod in $collection
let $count := $count + 1
return
<counter>{$count }</counter>

预期结果:

<counter>1</counter>
<counter>2</counter>
<counter>3</counter>

实际结果:

<counter>1</counter>
<counter>1</counter>
<counter>1</counter>
$count变量无法更新或被重置。为什么我不能重新分配现有变量?获得所需结果的更好方法是什么?

最佳答案

尝试使用 '在' :

for $d at $p in $collection
return
element counter { $p }

这将为您提供每个“$d”的位置。如果您想将它与 order by 一起使用子句,这将不起作用,因为位置基于初始顺序,而不是排序结果。为了克服这个问题,只需将 FLWOR 表达式的排序结果保存在一个变量中,并使用 at第二个 FLWOR 中的子句,它只是迭代第一个排序结果。

let $sortResult := for $item in $collection
order by $item/id
return $item

for $sortItem at $position in $sortResult
return <item position="{$position}"> ... </item>

关于xquery - 在 XQuery 中更新计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10294677/

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