JDK-8162602 : [JAXP] StAX outputs duplicated default namespace declarations
  • Type: Bug
  • Component: xml
  • Sub-Component: javax.xml.transform
  • Affected Version: 9
  • Priority: P4
  • Status: In Progress
  • Resolution: Unresolved
  • Submitted: 2016-07-27
  • Updated: 2018-09-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
When using StAX to apply an XSL template to an XML document, you end up with an XML where default namespace declarations are duplicated.

For instance:

apply test.xsl:

---------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                <xsl:template match="/">
                               <root xmlns="ns1">
                                               <xsl:call-template name="transform"/>
                               </root>
                </xsl:template>
                <xsl:template name="transform">
                <test xmlns="ns2"><b xmlns="ns2"><c xmlns=""></c></b></test>
                <test xmlns="ns1"><b xmlns="ns2"><c xmlns=""></c></b></test>
                <test><b><c xmlns=""></c></b></test>
                <test xmlns=""><b><c xmlns=""></c></b></test>
                <test xmlns="ns1"><b><c xmlns=""></c></b></test>
                <test xmlns=""/>
                </xsl:template>
</xsl:stylesheet>
-----------------------

to test.xml:

------------------------
<?xml version="1.0" encoding="UTF-8"?><aaa>
    <bbb></bbb>
</aaa>
------------------------

gives you back:

------------------------
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="ns1" xmlns="ns1">
      <test xmlns="ns2" xmlns="ns2"><b><c></c></b></test>
      <test><b xmlns="ns2" xmlns="ns2"><c></c></b></test>
      <test><b><c></c></b></test>
      <test><b><c></c></b></test>
      <test><b><c></c></b></test>
      <test></test>
</root>
-----------------------------

if you use StAXSource and StAXResult:

    public static void main(String[] args) throws XMLStreamException,
            TransformerConfigurationException, TransformerException {
        TransformerFactory trans = TransformerFactory.newInstance();
        XMLInputFactory input = XMLInputFactory.newFactory();
        XMLOutputFactory output = XMLOutputFactory.newFactory();
        XMLEventReader reader = input.createXMLEventReader(XSLTTest.class.getResourceAsStream("test.xsl"));
        StAXSource source = new StAXSource(reader);
        Templates temp = trans.newTemplates(source);
        StAXResult result = new StAXResult(output.createXMLEventWriter(System.out));
        temp.newTransformer().transform(
                new StAXSource(input.createXMLEventReader(XSLTTest.class.getResourceAsStream("test.xml"))),
                result);
    }

The problem doesn't show up if you use SAX and StreamResult.
Comments
I think I have understood the problem - It is a safety net in ToXMLSAXHandler which can be disabled. I'll propose a change on the mailing list.
28-07-2016

This issue was discovered while evaluating JDK-8162598.
27-07-2016