JDK-8230359 : Transform results in inconsistent declarations with the sources
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxp
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2019-08-29
  • Updated: 2020-02-12
Related Reports
Relates :  
Relates :  
Relates :  
The Transformer has following issues while producing the XML Declaration:

1. Inconsistent Transformation Results 

For a source without standalone attribute in its declaration, the transformer returns a result that:
when SAXSource is used, does not contain the standalone attribute
when DOMSource is used, contains the standalone attribute with a value 'no'.

For a source with an encoding other than "UTF-8", the transformer returns a result that:
when SAXSource is used, sets the encoding to UTF-8.
when DOMSource is used, produces an encoding the same as the source.

2. Incorrect Transformation

Using SAXSource and StAXSource, the Transformer always returns a result with a declaration: <?xml version="1.0" encoding="UTF-8"?>, regardless of what's in the Source.

Using DOMSource, the Transformer adds standalone='no' when it's not specified in the Source, omits it when the attribute is 'yes' in the Source. Both are incorrect. The later is more serious since omitting the attribute implies a value of 'no'.


Where feasible, set the encoding and/or standalone property on the Transformer:
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.STANDALONE, standalone);

Using StAXSource with a XMLStreamReader, the above properties can be set with values from the StreamReader, e.g.
    transformer.setOutputProperty(OutputKeys.ENCODING, streamReader.getCharacterEncodingScheme());
    transformer.setOutputProperty(OutputKeys.STANDALONE, streamReader.isStandalone() ? "yes" : "no");