如果您有一个嵌套的FLWOR语句,其中包含两个“for”语句:
for $x at $xPos in (1, 2, 3)
for $y at $yPos in fn:doc("/foo")//b
return ()如何准确计算for循环运行了多少次?假设'fn:doc("/foo")//b‘以随机长度返回序列,下面是运行的示例:
$xPos $yPos
1 1
1 2
1 3
2 1
2 2
3 1 <-- for loop ends here (total times ran = 6)另一个例子可以是:
$xPos $yPos
1 1
1 2
2 1
2 2
2 3
3 1
3 2
3 3
3 4 <-- for loop ends here (total times ran = 9)希望你能明白我的意思。如何在嵌套的for循环中保留和更新一个计数器变量,以计算在循环的每一次迭代中没有重置的情况下,我在这个循环中运行了多少次?
澄清编辑:这个问题仅仅是出于好奇,想知道在XQuery中这是否可行。我知道,您只需简单地放入这样的let语句,只需跟踪$xPos,这是一件简单的事情:
for $x at $xPos in (1, 2, 3)
let $_ :=
for $y at $yPos in fn:doc("/foo")//b
return ()
return ()发布于 2019-12-16 05:43:40
在MarkLogic中,您可以使用xdmp:设置来打破严格的FLWOR范式。
let $count := 0
for $x at $xPos in (1, 2, 3)
for $y at $yPos in ("a", "b", "c")
let $set := xdmp:set($count, $count + 1)
return concat($count, ": ", $x, " ", $y)生产:
1: 1 a
2: 1 b
3: 1 c
4: 2 a
5: 2 b
6: 2 c
7: 3 a
8: 3 b
9: 3 c发布于 2019-12-14 21:09:45
XQuery 3有一个count子句(https://www.w3.org/TR/xquery-31/#id-count):
for $x in (1 to 3)
for $y in //b
count $c
return ``[count `{$c}`: y: `{$y}`]``输入的https://xqueryfiddle.liberty-development.net/6qVRKvF
<doc>
<b>a</b>
<b>b</b>
<b>c</b>
</doc>返回结果
count 1: y: a
count 2: y: b
count 3: y: c
count 4: y: a
count 5: y: b
count 6: y: c
count 7: y: a
count 8: y: b
count 9: y: c但是,如果我正确地解释了11626,那么Marklogic就不支持count子句。
发布于 2019-12-16 08:47:35
然后数一数怎么样:
for $x at $xPos in (1, 2, 3)
let $c :=
for $y at $yPos in fn:doc("/foo")//b
return 1
return count($c)哈哈!
https://stackoverflow.com/questions/59338422
复制相似问题