Uploaded image for project: 'XalanJ2'
  1. XalanJ2
  2. XALANJ-2219

Namespace of child element written incorrectly as root namespace


    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.7, 2.6, 2.5
    • Fix Version/s: 2.7.1
    • Component/s: Serialization
    • Labels:
    • Environment:
    • Xalan info:


      Run this class:

      import java.io.StringReader;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.transform.Result;
      import javax.xml.transform.Source;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamResult;
      import javax.xml.transform.stream.StreamSource;
      import org.w3c.dom.Document;
      import org.xml.sax.InputSource;
      public class Test {
      private static final String IDENTITY_XSLT_WITH_INDENT =
      "<xsl:stylesheet version='1.0' " +
      "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " +
      "xmlns:xalan='http://xml.apache.org/xslt' " +
      "exclude-result-prefixes='xalan'>" +
      "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" +
      "<xsl:template match='@*|node()'>" +
      "<xsl:copy>" +
      "<xsl:apply-templates select='@*|node()'/>" +
      "</xsl:copy>" +
      "</xsl:template>" +
      public static void main(String[] args) throws Exception

      { String data = "<root xmlns='root'/>"; Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(data))); /* Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation().createDocument("root", "root", null); */ doc.getDocumentElement().appendChild(doc.createElementNS("child", "child")); Transformer t = TransformerFactory.newInstance().newTransformer( new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT))); Source source = new DOMSource(doc); Result result = new StreamResult(System.out); t.transform(source, result); }


      Just using plain JDK 5.0 JAXP, I get the expected

      <?xml version="1.0" encoding="UTF-8"?>
      <root xmlns="root">
      <child xmlns="child"/>

      If I add Xalan-J to the classpath, I get

      <?xml version="1.0" encoding="UTF-8"?>
      <root xmlns="root">
      <child xmlns="root"/>

      Note the incorrect namespace on the child element.

      This is true in Xalan 2.5.2, 2.6.0, 2.7.0, and dev builds (xalan-gump-24102005.jar).

      Prevents Xalan from being bundled with the NetBeans IDE, as it causes incorrectly written project metadata:


      If you use newTransformer() with no stylesheet the problem goes away (though of course you lose indentation unless it is readded using setOutputProperty). Also if the Document is created in memory rather than parsed (see commented-out code) the problem goes away.

      Does not seem to be reproducible in JDK 6, I don't know why.

      Inclusion of the bundled Xerces in the classpath does not appear to make any difference.


        1. jira2219.patch5.txt
          8 kB
          Brian Minchau
        2. ToStream.patch2.txt
          3 kB
          Brian Minchau
        3. ToStream.patch4.txt
          3 kB
          Brian Minchau

          Issue Links



              • Assignee:
                minchau@ca.ibm.com Brian Minchau
                jglick@netbeans.org Jesse Glick
                Henry Zongaro
              • Votes:
                1 Vote for this issue
                2 Start watching this issue


                • Created: