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

XML transformation and indentation when using xml:space

XMLWordPrintable

    • b01
    • 11
    • b28
    • Not verified

        ADDITIONAL SYSTEM INFORMATION :
        Tested it on MacOSX (Open Adopt JDK) and windows 10 (https://jdk.java.net/ and Open Adopt JDK)

        A DESCRIPTION OF THE PROBLEM :
        When switching from Java 8 (Oracle) to Java 11 (AdoptOpenJDK) the indentation of our XML has been changed.
        The indentation fails when a xml element is defined with an attribute xml:space=preserve. Everything after the xml element containing the attribute is no longer indented

        REGRESSION : Last worked in version 8u281

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        With the below code the problem can be reproduced. The variable data must be a XML string, for instance
        <?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b>650b4d21</b><c>If true</c><d xml:space=\"preserve\">TRUE</d><e>test</e></a>
        In this case the tags 'a','b','c','d' are indented, everything after tag 'd' will not be indented.

        Transformer transformer = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", Test.class.getClassLoader());

        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "body hyperlink");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        StringWriter writer = new StringWriter();

        StreamSource in = new StreamSource(new StringReader(data));
        transformer.transform(in, new StreamResult(writer));

        System.out.println("-----");
        System.out.println(writer.toString());
        System.out.println("-----");

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        <?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <a>
                <b>650b4d21</b>
                <c>If true</c>
                <d xml:space=\"preserve\">TRUE</d>
                <e>test</e>
            </a>
        ACTUAL -
        <?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <a>
                <b>650b4d21</b>
                <c>If true</c>
                <d xml:space=\"preserve\">TRUE</d><e>test</e></a>

        FREQUENCY : always


              joehw Joe Wang
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: