File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes XSL transformation in a servlet error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSL transformation in a servlet error" Watch "XSL transformation in a servlet error" New topic
Author

XSL transformation in a servlet error

John Lindo
Greenhorn

Joined: Jun 19, 2005
Posts: 5
Ok so this may be simple but I need some help. I'm new to Java/XML. I'm working on a servlet that simply reads in an xml file and applies the transformation. The error that I'm getting is as follows

javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: java.lang.ClassCastException: org.apache.xpath.functions.FuncQname

the code is as below

// Output goes in the response stream.
PrintWriter out = response.getWriter();
try
{

String ctx = getServletContext().getRealPath("") + FS;
String DOC_XSL = "birds.xsl";
String DOC_XML = "birds.xml";

TransformerFactory tFactory = TransformerFactory.newInstance();


if(tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE)){

DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder db = dFactory.newDocumentBuilder();

Document xslDoc = db.parse(DOC_XSL);
Document xmlDoc = db.parse(DOC_XML);

DOMSource xslDomSource = new DOMSource(xslDoc);
xslDomSource.setSystemId("birds.xsl");
DOMSource xmlDomSource = new DOMSource(xmlDoc);
xmlDomSource.setSystemId(DOC_XML);

out.println(ctx+"<br>1<br>");
//error here
Transformer transformer = tFactory.newTransformer(xslDomSource);
out.println("after transformer creation");
transformer.transform(xmlDomSource, new StreamResult(out));
}
out.println("step4");

}
catch(javax.xml.transform.TransformerConfigurationException tce){
out.println(tce.getMessage());
out.println(tce.getCause());
}

catch (Exception e)
{
out.write(e.getMessage());
e.printStackTrace(out);
}
out.close();
}



Basically. The servlet is bombing where I've marked //error here. If anyone has an idea please help.

Thanks
John Lindo
Greenhorn

Joined: Jun 19, 2005
Posts: 5
Some further digging reveals that Dom isn't supported by my setup of Tomcat(4.0.6). However I have Xalan and xerces 2.6.0 installed and included in the classpath when I compile.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Note that Tomcat does NOT use your CLASSPATH when running so there are plenty of possible library mixups. The Java standard library has included plenty of XML capability since 1.4 - are you sure you need those other libraries? Tomcat does use XML for reading configuration files so I don't understand your "Dom isn't supported" comment.

If you have to have them, see the Tomcat docs - class-loader-howto.html for where to put the Jar files so that Tomcat can find them. That file should be part of your normal Tomcat installation.
Bill
John Lindo
Greenhorn

Joined: Jun 19, 2005
Posts: 5
OK. Checked out Tomcat 4 's Class Loader How-to. I also punted to using Java131. I got no love from either changes. We placed the latested xalan jar files in the WEB-INF/lib directory for the application and in the $CATALINA_BASE/shared/lib directory. Nothing has worked. Any other ideas? Was there anything wrong with the code that I posted that could be causing this issue?

Thanks,
John
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5

If that is the error you are still getting, mismatched libraries has to be the cause. Is that version of Xalan supposed to work with Java131?
There is a lot of other odd looking code too - it looks like you are using relative file names such as "birds.xsl" that depend on the "current directory" to work - you have no control over the current directory in a servlet environment.
If this was my problem I would put the entire transform code in a separate class that could be tested outside the servlet environment first.
Bill
John Lindo
Greenhorn

Joined: Jun 19, 2005
Posts: 5
Tried separating code into a separate class. (after removing all of the servlet syntax) the code compiles and works.

And after many attempts at deleting jar files and adding them back I have proven you correct. I eliminated all of the xerces and xalan jar files except those in the WEB-INF/lib directory and everything works.

Thank you very much for your assistance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSL transformation in a servlet error
 
Similar Threads
getAssociatedStylesheets failed
Servlet + JDOM
xml transform code just not behaving
xml transform just not behaving!
how to get information from a XML file?