Skip to content

Commit bdd976d

Browse files
committed
[hotfix] serialize node sequence
1 parent ac2c7e1 commit bdd976d

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunSerialize.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,32 +144,20 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
144144
if (input.isEmpty())
145145
// "If the sequence that is input to serialization is empty, create a sequence S1 that consists of a zero-length string."
146146
{return StringValue.EMPTY_STRING;}
147+
final String _separator = itemSeparator == null ? DEFAULT_ITEM_SEPARATOR : itemSeparator;
147148
final ValueSequence temp = new ValueSequence(input.getItemCount());
148149
for (final SequenceIterator i = input.iterate(); i.hasNext(); ) {
149150
final Item next = i.nextItem();
150151
if (Type.subTypeOf(next.getType(), Type.NODE)) {
151152
if (next.getType() == Type.ATTRIBUTE || next.getType() == Type.NAMESPACE || next.getType() == Type.FUNCTION_REFERENCE)
152153
{throw new XPathException(callingExpr, FnModule.SENR0001,
153154
"It is an error if an item in the sequence to serialize is an attribute node or a namespace node.");}
154-
if (itemSeparator != null && itemSeparator.length() > 0 && !temp.isEmpty()) {
155-
temp.add(new StringValue(itemSeparator + next.getStringValue()));
156-
} else {
157-
temp.add(next);
158-
}
155+
temp.add(next);
159156
} else {
160157
// atomic value
161-
Item last = null;
162-
if (!temp.isEmpty())
163-
{last = temp.itemAt(temp.getItemCount() - 1);}
164-
if (last != null && last.getType() == Type.STRING)
165-
// "For each subsequence of adjacent strings in S2, copy a single string to the new sequence
166-
// equal to the values of the strings in the subsequence concatenated in order, each separated
167-
// by a single space."
168-
{((StringValue)last).append((itemSeparator == null ? " " : itemSeparator) + next.getStringValue());}
169-
else
170-
// "For each item in S1, if the item is atomic, obtain the lexical representation of the item by
171-
// casting it to an xs:string and copy the string representation to the new sequence;"
172-
{temp.add(new StringValue(callingExpr, next.getStringValue()));}
158+
// "For each item in S1, if the item is atomic, obtain the lexical representation of the item by
159+
// casting it to an xs:string and copy the string representation to the new sequence;"
160+
temp.add(new StringValue(callingExpr, next.getStringValue()));
173161
}
174162
}
175163

@@ -184,6 +172,9 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
184172
} else {
185173
receiver.characters(next.getStringValue());
186174
}
175+
if (i.hasNext()) {
176+
receiver.characters(_separator);
177+
}
187178
}
188179
return (DocumentImpl)receiver.getDocument();
189180
} catch (final SAXException e) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,11 @@ function ser:serialize-html-5-needs-escape-elements() {
927927
=> serialize($params)
928928
=> normalize-space()
929929
};
930+
931+
(: test for https://github.com/eXist-db/exist/issues/4702 :)
932+
declare
933+
%test:assertEquals("<a>foo</a> <b>bar</b>")
934+
function ser:sequence-of-nodes() {
935+
(<a>foo</a>, <b>bar</b>) => serialize()
936+
};
937+

0 commit comments

Comments
 (0)