内的XQuery计数器 [英] XQuery counters inside a for

查看:119
本文介绍了内的XQuery计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下XQuery代码:

Let's say I have the XQuery code below:

   for $y in doc("file.xml")/A/B

        for $x in $y/C where $x/constraint1 != "-" and $x/constraint2 > 2.00
            do stuff 

我可以使用计数器来计算要输入多少代码在第二个for循环内?
我尝试过此操作:

Can I use a counter, to count how many my code will enter inside the second for loop? I tried this:

   for $y in doc("file.xml")/A/B
       let $i := 0
        for $x in $y/C where $x/constraint1 != "-" and $x/constraint2 > 2.00
            $i := $i + 1

但我遇到了编译错误。我还需要总结一些约束,例如:

but I got compile errors. I also I need to sum some constraints like this:

   for $y in doc("file.xml")/A/B
       let $i := 0
       let $sum := 0
        for $x in $y/C where $x/constraint1 != "-" and $x/constraint2 > 2.13
            $i := $i + 1
            $sum := $sum + $x/constraint2

但当然也没有用:(。

任何建议都会受到赞赏。
另外,您能建议吗

Any suggestions will be highly appreciated. Also, can you suggest a good book/tutorial/site for doing such stuff?

推荐答案

您并不经常需要它,但是如果您确实需要确实需要在XQuery 中用于表达式的计数器变量(类似于中的 position() xsl:for-each ),您可以使用 at 变量

You don't need it very often, but if you really do need a counter variable inside an XQuery for expression (analogous to position() in an xsl:for-each) you can use an at variable

for $x at $pos in //item return
  <item position="{$pos}" value="{string($x)}"/>

这篇关于内的XQuery计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