From def5a78cbff7199781010d930253a3a4cbb8631c Mon Sep 17 00:00:00 2001 From: Robin6939 Date: Wed, 25 Dec 2024 11:27:11 +0530 Subject: [PATCH] [bugfix] Fix ForClause PositionalVar empty sequence handling. --- .../src/main/java/org/exist/xquery/ForExpr.java | 6 +++++- exist-core/src/test/xquery/xquery3/flwor.xql | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/exist-core/src/main/java/org/exist/xquery/ForExpr.java b/exist-core/src/main/java/org/exist/xquery/ForExpr.java index ca8b9c1751a..0fcb918fcc9 100644 --- a/exist-core/src/main/java/org/exist/xquery/ForExpr.java +++ b/exist-core/src/main/java/org/exist/xquery/ForExpr.java @@ -229,7 +229,11 @@ 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)); + if (contextItem.getType() == Type.ANY_ATOMIC_TYPE) { + at.setValue(new IntegerValue(this, 0)); + } else { + at.setValue(new IntegerValue(this, p + 1)); + } } final Sequence contextSequence = contextItem.toSequence(); // set variable value to current item diff --git a/exist-core/src/test/xquery/xquery3/flwor.xql b/exist-core/src/test/xquery/xquery3/flwor.xql index 42dbabd9d26..20654b92d21 100644 --- a/exist-core/src/test/xquery/xquery3/flwor.xql +++ b/exist-core/src/test/xquery/xquery3/flwor.xql @@ -156,3 +156,17 @@ 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 +};