Skip to content

Commit c9d18fb

Browse files
authored
Merge pull request #5589 from Robin6939/hotfix/forclause-positionalvar-empty-sequence-when-allowempty
Fix ForClause PositionalVar empty sequence handling
2 parents 10053f2 + f7c3016 commit c9d18fb

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

exist-core/src/main/java/org/exist/xquery/ForExpr.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private void processItem(LocalVariable var, Item contextItem, Sequence in, Seque
229229
context.proceed(this);
230230
context.setContextSequencePosition(p, in);
231231
if (positionalVariable != null) {
232-
at.setValue(new IntegerValue(this, p + 1));
232+
final int position = contextItem == AtomicValue.EMPTY_VALUE ? 0 : p + 1;
233+
at.setValue(new IntegerValue(this, position));
233234
}
234235
final Sequence contextSequence = contextItem.toSequence();
235236
// set variable value to current item

exist-core/src/test/xquery/xquery3/flwor.xql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,32 @@ function flwor:allowing-empty($n as xs:integer) {
156156
return concat("[", $x, "]")
157157
};
158158

159+
declare
160+
%test:args(4)
161+
%test:assertEquals(":0")
162+
%test:args(2)
163+
%test:assertEquals("b:1")
164+
%test:args(1)
165+
%test:assertEquals("a:1")
166+
%test:args(5)
167+
%test:assertEquals(":0")
168+
function flwor:allowing-empty-fix($n as xs:integer) {
169+
let $sequence := ("a", "b", "c")[$n]
170+
for $x allowing empty at $y in $sequence
171+
return $x || ":" || $y
172+
};
173+
174+
declare
175+
%test:args(4)
176+
%test:assertEquals("")
177+
%test:args(2)
178+
%test:assertEquals("b:1")
179+
function flwor:no-allow-empty($n as xs:integer) {
180+
let $sequence := ("a", "b", "c")[$n]
181+
return
182+
if (empty($sequence)) then
183+
""
184+
else
185+
for $x at $y in $sequence
186+
return $x || ":" || $y
187+
};

0 commit comments

Comments
 (0)