@@ -51,6 +51,15 @@ public enum Feature implements FormatFeature
51
51
*/
52
52
WRITE_XML_DECLARATION (false ),
53
53
54
+ /**
55
+ * Feature that controls whether XML declaration should include the standalone attribute
56
+ * when generator is initialized (true) or not (false). Only honored when
57
+ * {@link Feature#WRITE_XML_DECLARATION WRITE_XML_DECLARATION} is enabled
58
+ *
59
+ * @since 2.19
60
+ */
61
+ WRITE_STANDALONE_YES_TO_XML_DECLARATION (false ),
62
+
54
63
/**
55
64
* Feature that controls whether output should be done as XML 1.1; if so,
56
65
* certain aspects may differ from default (1.0) processing: for example,
@@ -297,15 +306,24 @@ public void initGenerator() throws IOException
297
306
_initialized = true ;
298
307
try {
299
308
boolean xmlDeclWritten ;
300
- if (Feature .WRITE_XML_1_1 .enabledIn (_formatFeatures )) {
301
- _xmlWriter .writeStartDocument ("UTF-8" , "1.1" );
302
- xmlDeclWritten = true ;
303
- } else if (Feature .WRITE_XML_DECLARATION .enabledIn (_formatFeatures )) {
304
- _xmlWriter .writeStartDocument ("UTF-8" , "1.0" );
309
+
310
+ if (Feature .WRITE_XML_1_1 .enabledIn (_formatFeatures ) ||
311
+ Feature .WRITE_XML_DECLARATION .enabledIn (_formatFeatures )) {
312
+
313
+ String xmlVersion = Feature .WRITE_XML_1_1 .enabledIn (_formatFeatures ) ? "1.1" : "1.0" ;
314
+ String encoding = "UTF-8" ;
315
+
316
+ if (Feature .WRITE_STANDALONE_YES_TO_XML_DECLARATION .enabledIn (_formatFeatures )) {
317
+ _xmlWriter .writeStartDocument (xmlVersion , encoding , true );
318
+ } else {
319
+ _xmlWriter .writeStartDocument (encoding , xmlVersion );
320
+ }
321
+
305
322
xmlDeclWritten = true ;
306
323
} else {
307
324
xmlDeclWritten = false ;
308
325
}
326
+
309
327
// as per [dataformat-xml#172], try adding indentation
310
328
if (xmlDeclWritten && (_xmlPrettyPrinter != null )) {
311
329
// ... but only if it is likely to succeed:
0 commit comments