XML Stylesheet for Transformation (XSLT)

XSLT is a very flexible technique used to transform an XML document into a different format, such as XSL-FO, HTML, WML, PDF, or any format you choose, including an XML document conforming to another DTD or XML schema.

XSLT defines a stylesheet that can be applied to an XML document to transform the XML data into another format. The most common use of XSLT, at the moment, is to transform XML into HTML for display by a Web browser.

An XSLT stylesheet defines rules that will transform the XML data into the new format. The rules are driven by pattern matching XML elements and attributes in the original XML document. A stylesheet transforms any XML document independently from any DTD or schema to which the document may conform. However, the writer of the stylesheet must be aware of the source document's structure to ensure that the stylesheet achieves the desired result.

An XSLT stylesheet is a well-formed XML document in its own right and conforms to a standard defined by the W3C. Listing 17.2 shows the smallest valid stylesheet called simple.xsl (conventionally, stylesheets are stored in files with a .xsl suffix).

Listing 17.2. Full Text of simple.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>

Although the example stylesheet in Listing 17.2 apparently does nothing, it, in fact, transforms an XML document by removing the XML tags and outputting the remaining text. While this doesn't seem a very useful way of presenting data to a client, it is helpful for understanding the different ways of applying stylesheets to documents.

Applying Stylesheets

There are three ways of applying stylesheets when delivering data to a client such as a Web browser:

  • The server uses the stylesheet to transform the XML document into a local file, and the file is sent to the client instead of the original document.

  • The stylesheet is sent to the client and then the client transforms the specified XML document for display to the user.

  • The server uses the stylesheet to transform the document each time it is requested to a presentation language, such as HTML or WML, and then presents the transformed document to the client.

All three approaches in theory use the same XML data and stylesheet; they differ in where and when the transformation takes place.

Storing Transformed Documents on the Server

Storing the transformed data on the server and presenting this to the client is a good technique when the document changes infrequently and many clients will use the same document. This approach reduces the processing requirements because a new HTML document is only generated when the original data changes.

The downside to this approach is that at least two copies of the data must be retained—the original XML data and the formatted HTML data. There may be several copies of the data formatted for each possible client device. Keeping the formatted documents synchronized with original data can become an administrative nightmare. This increased complexity also introduces additional potential for subtle and hard to identify bugs in the application. This approach is impractical for large enterprise applications.

Presenting XML Documents and Stylesheets to the Client

Sending the stylesheet and the XML document to the client requires an XML/XSLT-aware client. Given that most clients are Web browsers, this means adding XSLT capabilities to the Web browser.

XSLT support is available in many browsers, such as the latest versions of Netscape, Internet Explorer, Mozilla, Opera, and many others. Sadly, not all implementations conform to the W3C standard. If you can ensure that your customers will use a W3C-conformant browser, presenting XML and a stylesheet to the client is a viable approach. Alternatively, you will have to potentially support a different XLST stylesheet for each type of browser. Again this can lead to administrative complications in ensuring each separate XSLT stylesheet is synchronized with the other XSLT stylesheets for a given XML document.

In practice, a web application supporting multiple browsers (especially older, non-XSLT versions) will usually transform the XML documents on the server. The rest of today's work with XSLT will concentrate on applying XSLT transformations on the server.

Transforming the XML Document on the Server

A simple approach to transforming XML for presentation by the client is to do the transformation on the server when the client requests the data. The formatted output is sent to the requesting client. Subsequent requests from the same or different clients must reformat the XML source. This approach ensures that the server retains control of the XML data and only presents the client with appropriate data. Because the client is just a simple display device, there is no requirement for the client to support XML or XSLT. The drawback is that the server must devote processing time to transforming the data.

Today's work will concentrate on applying XML transformations from within a Java servlet using the J2SE 1.4 XSLT implementation. If you are using an older version of J2SE, you should download the Java implementation of Apache Xalan from http://xml.apache.org/xalan-j. The discussion of the javax.xml.Transform classes is the same for both implementations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.247.81