Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8162602

[JAXP] StAX outputs duplicated default namespace declarations

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: In Progress
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 9
    • Fix Version/s: tbd
    • Component/s: xml
    • Labels:
      None

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              clanger Christoph Langer
              Reporter:
              dfuchs Daniel Fuchs
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: