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 12:59

All the solution above are valid but I would like to mention that you can use the XQuery Scripting extension to set variable values:

variable $count := 0;

for $prod in (1 to 10)
return {
  $count := $count + 1;
  <counter>{$count}</counter>
}

You can try this example live at http://www.zorba-xquery.com/html/demo#twh+3sJfRpHhZR8pHhOdsmqOTvQ=

查看更多
ら.Afraid
3楼-- · 2020-02-05 13:06

Immutable variables

XQuery is a functional programming language, which involves amongst others immutable variables, so you cannot change the value of a variable. On the other hand, a powerful collection of functions is available to you, which solves lots of daily programming problems.

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

let $count in line 1 defines this variable in all scope, which are all following lines in this case. let $count in line 3 defines a new $count which is 0+1, valid in all following lines within this code block - which isn't defined. So you indeed increment $count three times by one, but discard the result immediatly.

BaseX' query info shows the optimized version of this query which is

for $prod in $collection
  return element { "counter" } { 1 }

The solution

To get the total number of elements in $collection, you can just use

return count($collection)

For a list of XQuery functions, you could have a look at the XQuery part of functx which contains both a list of XQuery functions and also some other helpful functions which can be included as a module.

查看更多
家丑人穷心不美
4楼-- · 2020-02-05 13:08

I think you are looking for something like:

XQUERY:

for $x in (1 to 10)
return
  <counter>{$x}</counter>

OUTPUT:

<counter>1</counter>
<counter>2</counter>
<counter>3</counter>
<counter>4</counter>
<counter>5</counter>
<counter>6</counter>
<counter>7</counter>
<counter>8</counter>
<counter>9</counter>
<counter>10</counter>
查看更多
【Aperson】
5楼-- · 2020-02-05 13:16

Try using 'at':

for $d at $p in $collection
return 
element counter { $p }

This will give you the position of each '$d'. If you want to use this together with the order by clause, this won't work since the position is based on the initial order, not on the sort result. To overcome this, just save the sorted result of the FLWOR expression in a variable, and use the at clause in a second FLWOR that just iterates over the first, sorted result.

let $sortResult := for $item in $collection
                   order by $item/id
                   return $item

for $sortItem at $position in $sortResult
return <item position="{$position}"> ... </item>
查看更多
Emotional °昔
6楼-- · 2020-02-05 13:19

Specific to MarkLogic you can also use xdmp:set. But this breaks functional language assumptions, so use it conservatively.

http://docs.marklogic.com/5.0doc/docapp.xqy#display.xqy?fname=http://pubs/5.0doc/apidoc/ExsltBuiltins.xml&category=Extension&function=xdmp:set

For an example of xdmp:set in real-world code, the search parser https://github.com/mblakele/xqysp/blob/master/src/xqysp.xqy might be helpful.

查看更多
SAY GOODBYE
7楼-- · 2020-02-05 13:20

As @Ranon said, all XQuery values are immutable, so you can't update a variable. But if you you really need an updateable number (shouldn't be too often), you can use recursion:

declare function local:loop($seq, $count) {
  if(empty($seq)) then ()
  else
    let $prod  := $seq[1],
        $count := $count + 1
    return (
      <count>{ $count }</count>,
      local:loop($seq[position() > 1], $count)
    )
};

local:loop($collection, 0)

This behaves exactly as you intended with your example.

In XQuery 3.0 a more general version of this function is even defined in the standard library: fn:fold-right($f, $zero, $seq)

That said, in your example you should definitely use at $count as shown by @tohuwawohu.

查看更多
登录 后发表回答