Server-side Transformations
Now that you've got your tools installed, it's time to take a look at actually performing the transformation. Eventually, we'll allow the user to choose the style sheet, but for now we'll perform a transformation similar to the one we built in Part 2. This time, however, we'll do it on the server side.
In general, transformations on the server side involve the following steps:
Determine the XML source.
Determine the XSL source.
Create the processor using the style sheet to determine its actions.
Set the destination.
Perform the transformation.
First, we'll look at this transformation in an Active Server Page and then we'll look at it again in a servlet.
Transformation Using ASP
To begin, create a new file, and save it as main_basic.asp in the Web server's main directory. Transformation using ASP is done as follows:
<%@ language = "VBSCRIPT" %> <% 'Set source files sourceFile = Server.MapPath("content.xml") styleFile = Server.MapPath("style1.xsl") 'Create DOM Documents set source = Server.CreateObject("Msxml2.FreeThreadedDOMDocument") source.load(sourceFile) set style = Server.CreateObject("Msxml2.FreeThreadedDOMDocument") style.load(styleFile) 'Create the XSL Processor set xslTemplate = createObject("MSXML2.XSLTemplate") xslTemplate.stylesheet = style set processor = xslTemplate.createProcessor 'Set the source and destination processor.input=source processor.output=Response 'Perform the transformation processor.transform %>
First, determine the actual locations of the source files. You can specify them directly, or use Server.MapPath(), as shown. From there, create a Document object for each, and populate them with the content of the files using the load() method.
It is the processor, which is created using the style sheet as a template, which actually performs the transformation. Because of the way in which it was created, the processor already has the information contained in the style sheet. All it needs to know to perform the transformation is what to transform and where to send it.
Here the processor object processor takes as its input the source document object, and takes as its output the Response object. This way, when the processor performs the transformation, it sends the results directly to the browser.
Performing the transformation itself is as simple as calling the processor's transform method.
Transformation Using Java
Performing the transformation using a Java servlet is essentially the same process. (This class should be saved as main_basic.java in the webapps/examples/web-inf/classes directory (or the location of your choice), and compiled to main_basic.class.)
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; public class main_basic extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); try { //Set source files String XMLFileName = "http://localhost/content.xml"; StreamSource source = new StreamSource(XMLFileName); String XSLFileName = "http://localhost/style1.xsl"; StreamSource style = new StreamSource(XSLFileName); //Designate that the result goes to the browser; //StreamResult takes a Writer object. StreamResult result = new StreamResult(out); //Create the XSL processor TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(style); //Transform the document transformer.transform(source, result); } catch (Exception e) { out.print(e.getMessage()); e.printStackTrace(); } } }
In most Java servlets, the work is done in the doGet() method. (An exception to this rule-of-thumb is form action servlets, as we'll see in a later section.)
The doGet() method starts by using the HttpServletResponse object, response, to send the Content-Type header "text/html" to the browser. This tells the browser to expect an HTML page, rather than an image or other content. It then creates a PrinterWriter, out, which we use to send content to the browser in much the same way we use System.out in a traditional Java application.
From there, we begin the actual transformation. First, we create Source (or more specifically, StreamSource) objects out of the content and style sheet. We want to send the results to the browser, so we'll also create a Result (or StreamResult) object that points to out.
Next, we create the transformer factory, and use it to create a new transformer (similar to the preceding processor) using the style sheet.
Finally, transform the document. The transformer object sends the final document to out, which in turn sends it on to the browser.
The Result
The end result is HTML that is sent directly to the browser, as shown in Figure 1. To see this for yourself, access http://localhost/main_basic.asp or http://localhost/examples/servlet/main_basic, depending on your implementation.
Because the result is straight HTML, the browser can be completely ignorant of XML. The file main_basic.html contains the resulting code.
Figure 1 The result of the transformation is plain HTML.