You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by "Montrose, Brent A" <br...@eds.com> on 2002/01/16 20:41:12 UTC

Use of Xalan in a Servlets

I have a general architecture question regarding JAXP / Xalan usage in a
J2EE web app, specifically in a Servlet.

Our web app dynamically creates XML which is send to the browser (IE 5.5),
and the browser performs the translation based on the xml-stylesheet
processing instruction in the XML.  The XSL is contained in files on the web
server.  What we would like to do is perform the XSL transformation on the
web server utilizing JAXP, specifically Xalan.  Are there any
recommendations / best practices on which mechanism we should use to obtain
the XSL as a Source object?  Should the XSL be read in as File? URL?  What
about the use of the .getAssociatedStylesheet() method on the
TransformerFactory? Which is the most efficient way??? 
The dynamic XML is currently stored in a StringBuffer before being sent to
the browser, so I assume that the use of StreamSource is correct for the XML
Source.  Also, I plan on using a Templates stored in Collection as shown in
the Servlet examples.

I would appreciate any help?
Thanks!
Brent Montrose


Re: Use of Xalan in a Servlets

Posted by "Isaac S." <is...@netvision.net.il>.
> Our web app dynamically creates XML which is send to the browser (IE 5.5),
> and the browser performs the translation based on the xml-stylesheet
> processing instruction in the XML.  The XSL is contained in files on the
web
> server.  What we would like to do is perform the XSL transformation on the
> web server utilizing JAXP, specifically Xalan.  Are there any
> recommendations / best practices on which mechanism we should use to
obtain
> the XSL as a Source object?  Should the XSL be read in as File? URL?  What
> about the use of the .getAssociatedStylesheet() method on the
> TransformerFactory? Which is the most efficient way???

First off, I think you need to carefully examine the need of transforming
using XSL in the server-side
instead of the client-side. Why do it in the server if it can be done in the
client-side, assuming - as you
said - that the clients use an XML/XSL-aware browser (like IE)?
Maybe it'll be a better practice to leave the XSL transformation to be done
in the client-side.

Now, assuming you still want to do the XSL transformation in the
server-side:

In order to use JAXP for XSLT, you need to build a suitable transformer:

Transformer    t = TransformerFactory.newTransformer(source);

When "source" is a javax.xml.transform.Source implementation.

(Of course, if you're running a non-SingleThreadModel servlet, you should be
aware of JAXP's concurrency issues.
For example, the "Transformer" class is not guaranteed to be thread-safe,
etc)

Now, the question is how to construct the source: as a file? URL? or what?
Well, there's only one good solution
to this question: it depends on your needs.

If your servlet knows that it's going to deal with files that are not
neccesarily a part of the web-application's problem-context,
then constructing the Source object as an input stream from a "File" object
might be the best choice.

If your servlet is dealing with XSL files that are logically a part of the
web-application, then you might want to construct
the Source object based on an InputStream given to you by the
getServletContext().getResourceAsStream() method.
This method will expect to find the XSL resource within the
web-application's classpath.

If your servlet is dealing with XSL files that *may* be across the net, then
constructing a Source using a URL is a good choice
(consider a servlet that its job is to apply an XSL which is being taken
from an arbitrary location over the net).

In conclusion - as I said - it depends on the servlet's function and its
solution's space. Depends on what you want to achieve.

> The dynamic XML is currently stored in a StringBuffer before being sent to
> the browser, so I assume that the use of StreamSource is correct for the
XML
> Source.  Also, I plan on using a Templates stored in Collection as shown
in
> the Servlet examples.

Indeed, a StreamResult, constructed using a StringReader is suitable for
reading an XML document from a string:

Source mySource = new StreamSource(new StringReader(myString));

I'm not familiar with Templates, though.

Good luck!


    - Isaac