Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

missing namespaces after transform

843834Apr 9 2008 — edited Apr 10 2008
I am using JDK 1.5 on windows (1.5.0_15), with the stock Transformer (com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl)

It seems that whenever I perform an XSLT transformation with a source, I lose all namespaces on the elements. With the same xsl file using MSXML, the namespaces are maintained.

The output of the following is:
Original : example:inner
ID xform : example:inner
Formatted: inner

I would have expected all three to be "example:inner"

---------- format.xsl -----------------
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>
<!-- Copy any processing instructions -->
<xsl:template match="processing-instruction()">
<xsl:copy/>
</xsl:template>
<!-- Then deep copy the document -->
<xsl:template match="*">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
------------ Test.java ----------------------

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Test {

final static String xmlStringWithNs = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<example:outermost xmlns:example=\"http://www.example.com\">" +
"<example:middle>" +
"<example:inner>hello</example:inner>" +
"</example:middle>" +
"</example:outermost>" ;

public static void main(String[] args) throws Exception {
Document original = getDocumentFromString(xmlStringWithNs);
System.out.println("Original : " + original.getDocumentElement().getFirstChild().getFirstChild().getNodeName());
Document unformatted = noFormat(original);
System.out.println("ID xform : " + unformatted.getDocumentElement().getFirstChild().getFirstChild().getNodeName());
Document formatted = format(original);
System.out.println("Formatted: " + formatted.getDocumentElement().getFirstChild().getFirstChild().getNodeName());
}


static Document getDocumentFromString(String xml) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
StringReader strReader = new StringReader(xml);
InputSource inputSource = new InputSource(strReader);
return documentBuilder.parse(inputSource);

}

static private Document noFormat(Document doc) throws Exception {
Document result = null;

try {
// No parameter - I believe it's called an identity transform
Transformer serializer = TransformerFactory.newInstance().newTransformer();
DOMResult domResult = new DOMResult();
serializer.transform(new DOMSource(doc), domResult);
result = (Document)domResult.getNode();
} catch (TransformerException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return result;
}

static private Document format(Document doc) throws Exception {
Document result = null;

try {
StreamSource xslSource = new StreamSource(new FileInputStream(new File("format.xsl")));
Transformer serializer = TransformerFactory.newInstance().newTransformer(xslSource);
DOMResult domResult = new DOMResult();
serializer.transform(new DOMSource(doc), domResult);
result = (Document)domResult.getNode();
} catch (TransformerException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return result;
}


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 8 2008
Added on Apr 9 2008
4 comments
435 views