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'.
Workaround
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");
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'.
Workaround
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");
- relates to
-
JDK-6870815 When pretty printing XML from DOM in Java 6, an unwanted standalone="no" attribute appears
-
- Open
-
-
JDK-8231633 DOM: improve declaration handling
-
- Open
-
-
JDK-8230814 Enable SAX ContentHandler to handle XML Declaration
-
- Resolved
-