Back Contents Next

How does XSLT transform XML?

By now you are probably wondering exactly how XSLT goes about processing an XML document in order to convert it into the required output. There are usually two aspects to this process:

 

q        The first stage is a structural transformation, in which the data is converted from the structure of the incoming XML document to a structure that reflects the desired output.

q        The second stage is formatting, in which the new structure is output in the required format such as HTML or PDF.

 

The second stage covers the ground we discussed in the previous section: the data structure that results from the first stage can be output as HTML, a text file or as XML. HTML output allows the information to be viewed directly in a browser by a human user or be input into any modern word processor. Plain text output allows data to be formatted in the way an existing application can accept, for example comma-separated values or one of the many text-based data interchange formats that were developed before XML arrived on the scene. Finally, XML output allows the data to be supplied to one of the new breed of applications that accepts XML directly. Typically this will use a different vocabulary of XML tags from the original document: for example an XSLT transformation might take the monthly sales figures as its XML input and produce a histogram as its XML output, using the XML-based SVG standard for vector graphics. Or you could use an XSLT transformation to generate VOXML output, for aural rendition of your data.

 

A white paper describing Motorola's VOXML Voice Markup Language can be found at http://www.voxml.com/downloads/VoxMNwp.pdf

Let's now delve into the first stage, transformation - the stage with which

XSLT is primarily concerned and which makes it possible to provide output in all of these formats. This stage might involve selecting data, aggregating and grouping it, sorting it, or performing arithmetic conversions such as changing centimeters to inches.

 

So how does this come about? Before the advent of XSLT, you could only process incoming XML documents by writing a custom application. The application wouldn't actually need to parse the raw XML, but it would need to invoke an XML parser, via a defined Application Programing Interface (API), to get information from the document and do something with it. There are two principal APIs for achieving this: the Simple API for XML (SAX) and the Document Object Model (DOM).

 

The SAX API is an event-based interface in which the parser notifies the application of each piece of information in the document as it is read. If you use the DOM API, then the parser interrogates the document and builds a tree-like object structure in memory. You would then write a custom application (in a procedural language such as C++, Visual Basic, or Java, for example), which could interrogate this tree structure. It would do so by defining a specific sequence of steps to be followed in order to produce the required output. Thus, whatever parser you use, this process has the same principal drawback: every time you want to handle a new kind of XML document, you have to write a new custom program, describing a different sequence of steps, to process the XML.

 

Both the DOM and the SAX APIs are fully described in the Wrox Press book Professional XML, ISBN 1-861003-11-0.

 

So how is using XSLT to perform transformations on XML better than writing "custom applications"? Well, the design of XSLT is based on a recognition that these programs are all very similar, and it should therefore be possible to describe what they do using a high-level declarative language rather than writing each program from scratch in C++, Visual Basic, or Java. The required transformation can be expressed as a set of rules. These rules are based on defining what output should be generated when particular patterns occur in the input. The language is declarative, in the sense that you describe the transformation you require, rather than providing a sequence of procedural instructions to achieve it. XSLT describes the required transformation and then relies on the XSL processor to decide the most efficient way to go about it.

 

XSLT still relies on a parser –  be it a DOM parser or a SAX-compliant one – to convert the XML document in to a "tree structure". It is the structure of this tree representation of the document that XSLT manipulates, not the document itself. If you are familiar with the DOM, then you will be happy with the idea of treating every item in an XML document (elements, attributes, processing instructions etc.) as a node. With XSLT we have a high-level language that can navigate around a node tree, select specific nodes and perform complex manipulations on these nodes.

 

The XSLT tree model is similar in concept to the DOM but it is not the same. The full XSLT processing model is discussed in Chapter 2.

The description of XSLT given thus far (a declarative language that can navigate to and select specific data and then manipulate that data) may strike you as being similar to that of the standard database query language: SQL. Let's take a closer look at this comparison.

XSLT and SQL: an Analogy

