Skip to content

Commit efe2b0d

Browse files
[bugfix] Fix serialization item separators
1 parent 2b0738c commit efe2b0d

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.exist.dom.memtree.DocumentBuilderReceiver;
2626
import org.exist.dom.memtree.DocumentImpl;
2727
import org.exist.dom.memtree.MemTreeBuilder;
28+
import org.exist.storage.serializers.EXistOutputKeys;
2829
import org.exist.util.serializer.XQuerySerializer;
2930
import org.exist.xquery.*;
3031
import org.exist.xquery.functions.map.AbstractMapType;
@@ -41,6 +42,8 @@
4142

4243
public class FunSerialize extends BasicFunction {
4344

45+
private final static String DEFAULT_ITEM_SEPARATOR = "\n";
46+
4447
public final static FunctionSignature[] signatures = {
4548
new FunctionSignature(
4649
new QName("serialize", Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX),
@@ -81,7 +84,8 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
8184

8285
Sequence seq = args[0];
8386
if (xqSerializer.normalize()) {
84-
seq = normalize(this, context, seq);
87+
final String itemSeparator = outputProperties.getProperty(EXistOutputKeys.ITEM_SEPARATOR, DEFAULT_ITEM_SEPARATOR);
88+
seq = normalize(this, context, seq, itemSeparator);
8589
}
8690

8791
xqSerializer.serialize(seq);
@@ -133,7 +137,7 @@ private static boolean isSerializationParametersElement(final Item item) {
133137
* @return normalized sequence
134138
* @throws XPathException in case of dynamic error
135139
*/
136-
public static Sequence normalize(final Expression callingExpr, final XQueryContext context, final Sequence input) throws XPathException {
140+
public static Sequence normalize(final Expression callingExpr, final XQueryContext context, final Sequence input, final String itemSeparator) throws XPathException {
137141
if (input.isEmpty())
138142
// "If the sequence that is input to serialization is empty, create a sequence S1 that consists of a zero-length string."
139143
{return StringValue.EMPTY_STRING;}
@@ -154,7 +158,7 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
154158
// "For each subsequence of adjacent strings in S2, copy a single string to the new sequence
155159
// equal to the values of the strings in the subsequence concatenated in order, each separated
156160
// by a single space."
157-
{((StringValue)last).append(" " + next.getStringValue());}
161+
{((StringValue)last).append((itemSeparator == null ? " " : itemSeparator) + next.getStringValue());}
158162
else
159163
// "For each item in S1, if the item is atomic, obtain the lexical representation of the item by
160164
// casting it to an xs:string and copy the string representation to the new sequence;"

exist-core/src/main/java/org/exist/xquery/functions/util/Eval.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ private Sequence doEval(final XQueryContext evalContext, final Sequence contextS
404404

405405
final Sequence seq;
406406
if (xqSerializer.normalize()) {
407-
seq = FunSerialize.normalize(this, context, result);
407+
seq = FunSerialize.normalize(this, context, result, null);
408408
} else {
409409
seq = result;
410410
}

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

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ declare %private function ser:adaptive-map-params($data) {
6161
ser:adaptive-map-params($data, ())
6262
};
6363

64+
declare %private function ser:serialize-with-item-separator($data as item()*, $method as xs:string) {
65+
let $options :=
66+
<output:serialization-parameters
67+
xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
68+
<output:method value="{$method}"/>
69+
<output:indent>no</output:indent>
70+
<output:item-separator>--</output:item-separator>
71+
</output:serialization-parameters>
72+
return
73+
fn:serialize($data, $options)
74+
};
75+
6476
declare variable $ser:atomic :=
6577
<atomic:root xmlns:atomic="http://www.w3.org/XQueryTest" xmlns:foo="http://www.example.com/foo"
6678
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
@@ -202,7 +214,8 @@ function ser:serialize-empty-params() {
202214
};
203215

204216
declare
205-
%test:assertEquals("aaa bbb")
217+
%test:assertEquals("aaa
218+
bbb")
206219
function ser:serialize-atomic() {
207220
let $nodes := ("aaa", "bbb")
208221
return
@@ -754,4 +767,46 @@ declare
754767
function ser:exist-process-xsl-pi-false() {
755768
let $doc := doc($ser:collection || "/test-xsl.xml")
756769
return fn:serialize($doc, map {xs:QName("exist:process-xsl-pi"): false()})
757-
};
770+
};
771+
772+
declare
773+
%test:assertEquals("1--2")
774+
function ser:item-separator-text-method() {
775+
let $data := (1, 2)
776+
return ser:serialize-with-item-separator($data, "text")
777+
};
778+
779+
declare
780+
%test:assertEquals("1--2")
781+
function ser:item-separator-html-method() {
782+
let $data := (1, 2)
783+
return ser:serialize-with-item-separator($data, "html")
784+
};
785+
786+
declare
787+
%test:assertEquals("1--2")
788+
function ser:item-separator-xhtml-method() {
789+
let $data := (1, 2)
790+
return ser:serialize-with-item-separator($data, "xhtml")
791+
};
792+
793+
declare
794+
%test:assertEquals("1--2")
795+
function ser:item-separator-xml-method() {
796+
let $data := (1, 2)
797+
return ser:serialize-with-item-separator($data, "xml")
798+
};
799+
800+
declare
801+
%test:assertEquals("1--2")
802+
function ser:item-separator-adaptive-method() {
803+
let $data := (1, 2)
804+
return ser:serialize-with-item-separator($data, "adaptive")
805+
};
806+
807+
declare
808+
%test:assertEquals("1--2")
809+
function ser:test() {
810+
let $data := (1, 2)
811+
return ser:serialize-with-item-separator($data, "xml")
812+
};

0 commit comments

Comments
 (0)