@@ -82,13 +82,16 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
82
82
try (final StringWriter writer = new StringWriter ()) {
83
83
final XQuerySerializer xqSerializer = new XQuerySerializer (context .getBroker (), outputProperties , writer );
84
84
85
- Sequence seq = args [0 ];
85
+ final Sequence input = args [0 ];
86
86
if (xqSerializer .normalize ()) {
87
87
final String itemSeparator = outputProperties .getProperty (EXistOutputKeys .ITEM_SEPARATOR , DEFAULT_ITEM_SEPARATOR );
88
- seq = normalize (this , context , seq , itemSeparator );
88
+ final Sequence normalized = normalize (this , context , input , itemSeparator );
89
+ xqSerializer .serialize (normalized );
90
+ }
91
+ else {
92
+ xqSerializer .serialize (input );
89
93
}
90
94
91
- xqSerializer .serialize (seq );
92
95
return new StringValue (this , writer .toString ());
93
96
} catch (final IOException | SAXException e ) {
94
97
throw new XPathException (this , FnModule .SENR0001 , e .getMessage ());
@@ -97,9 +100,9 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
97
100
98
101
public static Properties getSerializationProperties (final Expression callingExpr , final Item parametersItem ) throws XPathException {
99
102
final Properties outputProperties ;
100
- if (parametersItem .getType () == Type .MAP ) {
103
+ if (parametersItem .getType () == Type .MAP ) {
101
104
outputProperties = SerializerUtils .getSerializationOptions (callingExpr , (AbstractMapType ) parametersItem );
102
- } else if (isSerializationParametersElement (parametersItem )) {
105
+ } else if (isSerializationParametersElement (parametersItem )) {
103
106
outputProperties = new Properties ();
104
107
SerializerUtils .getSerializationOptions (callingExpr , (NodeValue ) parametersItem , outputProperties );
105
108
} else {
@@ -144,20 +147,33 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
144
147
if (input .isEmpty ())
145
148
// "If the sequence that is input to serialization is empty, create a sequence S1 that consists of a zero-length string."
146
149
{return StringValue .EMPTY_STRING ;}
147
- final String _separator = itemSeparator == null ? DEFAULT_ITEM_SEPARATOR : itemSeparator ;
148
150
final ValueSequence temp = new ValueSequence (input .getItemCount ());
149
151
for (final SequenceIterator i = input .iterate (); i .hasNext (); ) {
150
152
final Item next = i .nextItem ();
151
- if (Type .subTypeOf (next .getType (), Type .NODE )) {
152
- if (next .getType () == Type .ATTRIBUTE || next .getType () == Type .NAMESPACE || next .getType () == Type .FUNCTION_REFERENCE )
153
- {throw new XPathException (callingExpr , FnModule .SENR0001 ,
154
- "It is an error if an item in the sequence to serialize is an attribute node or a namespace node." );}
153
+ final int itemType = next .getType ();
154
+ if (Type .subTypeOf (itemType , Type .NODE )) {
155
+ if (itemType == Type .ATTRIBUTE || itemType == Type .NAMESPACE || itemType == Type .FUNCTION_REFERENCE ) {
156
+ throw new XPathException (callingExpr , FnModule .SENR0001 ,
157
+ "It is an error if an item in the sequence to serialize is an attribute node or a namespace node." );
158
+ }
159
+ // if (itemType == Type.TEXT || itemType == Type.COMMENT) {
160
+ // final StringValue stringRepresentation = new StringValue(callingExpr, next.getStringValue());
161
+ // if (stringRepresentation.getStringValue().isEmpty()) {
162
+ // continue;
163
+ // }
164
+ // temp.add(stringRepresentation);
165
+ // }
155
166
temp .add (next );
156
167
} else {
157
168
// atomic value
158
169
// "For each item in S1, if the item is atomic, obtain the lexical representation of the item by
159
170
// casting it to an xs:string and copy the string representation to the new sequence;"
160
- temp .add (new StringValue (callingExpr , next .getStringValue ()));
171
+ final StringValue stringRepresentation = new StringValue (callingExpr , next .getStringValue ());
172
+ // skip values that evaluate to an empty string
173
+ if (stringRepresentation .getStringValue ().isEmpty ()) {
174
+ continue ;
175
+ }
176
+ temp .add (stringRepresentation );
161
177
}
162
178
}
163
179
@@ -170,10 +186,12 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
170
186
if (Type .subTypeOf (next .getType (), Type .NODE )) {
171
187
next .copyTo (context .getBroker (), receiver );
172
188
} else {
173
- receiver .characters (next .getStringValue ());
189
+ final String stringValue = next .getStringValue ();
190
+ receiver .characters (stringValue );
174
191
}
192
+ // add itemSeparator if there is a next item and the current item is not an empty string
175
193
if (i .hasNext ()) {
176
- receiver .characters (_separator );
194
+ receiver .characters (itemSeparator );
177
195
}
178
196
}
179
197
return (DocumentImpl )receiver .getDocument ();
0 commit comments