Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XSL gives your XML some style

Use XSL and servlets to style your XML data

  • Print
  • Feedback

Page 4 of 7

The void reset() method, to be used after process(), resets the processor to its original state.

The process() method is overloaded 18 times. Each signature provides a different way to process your XML and XSL. Some return an org.w3c.dom.Document object. I have found that the above process() method is the handiest; the documentation recommends its use because of the XSLTInputSource (used to read in XML or XSL) and XSLTResultTarget (used to write out the results) classes, which we examine next in turn.

Class com.lotus.xsl.XSLInputSource

The XSLInputSource class can be created by using any of the following constructors:

  • XSLTInputSource(): Zero-argument default constructor
  • XSLTInputSource(org.xml.sax.InputSource isource): Creates a new XSLTInputSource source from a SAX input source
  • XSLTInputSource(java.io.InputStream byteStream): Creates a new input source with a byte stream
  • XSLTInputSource(org.w3c.dom.Node node): Creates a new input source with a DOM node
  • XSLTInputSource(java.io.Reader characterStream): Creates a new input source with a character stream
  • XSLTInputSource(java.lang.String systemId): Creates a new input source with a system identifier


Class com.lotus.xsl.XSLResultTarget

The XSLResultTarget class can be created by using any of the following constructors:

  • XSLTResultTarget(): Zero-argument default constructor
  • XSLTResultTarget(org.xml.sax.DocumentHandler handler): Creates a new output target with a SAX Document handler, which will handle result events
  • XSLTResultTarget(org.w3c.dom.Node n): Creates a new output target with a character stream
  • XSLTResultTarget(java.io.OutputStream byteStream): Creates a new output target with a byte stream
  • XSLTResultTarget(java.lang.String fileName): Creates a new output target with a file name
  • XSLTResultTarget(java.io.Writer characterStream): Creates a new output target with a character stream


The API also includes classes that enable you to listen for events -- problems or document events, for example -- during processing.

Put it all together

Now that we have a basic overview of the XSL API and XSL stylesheets, let's look at a problem that those technologies could help us solve.

In this example, we've got a server-side process that uses XML to store transaction data. There is a business need for the ability to analyze the day's transactions. The solution should be as unobtrusive as possible, leaving the company's current process and data storage procedures unchanged.

Here are some of the possible solutions using XSL:

  1. Create a periodically running program on the server that will use XSL to transform XML into HTML. The resulting HTML files will be stored on a file server where they can be accessed by a rowser.
  2. If you have a controlled set of users, make sure they have browsers that support XSL. In that case, your XML will include a header denoting which XSL document to use, allowing the browser to perform the translation for you.
  3. Use XSL in a server-side medium such as servlets. Pull the XML from wherever it may be in a file, a database, or a message queue, then style the XML using XSL.


We'll use approach three, but the first and second approaches are both valid. Approach one would solve any performance problems, but it doesn't provide the dynamic HTML creation that will make our application special. Approach two will be a real possibility soon -- Internet Explorer 5 can do XSL translations, and Netscape has announced that its next release will, too. However, your users won't all have the newest browsers, and we still have to do server-side processing to pull the XML data.

  • Print
  • Feedback

Resources