Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
1 reply Latest Post - ‏2011-08-27T15:08:21Z by SystemAdmin
abnerup
abnerup
1 Post
ACCEPTED ANSWER

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="http://www.aa.com/aa">
<aa:people>
<aa:name>apple</aa:name>
<aa:sex>male</aa:sex>
</aa:people>
</aa:root>

the XSL I created as below:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aa="http://www.aa.com/aa">

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

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

</xsl:stylesheet>

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="http://www.aa.com/aa">
<hh>male</hh>
</root>

but, actually the result is:
<root xmlns:aa="http://www.aa.com/aa">
<hh/>
</root>

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
    SystemAdmin
    1138 Posts
    ACCEPTED ANSWER

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

    ‏2011-08-27T15:08:21Z  in response to abnerup
    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(
    
    true); 
    //etc...
    

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