Updating counter in XQuery

2020-02-05 12:28发布

I want to create a counter in xquery. My initial attempt looked like the following:

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

Expected result:

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

Actual result:

<counter>1</counter>
<counter>1</counter>  
<counter>1</counter>

The $count variable either failing to update or being reset. Why can't I reassign an existing variable? What would be a better way to get the desired result?

7条回答
戒情不戒烟
2楼-- · 2020-02-05 13:22

Use xdmp:set instead of the below query

let $count := 0
for $prod in (1 to 4)
return ( xdmp:set($count,number($count+1)) ,<counter>{$count }</counter> 
查看更多
登录 后发表回答