April 24, 2011, 8:17 a.m.
posted by un
A Bit of Housekeeping
The xsl:output Element
XLST is oriented toward transforming a source tree into a result tree. What, exactly, a processor does with the result tree is not within the scope of the XSLT Recommendation. However, XSLT does give the stylesheet designer a way to request that an XSLT processor creates output with certain characteristics. Such requests are made by using the xsl:output Element. It is optional, and although the Recommendation says that processors should use the information in this Element to produce the output, the Recommendation doesn't require that they do. However, the most commonly used processors do pay attention to the xsl:output Element.
The xsl:output Element has several optional Attributes, but we'll use only a few. The method Attribute allows users to request XML, HTML, text, or their own defined type of output. We'll use "xml" as the value. We'll also use the encoding Attribute to specify an encoding of UTF-8. Some processors produce UTF-16 as the default, but for ASCII-oriented systems (and people) we would generally prefer UTF-8. For readability in pasting example result documents into this book, I also used the indent Attribute with a value of "yes". You might not want to use it in production applications since it generates unneeded whitespace.
Running Transformations from a Command Line
You've probably asked by now, "OK, this is all well and good, but how do I actually run one of these transformations?" IDEs like XMLSPY or TurboXML provide menu options for performing XSLT transformations. However, some IDEs may require you to include in your source document a stylesheet processing instruction, such as the following.
<?xml-stylesheet type="text/xsl" href="ProspectToCustomer.xsl"?>
While this may be fine for an IDE, you wouldn't want to rely on this approach in most production situations. No matter how good a supplier you are, Ford and GM are unlikely to put your own stylesheet reference in the purchase orders they send you.
Unless you build XSLT transformations into your business application (which I'll discuss briefly in Chapter 12), you'll probably invoke them from a command line utility in a script or via some other utility. There are two such command line XSLT processors available for use with the APIs used in this book. The Java version is Xalan. We use the Process application defined in the xslt class as shown below.
The jar files for JAXP, Xerces, and Xalan need to be installed in the directory appropriate to your Java runtime environment. The command line version comes with the API libraries, so there's no special installation. There is also a C++ version of Xalan.
In the MSXML world, Microsoft offers a free download of a command line utility called msxsl. It is run like any other program from a command line prompt. Here's how we would run it for the same scenario.
The msxsl utility uses the MSXML API library, so you'll have to have it installed. It seems to me that Microsoft has a tendency to move things around on its Web site and make links to internal pages somewhat difficult. So, to obtain msxsl I recommend that you go to the main Microsoft site and do a search for msxsl.
There are, of course, other such utilities (for example, xsltproc). Xalan and msxsl are the two I used for this book, and they'll do for this tutorial.
Okay, we've laid down some basic concepts, discussed some basic issues, and done some housekeeping. Let's look now at a few of the common types of transformations you'll need to perform. The first set, presented in the next section, has to do with manipulating content of only a few Elements and/or Attributes. The section following that deals with the types of problems frequently encountered when the structures of the source and result trees are different or when we need to move things to different places in the trees.