Skip to content

Commit

Permalink
Merge pull request #5589 from Robin6939/hotfix/forclause-positionalva…
Browse files Browse the repository at this point in the history
…r-empty-sequence-when-allowempty

Fix ForClause PositionalVar empty sequence handling
  • Loading branch information
line-o authored Dec 30, 2024
2 parents 10053f2 + f7c3016 commit c9d18fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion exist-core/src/main/java/org/exist/xquery/ForExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ private void processItem(LocalVariable var, Item contextItem, Sequence in, Seque
context.proceed(this);
context.setContextSequencePosition(p, in);
if (positionalVariable != null) {
at.setValue(new IntegerValue(this, p + 1));
final int position = contextItem == AtomicValue.EMPTY_VALUE ? 0 : p + 1;
at.setValue(new IntegerValue(this, position));
}
final Sequence contextSequence = contextItem.toSequence();
// set variable value to current item
Expand Down
29 changes: 29 additions & 0 deletions exist-core/src/test/xquery/xquery3/flwor.xql
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,32 @@ function flwor:allowing-empty($n as xs:integer) {
return concat("[", $x, "]")
};

declare
%test:args(4)
%test:assertEquals(":0")
%test:args(2)
%test:assertEquals("b:1")
%test:args(1)
%test:assertEquals("a:1")
%test:args(5)
%test:assertEquals(":0")
function flwor:allowing-empty-fix($n as xs:integer) {
let $sequence := ("a", "b", "c")[$n]
for $x allowing empty at $y in $sequence
return $x || ":" || $y
};

declare
%test:args(4)
%test:assertEquals("")
%test:args(2)
%test:assertEquals("b:1")
function flwor:no-allow-empty($n as xs:integer) {
let $sequence := ("a", "b", "c")[$n]
return
if (empty($sequence)) then
""
else
for $x at $y in $sequence
return $x || ":" || $y
};

0 comments on commit c9d18fb

Please sign in to comment.