-
Bug
-
Resolution: Won't Fix
-
P3
-
1.4.2_08
-
x86
-
linux
Compile and run the following class:
---%<---
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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 TestNestedNamespaces {
public static void main(String[] args) throws Exception {
Transformer t1 = TransformerFactory.newInstance().newTransformer();
t1.setOutputProperty(OutputKeys.INDENT, "yes");
t1.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
String xslt =
"<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>" +
"</xsl:stylesheet>";
Transformer t2 = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt)));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = factory.newDocumentBuilder().getDOMImplementation().createDocument("urn:root", "root", null);
doc.getDocumentElement().appendChild(doc.createElementNS("urn:child", "child"));
t1.transform(new DOMSource(doc), new StreamResult(System.out));
t2.transform(new DOMSource(doc), new StreamResult(System.out));
String xml = "<root xmlns='urn:root'/>";
doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
doc.getDocumentElement().appendChild(doc.createElementNS("urn:child", "child"));
t1.transform(new DOMSource(doc), new StreamResult(System.out));
t2.transform(new DOMSource(doc), new StreamResult(System.out));
}
}
---%<---
When run under JDK 1.5 (or 1.6) it works OK:
---%<---
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
---%<---
But under JDK 1.4 the output is mangled in various ways, depending both on how you created the document, and how you serialized it:
---%<---
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:root"/>
</root>
---%<---
Note that the output is only correct in the case that you created the document in-memory (not from parsing), *and* serialized it using an explicit stylesheet (not the default identity transformation).
---%<---
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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 TestNestedNamespaces {
public static void main(String[] args) throws Exception {
Transformer t1 = TransformerFactory.newInstance().newTransformer();
t1.setOutputProperty(OutputKeys.INDENT, "yes");
t1.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
String xslt =
"<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>" +
"</xsl:stylesheet>";
Transformer t2 = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt)));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = factory.newDocumentBuilder().getDOMImplementation().createDocument("urn:root", "root", null);
doc.getDocumentElement().appendChild(doc.createElementNS("urn:child", "child"));
t1.transform(new DOMSource(doc), new StreamResult(System.out));
t2.transform(new DOMSource(doc), new StreamResult(System.out));
String xml = "<root xmlns='urn:root'/>";
doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
doc.getDocumentElement().appendChild(doc.createElementNS("urn:child", "child"));
t1.transform(new DOMSource(doc), new StreamResult(System.out));
t2.transform(new DOMSource(doc), new StreamResult(System.out));
}
}
---%<---
When run under JDK 1.5 (or 1.6) it works OK:
---%<---
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
---%<---
But under JDK 1.4 the output is mangled in various ways, depending both on how you created the document, and how you serialized it:
---%<---
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:child"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:root">
<child xmlns="urn:root"/>
</root>
---%<---
Note that the output is only correct in the case that you created the document in-memory (not from parsing), *and* serialized it using an explicit stylesheet (not the default identity transformation).
- relates to
-
JDK-4981389 Cannot correctly serialize namespaced DOM trees in either JDK 1.4 or 1.5
-
- Resolved
-