Transform self-define namespace XML with DOM, can't make value in the result Document

Discussion in 'Software' started by abnerliu, Aug 4, 2011.

  1. abnerliu

    abnerliu

    Joined:
    Aug 4, 2011
    Messages:
    1
    Likes Received:
    0
    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?
     
    abnerliu, Aug 4, 2011
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.