for 内的 XQuery 计数器

发布于 2024-11-01 19:27:35 字数 762 浏览 2 评论 0原文

假设我有下面的 XQuery 代码:

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

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

我可以使用计数器来计算有多少代码将进入第二个 for 循环吗? 我尝试了这个:

   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

但出现编译错误。我还需要总结一些这样的约束:

   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

但是当然这也不起作用:(。

任何建议将不胜感激。 另外,你能推荐一本好书/教程/网站来做这些事情吗?

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 

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

but of course this didn't work either :(.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的旅程 2024-11-08 19:27:36

我认为您还没有理解声明性范式的基础知识。

总计数和总和将为:

let $items := doc('file.xml')/A/B/C[constraint1 != '-' and constraint2 > 2.13]
return ('Count:', count($items), 'Sum:', sum($items/constraint2))

部分计数和总和将为:

let $items := doc('file.xml')/A/B/C[constraint1 != '-' and constraint2 > 2.13]
for $pos in (1 to count($items))
return ('Count:', $pos, 'Sum:', sum($items[position() le $pos]/constraint2))

I don't think you have understood the basics of declarative paradigm.

Total count and sum would be:

let $items := doc('file.xml')/A/B/C[constraint1 != '-' and constraint2 > 2.13]
return ('Count:', count($items), 'Sum:', sum($items/constraint2))

Partial count and sum would be:

let $items := doc('file.xml')/A/B/C[constraint1 != '-' and constraint2 > 2.13]
for $pos in (1 to count($items))
return ('Count:', $pos, 'Sum:', sum($items[position() le $pos]/constraint2))
柠檬色的秋千 2024-11-08 19:27:36

要在循环中显示计数器,最好、更简单的方法是在 for 循环中添加 at $pos 。 $pos 将用作计数器。

代码片段:

{for $artist **at $pos** in (/ns:Entertainment/ns:Artists/ns:Name)

  return
      <tr><td>{$pos}  
    {$artist}</td></tr>

}

输出:

1   Artist

2   Artist 

3   Artist 

4   Artist

5   Artist

6   Artist

To Display counter in loop the best and easier way is to add at $pos in your for loop. $pos will work as a counter.

Code Snippet :

{for $artist **at $pos** in (/ns:Entertainment/ns:Artists/ns:Name)

  return
      <tr><td>{$pos}  
    {$artist}</td></tr>

}

Output:

1   Artist

2   Artist 

3   Artist 

4   Artist

5   Artist

6   Artist
任谁 2024-11-08 19:27:36

如果您不想对集合中的每个项目进行计数或操作,但只想在特定条件适用时进行计数/操作,则这是另一种解决方案。
在这种情况下,您可能会记得 XQuery 的函数式编程范例,并通过使用递归函数调用来实现此计数/操作:

declare function ActIf($collection as element(*)*, $index as xs:integer, $indexMax as xs:integer, $condition as xs:string, $count as xs:integer) as xs:integer
{
if($index <= $indexMax) then
    let $element := $collection[$index]
    if($element/xyz/text() eq $condition) then
        (: here you can call a user-defined function before continuing to the next element :)
        ActIf($collection, $index + 1, $indexMax, $condition, $count + 1)
    else
        ActIf($collection, $index + 1, $indexMax, $condition, $count)
else (: no more items left:)
    $count
};

更简单的方法是使用适当的 XPath 表达式:

for $element at $count in $collection[xyz = $condition]
return
    (: call your user-defined function :)

或者甚至

myFunction($collection[xyz = $condition])

递归解决方案可能会非常有帮助,如果您想要用 XQuery 实现更复杂的东西...

Here is another solution, if you don't want to count or act on every item of a collection, but you want to count/act only, if a certain condition applies.
In that case you might remember the functional programming paradigm of XQuery and implement this counting/acting by using recursive function calls:

declare function ActIf($collection as element(*)*, $index as xs:integer, $indexMax as xs:integer, $condition as xs:string, $count as xs:integer) as xs:integer
{
if($index <= $indexMax) then
    let $element := $collection[$index]
    if($element/xyz/text() eq $condition) then
        (: here you can call a user-defined function before continuing to the next element :)
        ActIf($collection, $index + 1, $indexMax, $condition, $count + 1)
    else
        ActIf($collection, $index + 1, $indexMax, $condition, $count)
else (: no more items left:)
    $count
};

The more easier way would be the usage of an appropriate XPath expression:

for $element at $count in $collection[xyz = $condition]
return
    (: call your user-defined function :)

or even

myFunction($collection[xyz = $condition])

The recursive solution might be quite helpful, if you want to implement something more complex with XQuery...

一念一轮回 2024-11-08 19:27:35

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

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

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)}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文