Using XSL Processors

To use an XSL stylesheet, we need a way to associate the stylesheet with a document. The W3C provides a single method for this—the xml:stylesheet processing instruction. As we shall see shortly, xml:stylesheet allows you to embed into your XML document the URI of its associated stylesheet. However, especially in the case of Java servlets, ASP, and similar technologies, we also would like other mechanisms for associating a stylesheet with a document. XT, LotusXSL, MSIE 5.0, and other XSL-enabled applications allow for specifically associating, at runtime, a stylesheet with a given XML document. Each has its own benefits and drawbacks. Let's look at the standard first—embedding a reference to a stylesheet within an XML document.

Using xml:stylesheet

The W3C defines the xml:stylesheet processing instruction to associate a stylesheet with an XML document. Many people are familiar with HTML and under normal circumstances a stylesheet, for example a CSS stylesheet, could be associated with a document as follows:

<link href="somestylesheet.css" rel="stylesheet" type="text/css"?>

The equivalent xml:stylesheet processing instruction would be as follows:

<?xml:stylesheet href="somestylesheet.css" type="text/css">

Note that under normal circumstances, the xsl:stylesheet processing instruction would most likely be more of the flavor

<?xml:stylesheet href="ImaXslsheet.xsl" type="text/xsl"?>

xml:stylesheet supports a syntax identical to HTML's <LINK.rel="stylesheet"..>, but uses the following attributes:

  • href="..."—The URI to the stylesheet, required.

  • type="..."—The type of the stylesheet, text/xsl for an XSL stylesheet, required.

  • title="..."—The title of the stylesheet, implied. Titles are used to group multiple xml:stylesheets together to define a single style.

  • charset="cdata"—The characterset of the stylesheet, implied.

  • media="..."—The intended media, for example print,screen and so on. Default is all. Normally stylesheets are designed for use in printing or displaying a document to the screen. However, there's nothing to stop you from rendering a document in braille or some other way.

  • alternate="yes|no"—Is this an alternate stylesheet? Default is no. This is an additional attribute for xml:stylesheet's alternate=.... Alternate is designed so that you can associate multiple stylesheets with a given XML document, each with its own purpose in mind.

<?xml:stylesheet alternate="yes" title="mini" href="styles/mini.xsl" type="text/xsl"?>
<?xml:stylesheet alternate="yes" title="bigger" href="styles/bigger.xsl" type="text/xsl"?>
<?xml:stylesheet alternate="yes" title="huge" href="styles/huge.xsl" type="text/xsl"?>

To associate an XSL stylesheet, say dogs.xsl, with an XML document, say allDogBreeds.xml, we might write the following:

<!-- allDogBreeds.xml -->
<xml version="1.0"?>
<!DOCTYPE dogbreeds [...]>
<?xml:stylesheet type="text/xml" href="dogs.xsl"?>
<!-- Remainder of XML document...-->
</dogbreeds>

You Can Use CSS with XML

While we will not discuss CSS at all (well almost not at all), you can use a CSS stylesheet with an XML document. The regular nature of XML lends itself well to CSS. Simply specify the CSS stylesheet as just shown and you're on your way! As always, caveat emptor. The buyer beware, your mileage may vary!


Running XT

XT is an implementation of the XSL transforms portion of the XSL specification. XT was developed by James Clark and, as of this writing, can be obtained from http://www.jclark.com. For simplicity, a version of XT is available on the CD-ROM. Two versions of XT exist. One is specific to Win32 and is used in most of the examples in this chapter. The other can be used via command line with any of the more recent Java SDKs. To use XT, simply follow the installation instructions. For the Win32 version, all you need to do is be sure that XT is in your PATH. Note that for the Win32 version of XT you need to be running Internet Explorer version 4.01 or later. Actually, what you really need is the Microsoft Virtual machine, but it ships with IE and so it's easier to just get the whole kit and install it.

