This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Invoking XSLT styling from a Java servlet
- From: "Roger L. Costello" <costello at mitre dot org>
- To: xsl-list at lists dot mulberrytech dot com
- Cc: "Costello,Roger L." <costello at mitre dot org>
- Date: Thu, 27 Jun 2002 15:22:41 -0400
- Subject: [xsl] Invoking XSLT styling from a Java servlet
- Organization: The MITRE Corporation
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Folks,
Below is Java code to invoke XALAN with in-memory strings, representing
the XML and Stylesheet respectively. I know that this code works. I
have a program which invokes this method and it works great.
However, when I use this method within a Java servlet it fails. I have
traced it down to this statement:
Transformer transformer = tFactory.newTransformer(xsl_ss);
The javadocs mention this for the newTransformer() method: "Care must be
given not to use this object in multiple threads running concurrently".
I am not running multiple threads in my servlet. So, I can't imagine
that that is the problem. I am really clueless as to what could be
going wrong. Any help would be much appreciated. [I am using Tomcat
4.0.3 as the Web server.] /Roger
// This code shows how to invoke xalan by passing to it the
// xml and xsl as in-memory strings
public static String doTransform(String xml, String xsl)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException {
TransformerFactory tFactory = TransformerFactory.newInstance();
StringReader xsl_sr = new StringReader(xsl);
StreamSource xsl_ss = new StreamSource(xsl_sr);
** the following statement is where it fails **
Transformer transformer = tFactory.newTransformer(xsl_ss);
StringReader xml_sr = new StringReader(xml);
StreamSource xml_ss = new StreamSource(xml_sr);
StringWriter out_sw = new StringWriter();
StreamResult out_sr = new StreamResult(out_sw);
transformer.transform(xml_ss, out_sr);
return out_sw.toString();
}
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list