Sushant,

It looks like what's happening is you're confusing the root of the document (in xpath terms) with the root element (in XML spec terms), which is a common problem. Your template only matches the "/", which is the root of the document, which is NOT the same at the <html> root element, confusingly. Think of the Document object as being a wrapper around the actual XML document. The children of Document include the root of the XML input, <html>, and can also include comment and processing instruction nodes.

See http://www.dpawson.co.uk/xsl/sect2/root.html for a nice discussion.

In your #1 transform, you are sending the Document itself as input to the XSLT, which successfully matches "/", and does everything you expect. In your #2 transform, you are actually sending the <html> child of the Document as the input to the XSLT, and that no longer matches your "/" template. Since no explicit templates in your XSLT match the <html> element, the default XSLT templates kick in, which essentially print out text values of children elements.

I am not sure why you get the correct results if you don't specify XALAN however.

Nathan

Sushant Sinha wrote:
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.



--
Nathan Nadeau
n...@gleim.com
Software Development
Gleim Publications, Inc.
http://www.gleim.com

Reply via email to