The XT command line is as follows:

c:xt inputdoc.xml stylesheet

where inputdoc is some XML document and xml stylesheet is a stylesheet to associate with it. For example

C:Xt catalog.xml catalog-simple.xsl

results in

[heading]
This is the catalog header only one instance of this guy

. . .
This is the catalog trailer only one instance of this guy as well
[trailer]

when executed using the files in the Chapter 5 directory of the CD-ROM.

Running XT using Java is fairly simple as well, although the command line is longer. Assuming xt.jar and sax.jar are in your CLASSPATH, you simply type

c:java -Dcom.jclark.xsl.sax.parser=com.jclark.xml.sax.CommentDriver com.jclark.xsl.sax
.Driver catalog.xml  catalog-simple.xsl catalog.output.

In general the command is

c:java -Dcom.jclark.xsl.sax.parser=com.jclark.xml.sax.CommentDriver com.jclark.xsl.sax
.Driver Input_file.xsl somestylesheet.xsl outputfile

Note that other XSL processors do exist and, at least in a perfect world, would work similarly, albeit with different command-line syntax. LotusXSL, available on the CD-ROM, is another such XSL processor.

Running LotusXSL

LotusXSL is another of a large crop of XSL applications that implement the transform portion of the XSL specification (XSLT). As I was developing this chapter, LotusXSL was at version 0.18 and supported the most recent draft of XSLT. It's almost a certainty that by the time you are reading this a newer version will be available from http://www.alphaworks.ibm.com. LotusXSL was written completely in Java and can be used in servlets, from the command line, or as part of other Java applications.

LotusXSL is run in a fashion similar to XT. You simply specify the starting class as well as the input and output parameters.

Typically your command line will look something like the following:

c:java com.lotus.xsl.xml4j.ProcessXSL –IN xmlfile –XSL xsl-stylesheet –OUT filename

In addition, a number of other command-line switches exist for manipulating LotusXSL functions. See the LotusXSL software documentation for a complete list.

Buyer Beware!

LotusXSL, while not trivial, was fairly straightforward to configure and use. Several additions were required to the CLASSPATH, as well as having the Java 1.2 SDK or later correctly installed. The installation documentation describes the process in depth and was invaluable when I began experimenting with the LotusXSL tool. However, rather than giving any detailed installation and functionality descriptions, I direct the savvy developer—after all, you'd have bought one of the other early, mostly outdated, or just wrong XML books and not this one if you were not a savvy developer—to the IBM Web site for the most up-to-date information, source, and documentation for LotusXSL. As with all developing standards, your mileage may vary!


XML/XSL with Microsoft Internet Explorer 5.0

Microsoft was not to be forgotten and worked hard to get an XML/XSL-enabled product to market. Microsoft Internet Explorer 5.0 goes a long way toward implementing support for XML and XSL. However, as with any bleeding-edge product, MSIE 5.0 shipped before the standards had solidified and, unfortunately, much has changed. In addition, MSIE 5.0 added to XML in many non-standard ways. XML Island discussions as well as other MS proprietary extensions have been debated for what seems like ages on the XML newsgroups. Unfortunately, as with any early release, much of what Microsoft did does not match the current W3C recommendations. Microsoft will most certainly move closer to the XML and XSL standards as time progresses. However, MSIE 5.0 was too far afield to be of much use during this specific chapter's development. Some of the most common differences between MSIE 5.0 and the W3C standards were

  • Namespaces— MSIE 5.0 supports an old, out-of-date namespace and flags an error when the correct, current, XSL namespace is specified. After the error, all processing stops.

  • Default rules— MSIE 5.0 did not support any of the default XSL rules requiring developers to do much more work to create useful XSL stylesheets.

  • Expression Syntax— Most of the power of XSL is in its expression syntax, much of which was omitted from MSIE 5.0.

Other less important differences exist as well, many of which may have been addressed by the time this book comes to print.

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

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