xquery - 在 XQuery 中更新计数器

标签 xquery marklogic

我想在 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/

相关文章:

linux - Linux 上的 Blobfuse 与 Azure - 如何在 Azure 上创建 tmp-path

马克逻辑 8 : delete in bulk

作为 App Builder 构面的 M​​arkLogic 路径范围索引

sparql - 如何使用模式过滤sparql查询结果?

SQL - 空白默认命名空间

xquery - xquery初学者-如何在FLWOR语句中使用if-then-else

xml - xquery order by 和 fn 替换的结果

xml - 如何解决 Marklogic 中的 'Conflicting Update' 错误

Marklogic Rest Api Instance install - 指令怎么会失败?

xquery - 使用 MarkLogic 时不要在构面中包含重复数据