I like to think of an analogy with relational databases. In a relational database, the data consists of a set of tables. By themselves, the tables are not much use, the data might as well be stored in flat files in comma-separated values format. The power of a relational database doesn't come from its data structure; it comes from the language that processes the data, SQL. In the same way, XML on its own just defines a data structure. It's a bit richer than the tables of the relational model, but by itself it doesn't actually do anything very useful. It's when we get a high-level language expressly designed to manipulate the data structure that we start to find we've got something interesting on our hands: and for XML data that language is XSLT.

 

Superficially, SQL and XSLT are very different languages. But if you look below the surface, they actually have a lot in common. For starters: in order to process specific data, be it in a relational database or an XML document, the processing language must incorporate a declarative query syntax for selecting the data that needs to be processed. In SQL, that's the SELECT statement. In XSLT, the equivalent is the XPath expression.

 

The XPath expression language forms an essential part of XSLT, though it is actually defined in a separate W3C Recommendation (http://www.w3.org/TR/xpath) because it can also be used independently of XSLT (the relationship between XPath and XSLT is discussed further on page 23).

 

The XPath query syntax is designed to retrieve nodes from an XML document, based on a path through the XML document or the context in which the node appears. It allows access to specific nodes, while preserving the hierarchy and structure of the document. XSLT is then used to manipulate the results of these "queries" (rearranging selected nodes, constructing new nodes etc).

 

There are further similarities between XSLT and SQL:

 

q        Both languages augment the basic query facilities with useful additions for performing basic arithmetic, string manipulation, and comparison operations.

q        Both languages supplement the declarative query syntax with semi-procedural facilities for describing the sequence of processing to be carried out, and they also provide hooks to escape into conventional programming languages where the algorithms start to get too complex.

q        Both languages have an important property called closure, which means that the output has the same data structure as the input. For SQL15, this structure is tables, for XSLT it is trees – the tree representation of XML documents. The closure property is extremely valuable because it means operations performed using the language can be combined end-to-end to define bigger more complex operations: you just take the output of one operation and make it the input of the next operation. In SQL you can do this by defining views or subqueries; in XSLT you can do it by passing your data through a series of stylesheets.

 

In the real world, of course, XSLT and SQL have to coexist. There are many possible relationships, but typically data will be stored in relational databases and transmitted between systems in XML. The two languages don't fit together as comfortably as one would like, because the data models are so different. But XSLT transformations can play an important role in bridging the divide. A number of database vendors are working on products that integrate XML and SQL, though there are no standards in this area as yet.

 

SQL Server 2000 will support XPath queries on its data. Prior to the release of SQL Server 2000, Microsoft has released the XML SQL Technology Preview, which allows access to data in a SQL Server 6.5 or 7.0 databases in XML form.

The XML SQL Technology Preview is available from http://msdn.microsoft.com/workshop/xml/articles/xmlsql/sqlxmlsetup.exe.

 

Before we move on to look at a simple working example of an XSLT transformation, we need to briefly discuss a few of the XSLT processors that are available to effect these transformations.

 

XSLT Processors

The principle role of an XSLT processor is to apply an XSLT stylesheet to an XML source document and produce a result document. It is important to note that each of these is an application of XML and so the underlying structure of each is a tree. So, in fact, the XSLT processor handles three trees.

 

There are several XSLT processors to choose from. Here I'll mention three: Saxon, xt, and Microsoft MSXML3. All of these can be downloaded free of charge (but do read the licensing conditions).

 

These three processors and several others are described in Chapter 10.

Saxon is an open source XSLT processor developed by the author of this book. It is a Java application, and can be run directly from the command prompt: no web server or browser is required. The Saxon program will transform the XML document to, say, a HTML document, which can then be placed on a web server. In this example, both the browser and web server only deal with the transformed document.

 

If you are running Windows (95/98/NT/2000) the simplest way to use it is to download Instant Saxon, which is packaged as a Windows executable. You will need to have Java installed, but that will be there already if you have any recent version of Internet Explorer. On non-Windows platforms you will need to install the full Saxon product and follow the instructions that come with it. You can download Instant Saxon for free from http://users.iclway.co.uk/mhkay/saxon/instant.html. Saxon will run with any XML parser that implements the SAX interface (in its original Java form).

 

xt is another open source XSLT processor developed by James Clark, the editor of the XSLT specification. Like Saxon, this is a Java application that can be run from the command prompt; it too has a simple packaged version for the Windows platform and a full version for other environments. This time the download is from http://www.jclark.com/xml/xt.html. Like Saxon, xt can operate with any SAX-compliant parser.

 

Alternatively, you can run XSLT stylesheets actually within Internet Explorer. You'll need to install Internet Explorer 5 and the latest version of the Microsoft MSXML processor, which you can find at http://www.microsoft.com/xml. The information here is correct for the 15 March 2000 technology preview, referred to as MSXML3, but Microsoft has promised a rapid sequence of new releases, so check the latest position. MSXML3 comes with a new version of the MSXML parser.

 

Download and install both the SDK and the run-time package. Installing the SDK creates a program called xmlinst.exe, typically in the windows\system directory. Run this program to establish MSXML3 as the default XML processor to be used by Internet Explorer (if you don't do this, IE5 will try to use the old 1998 MSXML processor, which implements an obsolete dialect of XSL that is quite different from the language described in this book: see Chapter 10 for details). The big advantage of Microsoft's technology is that the XSLT processing can take place on the browser.

 

I've avoided talking about specific products in most of the book, because the information is likely to change quite rapidly. It's best to get the latest status from the web. Some good places to start are:

 

q        http://www.w3.org/Style/XSL

q        http://www.xslinfo.com/

q        http://www.xml.com/

q        http://www.oasis-open.org/cover

An Example Stylesheet

Now we're ready to take a look at an example of using XSLT to transform a very simple XML document.

 

Example: A "Hello, world!" XSLT Stylesheet

 

Kernighan and Ritchie in their classic The C Programming Language originated the idea of presenting a trivial but complete program right at the beginning of the book, and ever since then the "Hello world" program has been an honored tradition. Of course, a complete description of how this example works is not possible until all the concepts have been defined: so if you feel I'm not explaining it fully, don't worry – the explanations will come later.

 

Input

What kind of transformation would we like to do? Let's try transforming the following XML document:

 

<?xml version="1.0" encoding="iso-8859-1"?>

<greeting>Hello, world!</greeting>

 

A simple node-tree-representation of this document would look as follows:

 

 

There is one root node per document. The root node in the XSLT model performs the same function as the document node in the DOM model. The XML declaration is not visible to the parser and, therefore, is not included in the tree.

 

Output

Our required output is the following HTML, which will simply change the browser title to "Today's Greeting" and display whatever greeting is in the source XML file:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<html>

<head>

   <title>Today's greeting</title>

</head>

<body>

   <p>Hello, world!</p>

</body>

</html>

 

XSLT StyleSheet

Without any more ado, here's the XSLT stylesheet to effect the transformation:

 

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet

   version="1.0"

   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

<xsl:template match="/">

   <html>

   <head>

      <title>Today's greeting</title>

   </head>

   <body>

      <p><xsl:value-of select="greeting"/></p>

   </body>

   </html>

</xsl:template>

 

</xsl:stylesheet>

 

Running the Stylesheet

You can run this stylesheet using any of the three processors described in the previous section.

Saxon

With Saxon, the steps are:

 

q        Download the processor

q        Install the executable saxon.exe in a suitable directory, and make this the current directory

 

 

 

 

 

 

 

 

 

 

q        Using Notepad, type the two files above into hello.xml and hello.xsl respectively, within this directory (or get them from the Wrox web site at http://www.wrox.com)

q        Bring up an MSDOS-style console window (Start | Programs | MSDOS Prompt)

q        Type the following at the command prompt:
saxon hello.xml hello.xsl

q        Admire the HTML displayed on the standard output

 

If you want to view the output using your browser, simply save the command line output as an HTML file, in the following manner:

 

Saxon hello.xml hello.xsl > hello.html

xt

The procedure is very similar if you use xt. This time the command to use the Windows executable is xt rather than saxon. It should give the same result.

<