how to include the value of position() in an Xquery result?

六月ゝ 毕业季﹏ 提交于 2021-02-11 14:22:48

问题


Without using tumbling windows, how is the position itself included in a result?

Obviously "$p" should be the "index" of each $item in $items. That being said, iteration isn't guaranteed to be sequential at all.

output:

<result>
  <items>
    <item pos="$p">5</item>
    <item pos="$p">3</item>
    <item pos="$p">7</item>
    <item pos="$p">2</item>
    <item pos="$p">7</item>
    <item pos="$p">3</item>
  </items>
</result>

query:

xquery version "3.1";

let $items := (5,3,7,2,7,3)
return
   <result>   
      <items>
      {
         for $item in $items[position()]
     let $p := position()
         return <item pos="$p">{$item}</item>
      }
      </items>
   </result>

query adapted from:

https://www.tutorialspoint.com/xquery/xquery_position.htm


回答1:


You talk of using tumbling windows, but your query doesn't use tumbling windows.

It does however use FLWOR expressions, and FLWOR expressions don't set the focus, which means they don't set the value of position().

For your example, I think you want either

for $item at $p in $items return <item pos="{$p}">{$item}</item>

or more simply

$items ! <item pos="{position()}">{.}</item>

With tumbling windows you can bind variables to the positions of the start and end of the window by writing start at $s end at $e and then output $s and $e in your result.



来源:https://stackoverflow.com/questions/65274991/how-to-include-the-value-of-position-in-an-xquery-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!