-
Bug
-
Resolution: Unresolved
-
P4
-
9
-
None
-
Cause Known
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.
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.
- relates to
-
JDK-8162598 XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
-
- Resolved
-