Exist: position() function off-by-one error

Created on 18 Feb 2021  路  1Comment  路  Source: eXist-db/exist

Working with eXist 5.2, the position function returns what appears to be a 0-based index instead of 1-based as required:

let $a := <a>
  <b n="a"/>
  <b n="b"/>
  <b n="c"/>
</a>
return $a/b[@n=('b', 'c')]/position()

returns

0
1

instead of the expected

1
2

To Reproduce
The follow xQuery test tests for the proper results.

xquery version "3.1";

module namespace t="http://exist-db.org/xquery/test";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare variable $t:XML := document {
<a>
  <b n="a"/>
  <b n="b"/>
  <b n="c"/>
</a>
};

declare
    %test:assertTrue
function t:test() {
    let $r := $t:XML/a/b[@n=('b', 'c')]/position()
    return count($r) eq 2 and $r[1] eq 1 and $r[2] eq 2
};

Context (please always complete the following information):

  • OS: Mac OS 11.1, RHEL 7, CentOS 7
  • eXist-db version: 5.2.0
  • Java Version: 11.0.8 and others

Additional context

  • How is eXist-db installed? JAR installer
  • Any custom changes in e.g. conf.xml? No
bug xquery

Most helpful comment

I can reproduce the issue on 5.1.1
Any predicate will cause the position() to be off by one. Both in-memory and persisted nodes are affected.

xquery version "3.1";

declare variable $local:XML := document {
<a>
  <b n="a"/>
  <b n="b"/>
  <b n="c"/>
</a>
};

xmldb:store('/db/temp', 'stored.xml', $local:XML),
$local:XML/a/position(),
doc('/db/temp/stored.xml')/a/position(),
$local:XML/a[true()]/position(),
doc('/db/temp/stored.xml')/a[true()]/position()

>All comments

I can reproduce the issue on 5.1.1
Any predicate will cause the position() to be off by one. Both in-memory and persisted nodes are affected.

xquery version "3.1";

declare variable $local:XML := document {
<a>
  <b n="a"/>
  <b n="b"/>
  <b n="c"/>
</a>
};

xmldb:store('/db/temp', 'stored.xml', $local:XML),
$local:XML/a/position(),
doc('/db/temp/stored.xml')/a/position(),
$local:XML/a[true()]/position(),
doc('/db/temp/stored.xml')/a[true()]/position()
Was this page helpful?
0 / 5 - 0 ratings