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 6 of 7

Figure 2. Stylesheet 3: Output from the Detail view (48 KB)



Wrap it up

The servlet will extend the HTTPServlet Java class and employ the doPost() and doGet() methods.

The XSLInputSource gives us the ability to just hand the XSLProcessor a java.io.Reader object -- perfect since our XML and XSL will reside in flat files. If we had these in any other place, we could just use a StringReader. The XSLInputSource will also accept a org.w3c.dom.Node.

The XSLTResultTarget writes out to an OutputStream -- convenient, since we can get an OutputStream from our HTTPResponse.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.lotus.xsl.XSLTInputSource;
import com.lotus.xsl.XSLTResultTarget;
import com.lotus.xsl.XSLProcessor;
import org.xml.sax.SAXException;
public class XSLServlet extends HttpServlet {
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  //By redirecting our doGets to the doPost method we can have a stylesheet
  //executed from a link
  //ex. <a 
href="xsl?stylesheet=detail&invoiceNum=00002">00002
  public void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
    doPost(request, response);
  }
  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
    PrintWriter out = new PrintWriter (response.getOutputStream());
    //Get the stylesheet selected by the user
    String stylesheet = request.getParameter("stylesheet");
    //Get the invoice number selected by the user, this is used only for 
certain
    //stylesheets.  If it is not present, the stylesheet won't use it.
    String invoiceNum = request.getParameter("invoiceNum");
    //Prepare FileReaders for the stylesheet and XML
    FileReader xslReader = new 
FileReader(".\\javaworld\\xsl\\"+stylesheet+".xsl");
    FileReader xmlReader = new 
FileReader(".\\javaworld\\xml\\"+"invoice.xml");
    response.setContentType("text/html");
    try
    {
      //Create an instance of an XSLProcessor
      XSLProcessor proc = new XSLProcessor();
      //Set the invoice number parameter
      proc.setStylesheetParam("invoiceNum", invoiceNum);
      //Process the stylesheet, all the output will go straight
      // out to the browser.
      proc.process(new XSLTInputSource(xmlReader),new 
XSLTInputSource(xslReader),new XSLTResultTarget(out));
    }
    catch (SAXException saxE)
    {
      out.write(saxE.getMessage());
      saxE.printStackTrace(out);
    }
    out.close();
  }
  //Get Servlet information
  public String getServletInfo() {
    return "XSLServlet Information";
  }
}


And that's all there is to it!

Conclusion

You have seen how to extract XML as HTML, using XSL. But as I mentioned in the beginning of the article, XSL can be used to transform XML from one doc type to another -- XML to HTML, HTML to XML, HTML to HTML, XML to DSML (Directory Service Markup Language), and so on. XSL's most useful feature: it doesn't have to be compiled. The way the servlet example works, we could add stylesheets to our stylesheet folder, then add the stylesheet to our list of stylesheets in our HTML.

  • Print
  • Feedback

Resources