You can also perform XSLT transformations on a Web server so that an XML document is transformed before the Web server sends it to a browser. The most common transformation here is to transform an XML document to HTML, but XML-to-XML transformations on the server are becoming more and more common.
Unlike the other XSLT transformations weve seen so far in this chapter, if you want to perform XSLT transformations on a Web server, youll usually need to do some programming. There are three common ways to perform XSLT transformations on Web servers: using Java servlets, Java Server Pages (JSP), and Active Server Pages (ASP). Chapter 10 explores all three in greater detail. Some XSLT processors can be set up to be used on Web servers heres a starter list:
AXSL www.javalobby.org/axsl.html. AXSL is a server-side tool that converts XML to HTML using XSLT.
Microsoft XML Parser http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp. MSXML3 provides server-safe HTTP access for use with ASP.
mod_xslt http://modxslt.userworld.com. A simple Apache Web server module that uses XSLT to deliver XML-based content. Uses the Sablotron processor to do the XSLT processing.
PXSLServlet www.pault.com/Pxsl. This servlet can be used to convert XML to HTML with XSLT. It also enables you to read from and write to a SQL database (JDBC).
xesalt www.inlogix.de/products.html. This XSLT processor is available as a module for both Apache and IIS Web servers.
XML Enabler www.alphaworks.ibm.com/tech/xmlenabler. The XML Enabler enables you to send requests to a servlet and when the servlet responds, the XML Enabler can format the data using different XSLT stylesheets.
XT can be used as a Java servlet. It requires a servlet engine that implements at least version 2.1 of the Java Servlet API. The Java servlet class is com.jclark.xsl.sax.XSLServlet.
The following example shows JSP used to invoke Xalan on the Web server. Xalan converts planets.xml to planets.html, using the planets.xsl stylesheet. The code then reads in planets.html and sends it back to the browser from the Web server:
<%@ page errorPage="error.jsp" language="java" contentType="text/html" import="org.apache.xalan.xslt.*;java.io.*" %> <% try { XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); processor.process(new XSLTInputSource("planets.xml"), new XSLTInputSource("planets.xsl"), new XSLTResultTarget("planets.html")); } catch(Exception e) {} FileReader filereader = new FileReader("planets.html"); BufferedReader bufferedreader = new BufferedReader(filereader); String instring; while((instring = bufferedreader.readLine()) != null) { %> <%= instring %> <% } filereader.close(); %>
You can see the results in Figure 1.4, which shows planets.html as sent to the Internet Explorer from a Web server running JSP. Chapter 10 provides more information about using Java servlets, JSP, and ASP for server-side XSLT transformations.
Up to this point, youve seen how to perform XSLT transformations using standalone XSLT processors in the Internet Explorer browser and on Web servers. However, the only transformation weve done so far is to transform XML into HTML. Although thats the most popular transformation, XML to XML transformations are becoming increasingly popular.