Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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 we've seen so far in this chapter, if you want to perform XSLT transformations on a Web server, you'll 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—here's 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, you've seen how to perform XSLT transformations using standalone XSLT processors in the Internet Explorer browser and on Web servers. However, the only transformation we've done so far is to transform XML into HTML. Although that's the most popular transformation, XMLto XML transformations are becoming increasingly popular.

XML-to-XML Transformations

XML-to-XML transformations are sometimes thought of as SQL for the Internet, because they enable you to use what amount to database querieson XML documents. Here's an example of what I mean. The planets.xmlfile we've been using has a lot of data about each planet, as you see here:

<?xml version="1.0"?>
<PLANETS>
  <PLANET>
    <NAME>Mercury</NAME>
    <MASS UNITS="(Earth = 1)">.0553</MASS>
    <DAY UNITS="days">58.65</DAY>
    <RADIUS UNITS="miles">1516</RADIUS>
    <DENSITY UNITS="(Earth = 1)">.983</DENSITY>
    <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->
  </PLANET>

  <PLANET>
    <NAME>Venus</NAME>
    <MASS UNITS="(Earth = 1)">.815</MASS>
    <DAY UNITS="days">116.75</DAY>
    <RADIUS UNITS="miles">3716</RADIUS>
    <DENSITY UNITS="(Earth = 1)">.943</DENSITY>
    <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->
  </PLANET>
    .
    .
    .

What if you just want a subset of that data, such as the name and mass of each planet? In database terms, planets.xml represents a table of data, and you want to create a new table holding just a subset of that data. That's what SQL can do in databases, and that's what XSLT can do with XML documents.

Here's a new version of planets.xsl that will perform the transformation we want, selecting only the name and mass of each planet, and sending that data to the output document. Note in particular that we're performing an XML-to-XML transformation, so I'm using the <xsl:output> element with the method attribute set to "xml" (in fact, the default output type is usually XML, but if an XSLT processor sees a <html> tag, it usually defaults to HTML):

Listing 1.6 Selecting Name and Mass Only

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

  <xsl:template match="PLANETS">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="PLANET">
    <xsl:copy>
     <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="MASS">
    <xsl:copy>
    <xsl:value-of select="."/>
    <xsl:value-of select="@UNITS"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="RADIUS">
  </xsl:template>

  <xsl:template match="DAY">
  </xsl:template>

  <xsl:template match="DENSITY">
  </xsl:template>

  <xsl:template match="DISTANCE">
  </xsl:template>

</xsl:stylesheet>

I'll apply this new version of planets.xsl to planets.xml using Xalan to create a new XML document, new.xml:

C:\planets>java org.apache.xalan.xslt.Process -IN planets.xml -XSL planets.xsl -OUT new.xml

Here's what the resulting XML document, new.xml, looks like:

<?xml version="1.0" encoding="UTF-8"?>

<PLANET>
  <NAME>Mercury</NAME>
  <MASS>.0553(Earth = 1)</MASS>
</PLANET>
<PLANET>
  <NAME>Venus</NAME>
  <MASS>.815(Earth = 1)</MASS>
</PLANET>
<PLANET>
  <NAME>Earth</NAME>
  <MASS>1(Earth = 1)</MASS>
</PLANET>

Note that this looks much like the original planets.xml, except that each <PLANET> element contains only <NAME> and <MASS> elements. In this way, we've been able to get a subset of the data in the original XML document.

You can make any number of other types of XML-to-XML transformations, of course. You can process the data in an XML document to create entirely new XML documents. For example, you can take an XML document full of student names and scores and create a new document that shows average scores. XSLT supports many built-in functions that enable you to work with data in this way, and you'll see those functions in Chapter 8.

In addition, many programs use XML to exchange data online, and they usually format their XML documents differently, so another popular use of XML-to-XML transformations on the Internet is to transform XML from the format used by one program to that used by another.

  • + Share This
  • 🔖 Save To Your Account