• 1 reply
  • Latest Post - ‏2011-08-27T15:08:21Z by SystemAdmin
1 Post

Pinned topic Transform self-define namespace XML with DOM, can't make value in the resul

‏2011-08-04T15:41:36Z |
Hi All
I have an issue when using DOM to transform an XML with name space. I create an XSL to transform the XML, but when I using DOM to transform it, I found the result document has no value.
the sample XML I used as below:
<?xml version="1.0" encoding="UTF-8"?>
<aa:root xmlns:aa="">

the XSL I created as below:
<?xml version="1.0"?>
xmlns:xsl="" version="1.0"

<xsl:template match="/">
<xsl:call-template name="test"/>

<xsl:template name="test">
<xsl:value-of select="/aa:root/aa:people/aa:sex/text()"/>


and Java DOM codes I came up with as below:
public void transformWithDOM() throws Exception{
TransformerFactory factory = TransformerFactory.newInstance();
Templates translet = factory.newTemplates(new StreamSource(xslFile));

DOMSource source = null;
Transformer transformer = null;
DOMResult resultXmlDoc = null;

Document srcXmlDoc = getDocObj(strxml);

source = new DOMSource(srcXmlDoc);
resultXmlDoc = new DOMResult();
transformer = translet.newTransformer();
transformer.transform(source, resultXmlDoc);

Document outputDoc = (Document) resultXmlDoc.getNode();

the expected result should be:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:aa="">

but, actually the result is:
<root xmlns:aa="">

can any one help to indicate, how should I do, can get the expected result?
Updated on 2011-08-27T15:08:21Z at 2011-08-27T15:08:21Z by SystemAdmin
  • SystemAdmin
    1138 Posts

    Re: Transform self-define namespace XML with DOM, can't make value in the resul

    I wonder why the getDocObj() method is hidden away from the question being asked. Without showing that, you can't expect any answer rationally deduced from the code, or can you?

    If getDocObj() return a Document object, then the most possible reason for the observation is that the DocumentBuilderFactory is built without setting up its namespace-awareness, which by default being namespace unaware. You have to add a line like this after the setup of DocumentBuilderFactory being proceding further in the getDocObj().
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(

    In fact resulting empty data has to be expected with that setup missing.