Inside Xslt

XSLT Transformations on Web Servers

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:

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.

Figure 1.4. Transforming XML on the Web server.

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.

Категории