Extensible Stylesheet Transformations (XSLT)

XSLT is a very flexible technique that is 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.

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. The pattern matching approach enables a single stylesheet to be used with XML documents conforming to different DTDs, provided that there is a reasonable amount of commonality between the XML elements.

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>

In Listing 17.2, line 2 binds the namespace xsl to the namespace URI http://www.w3.org/1999/XSL/Transform within the context or the document's root element. Although the example stylesheet apparently does nothing, in fact, it 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 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 data and the formatted data. There often 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.

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 Netscape 6, Internet Explorer5, Mozilla, Opera, and many others. Sadly, the IE5 and IE6 implementation does not conform to the W3C standard. If you can ensure your customers will use a W3C-conformant browser, presenting XML and a stylesheet to the client is a viable approach. In practice, given the dominance of IE as a Web browser at the present time, portable J2EE applications must adopt a different solution.

Because there are no other widely-used client browsers that support XML transformations (as defined by the W3C), the rest of today's work with XSLT will concentrate on the third means of applying XSLT transformations—that is, on the server.

Transforming the XML Document on the Server

The most common 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 the 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.

There are many tools on the market for supporting XLST, some that are free for personal use and some that require commercial licenses. You will use the XALAN processor from the Apache open-source project for investigating the XSLT transformations discussed in today's chapter. XALAN implements the JAXP framework for including XSLT in your Java programs, and it supports a command-line processing interface for transforming XML documents outside of a programming environment.

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

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