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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>>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;
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?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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....................
Leo EikelmanDirector, IT and Business DevelopmentCommented:
glad you solved it


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.