XSLT transform error in java


I am getting the following exception, any info on when and why it occurs will be great. The exception is

"javax.xml.transform.TransformerException: Did not find the stylesheet root!"

The code that I am using is;

    private String generateEmail(boolean addcommid,
            UserTypeLocalValue recipient, InputStream stream, UserTypeLocalValue sender,
            String subject, Map properties) throws Exception {


        CommunicationTypeLocalValue initEmailData = new CommunicationTypeLocalValue();


            CommunicationTypeLocal ctl;
                  ctl = CommunicationTypeUtil.getLocalHome().create(initEmailData);
            catch (Exception exception)
                  exception.printStackTrace ( ) ;
                  throw new Exception ( exception.getMessage ( ) ) ;

        CommunicationTypeLocalValue emailData = ctl.getCommunicationTypeLocalValue();


        emailData.setCreationDate(new Date());




        log.info("generateEmail:" + emailData.getCommunicationId());

        if ( addcommid ) {
            properties.put("communication", emailData.getPrimaryKey().toString());

        XsltResult result = xslTransformer.transform(stream, properties);

//            Was removed because the text extraction was faulty and not needed
//      emailData.setTextMessage(result.getTextResult());

//            The folowing is for test purposes only when the getHtmlResult is failing
//            emailData.setHtmlMessage("<html><header></header><body><h1>HTML Content</h1></body></html>");



        log.debug ( ctl.getCommunicationTypeLocalValue().getPrimaryKey() ) ;

log.info ( "generateEmail: 19" ) ;
        return ctl.getCommunicationTypeLocalValue().getPrimaryKey();

Appreciate your help.

Who is Participating?
Leo EikelmanDirector, IT and Business DevelopmentCommented:
glad you solved it

>>XsltResult result = xslTransformer.transform(stream, properties);

What class is 'xslTransformer'?
M_SYEDAuthor Commented:

Its of type,

The code of this class is;
 * Created on Mar 28, 2005
package com.blueprintny.ecpportal.xslt;

import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

 * Transforms XSL files.
 * @author mparrish
public class XslTransformer {
    private static Logger log = Logger.getLogger(XslTransformer.class);
    public XsltResult transform(InputStream stream, Map properties) throws Exception {
        StreamSource source = new StreamSource(stream);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer(source);


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><blank/>")));


        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);

        StringWriter sw2 = new StringWriter();
        Result result2 = new StreamResult(sw2);


        Source source2 = new DOMSource(document.getDocumentElement());

        Iterator propertiesIter = properties.entrySet().iterator();
        while (propertiesIter.hasNext()) {
            Map.Entry entry = (Entry) propertiesIter.next();
            trans.setParameter((String) entry.getKey(), entry.getValue());

        trans.setOutputProperty(OutputKeys.METHOD, "html");

        log.debug("starting transform");
        trans.transform(source2, result);

        trans.setOutputProperty(OutputKeys.METHOD, "text");
        trans.transform(source2, result2);

        XsltResult results = new XsltResult(sw2.toString(), sw.toString());
        return results;
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Leo EikelmanDirector, IT and Business DevelopmentCommented:
I've seen this happen sometimes if you try to declare two root nodes in your style sheet.

Basically, you have a null style sheet root (I'm sure you already knew this).  It might of not been initialized for some reason.

What line of code causes this problem?
M_SYEDAuthor Commented:

Let me check.
I will get back to you when I narrow the bug down a bit........... :- (
Try validating your stylesheet. Try

M_SYEDAuthor Commented:
Hi Experts,

Am back with more details; The piece of code is
public class XslTransformer {
    private static Logger log = Logger.getLogger(XslTransformer.class);
    public XsltResult transform(InputStream stream, Map properties) throws Exception {
        StreamSource source = new StreamSource(stream);
System.out.println("Inside XslTransformer"); // First SYS OUT

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer(source);
System.out.println(" Transformer Object formed"); // Second SYS OUT

Now the interesting thing is;
Everything works the first time around and the email is sent.
But when its executed the second time the Exception comes up. The first sys out is printed but the second sys out is not printed instead I get the javax.xml.transform.TransformerException: Did not find the style sheet root

Is there some kind of file handle or obect lock that has to be released.

PLS help .............................
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Are you releasing all of your resources?

You must not be releasing the StreamSource.

check that it is properly released

M_SYEDAuthor Commented:
None of the resources for this class are being released. I have posted the whole code in a prior posting.

I think that I will set the Transformer, TransformerFactory objects to null before returning.......

Lets see.......
Why should a file that is only being read be locked?
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Well it could be locked by the class using the source and not releasing it.  But even if this way happening, I think it would release it automatically after the first run was complete.  

M_SYEDAuthor Commented:
Hello Experts,

I have solved this problem.
Basicaly the issue was that the "Stream" being used as the base XSLT was initialized outside of the loop, which is perfectly normal.
For the first execution the method call picks up the stream correctly but for subsequent executions it fails to pick it up somehow......... ?
So I have initialized the "Stream" in the loop and everything seems to be working now......... : - )

Dont know why this behaviour is occuring though....................
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.