I am trying to use xalan for XSLT transformation of XML files. However,
I see different outputs when I invoke transform with the input source as
DOMSource(org.w3.dom.Document doc)  than with the
DOMSource(doc.getDocumentElement()). The output of the one with Document
seems correct but the one with getDocumentElement omits any tags
specified in the XSL file. I would like to know why we have problem with
transform when we pass it the getDocumentElement()

Here is a detailed example:

XML FILE:
=========================================
<html>
    <head>
        <title> DOM </title>
    </head>
    <body> Learning Java Programming </body>
</html>


XSL FILE:
=========================================
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <xsl:template match="/">
         <item>
             <title><xsl:value-of select="html/head/title"/></title>
             <body><xsl:value-of select="html/body"/></body>
         </item>
    </xsl:template>
</xsl:stylesheet>

CODE
=========================================

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docbuilder = factory.newDocumentBuilder();

Document doc = docbuilder.parse(xmlfilepath);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new
StreamSource(xslfile));

DOMSource docSource1 = new DOMSource(doc);
DOMSource docSource2 = new DOMSource(doc.getDocumentElement());

#1. transformer.transform(docSource1, new StreamResult(System.out));
#2. transformer.transform(docSource2, new StreamResult(System.out));


With transform #1, we get 
=========================================
<?xml version="1.0" encoding="UTF-8"?><item><title> DOM </title><body>
Learning Java Programming </body></item>
=========================================

With transform #2, we get 
=========================================
<?xml version="1.0" encoding="UTF-8"?>
    
         DOM 
    
     Learning Java Programming 
=========================================

So as you can see transform #2 is missing all the XML tags specified in
the XSL file. It is important to note that when I run the above code
without specifying XALAN, I get the correct output. I am assuming that
Java is using an older version of Xalan which works correctly for
getDocumentElement()

If possible please explain why this is happening and if we can pass the
getDocumentElement to the transform function.

Thanks,
Sushant.


Reply via email to