Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > HTML/XHTML

📄 Contents

  1. Using XSLT Stylesheets
  2. Making an XSLT Transformation Happen
  3. About This Article
  • Print
  • + Share This
From the author of

From the author of

Making an XSLT Transformation Happen

You use an XSLT processor to bring about an XSLT transformation, such as transforming planets.xml into planets.html. You can use XSLT in three ways to transform XML documents:

  • With standalone programs called XSLT processors. There are several programs, usually based on Java, that perform XSLT transformations; I'll cover a number of them in this article.

  • In the client. A client program, such as a browser, can perform the transformation, reading in the stylesheet you specify with the <?xml-stylesheet?> processing instruction. Internet Explorer can handle transformations this way to some extent.

  • In the server. A server program, such as a Java servlet, can use a stylesheet and an XSLT processor to transform a document automatically and send it to the client.

Using Standalone XSLT Processors

One of the most common ways of making XSLT transformations happen is to use standalone XSLT processors. There are plenty of such processors around, although not all can handle all possible XSLT stylesheets. To use an XSLT processor, you just run it from the command line (which means in a DOS window in Windows), giving it the name of the XML source document, the XSLT stylesheet to use, and the name of the document you want to create.

Here's a starter list of some standalone XSLT processors available free online. These processors are all Java-based, so you'll need Java installed on your system. If you don't already have Java, you can get it free at Sun's Java site. The most recent edition, Java 2 version 1.3, is available at http://java.sun.com/j2se/1.3, as of this writing.

  • SAXON. An XSLT processor that fully implements XSLT 1.0 and XPath 1.0, as well as a number of extensions to these specifications. Note that this release also has some support for the XSLT 1.1 working draft.

  • Xalan Java. Java implementation of the W3C Recommendations for XSLT and the XML Path Language (XPath); the Java version of the famous Apache Xalan processor. Also includes extension functions for SQL access to databases via JDBC, and much more.

  • XML parser for Java. Oracle's XSLT processor. Supports the XSLT 1.0 Recommendation, created for use with Java.

  • XT. A well-known implementation in Java of the XSLT Recommendation.

The following sections examine these four XSLT processors in more detail. Although you need Java to run these XSLT processors, don't panic if you're not a programmer - no programming is required. All these processors can be run from the command line. If you are running Windows, there's an even easier way to use XT and Saxon: They both come packaged as an .exe file (xt.exe and saxon.exe) that you can run directly in Windows, and you won't need Java at all.

Using a Java XSLT Processor

To use a Java-based XSLT processor, you download it and unzip it, and it's ready to go. You should read the posted directions, of course, but typically there are just two steps to take.

First, you must let Java know how to find the XSLT processor, which is stored in a Java Archive (JAR) file. To tell Java to search the JAR file, you set the classpath environment variable to the path of the JAR file. For example, in any version of Windows, you start by opening a DOS window. Then you can execute a line such as the following, which sets the classpath variable to the Oracle XSLT processor's JAR file, xmlparserv2.jar, which in this case is stored in the directory c:\oraclexml\lib:

C:\>set classpath=c:\oraclexml\lib\xmlparserv2.jar

Now you're ready to take the second step, which is to run the XSLT processor. This involves executing the Java class that supports the XSLT processor. For the Oracle XSLT processor, this is oracle.xml.parser.v2.oraxsl. In Windows, for example, you could change to the directory that held the planets.xml and planets.xsl files, and execute oracle.xml.parser.v2.oraxsl using Java this way:

C:\planets>java oracle.xml.parser.v2.oraxsl planets.xml planets.xsl
  planets.html

This line transforms planets.xml to planets.html using planets.xsl. Note that this example assumes that java.exe, which is what runs Java, is in your Windows path. If it's not, you can specifically give its location, which is the Java bin directory, such as c:\jdk1.3\bin (JDK stands for Java Development Kit, and Java 2 version 1.3 installs itself in the c:\jdk1.3 directory by default) as follows:

C:\planets>c:\jdk1.3\bin\java oracle.xml.parser.v2.oraxsl 
  planets.xml planets.xsl planets.html

In fact, you can combine the two steps (setting the classpath and running the XSLT processor) into one if you use -cp with Java to indicate what classpath to use:

C:\planets>c:\jdk1.3\bin\java -cp c:\oraclexml\lib\xmlparserv2.jar
  oracle.xml.parser.v2.oraxsl planets.xml planets.xsl planets.html

These are all fairly long command lines, and you might feel this is a complex way of doing things. However, there's a reason that most XSLT processors are written in Java: Java is supported on many platforms, from Macintosh to UNIX, which means the XSLT processor can run on all those platforms as well.

Of course, these steps are a lot easier if you're running Windows and use the precompiled version of XT (which is xt.exe) or Saxon (saxon.exe). For example, here's how to use xt.exe in Windows to perform the same transformation (this example assumes that xt.exe is in your path):

C:\planets>xt planets.xml planets.xsl planets.html

That's the process in overview; now I'll take a look at each of the four XSLT processors in depth, showing exactly how to use each one. First, note two things: XML and XSL software changes very quickly, so by the time you read this, some of it might already be out of date. Second, although these XSLT processors are supposed to support all standard XSLT, they give different results on some occasions.

James Clark's XT

You can get James Clark's XT at http://www.jclark.com/xml/xt.html. Besides XT itself, you'll also need an XML parser, which XT uses to read your XML document. The XT download also comes with sax.jar, which holds James Clark's XML parser, or you can use James Clark's XP parser, which you can get at http://www.jclark.com/xml/xp/index.html, for this purpose.

My own preference is to use the Apache Project's Xerces XML parser, which is available at http://xml.apache.org. (As of this writing, the current version, Xerces 1.3.0, is available at http://xml.apache.org/dist/xerces-j/ in zipped format for UNIX as Xerces-J-bin.1.3.0.tar.gz and Windows as Xerces-J-bin.1.3.0.zip.)

XT itself is a Java application, and included in the XT download is the JAR file you'll need, xt.jar. To use xerces.jar and xt.jar, you must include them both in your classpath, as shown in the following example for Windows (modify the locations of these files as needed):

C:\>set classpath=C:\xerces-1_3_0\xerces.jar;C:\xt\xt.jar

Then you can use the XT transformation class, com.jclark.xsl.sax.Driver.class. You supply the name of the parser you want to use, which in this case is org.apache.xerces.parsers.SAXParser in xerces.jar, by setting the com.jclark.xsl.sax.parser variable to that name on the command line. For example, here's how I use XT to transform planets.xml, using planets.xsl, into planets.html in Windows (assuming that c:\planets is the directory that holds planets.xml and planets.xsl, and that java.exe is in your path):

C:\planets>java -Dcom.jclark.xsl.sax.parser=org.apache.xerces.
  parsers.SAXParser com.jclark.xsl.sax.Driver planets.xml planets.xsl
  planets.html

That line is quite a mouthful, so it might provide some relief to know that XT is also packaged as a Win32 executable program, xt.exe. To use xt.exe, however, you need the Microsoft Java Virtual Machine (VM) installed (which is included with the Internet Explorer). Here's an example in Windows that performs the same transformation as the preceding command, assuming xt.exe is in your path:

C:\planets>xt planets.xml planets.xsl planets.html

If xt.exe is not in your path, you can specify its location directly, like this if xt.exe is in c:\xt:

C:\planets>c:\xt\xt planets.xml planets.xsl planets.html

Saxon

Saxon by Michael Kay is one of the earliest XSLT processors, and you can get it for free at http://users.iclway.co.uk/mhkay/saxon/. All you have to do is to download saxon.zip and unzip it, which creates the Java JAR file you need, saxon.jar.

To perform XSLT transformations, first make sure that saxon.jar is in your classpath. For example, in Windows, assuming that saxon.jar is in c:\saxon, you can set the classpath variable this way:

C:\>set classpath=c:\saxon\saxon.jar

Now you can use the Saxon XSLT class, which is com.icl.saxon.StyleSheet.class, like this to perform an XSLT transformation:

C:\planets>java com.icl.saxon.StyleSheet planets.xml planets.xsl

By default, Saxon sends the resulting output to the screen, which is not what you want if you want to create the file planets.html. To create planets.html, you can use the UNIX or DOS > pipe symbol like this, which sends Saxon's output to that file:

C:\planets>java com.icl.saxon.StyleSheet planets.xml 
  planets.xsl > planets.html

If you're running Windows, you can also use instant Saxon, which is a Win32 executable program named saxon.exe. You can download saxon.exe from the same Web site you used for the Saxon processor, and run it in Windows like this (the -o planets.html part specifies the name of the output file here):

C:\planets>saxon -o planets.html planets.xml planets.xsl

Oracle XSLT

Oracle Corporation also has a free XSLT processor, which you can get from http://technet.oracle.com/tech/xml/. You have to go through a lengthy registration process to get it, though. As of this writing, to get the XSLT processor, you click the XDK for Java link.

When you unzip the download from Oracle, the JAR file you need (as of this writing) is named xmlparserv2.jar. You can put it in your classpath in Windows as follows:

C:\>set classpath=c:\oraclexml\lib\xmlparserv2.jar

The actual Java class you need is oracle.xml.parser.v2.oraxsl, and you can use it like this to transform planets.xml into planets.html using planets.xsl:

C:\planets>java oracle.xml.parser.v2.oraxsl planets.xml planets.xsl 
  planets.html

Xalan

Probably the most widely used standalone XSLT processor is Xalan, from the Apache Project (Apache is a type of Web server in widespread use). You can get the Java version of Xalan at http://xml.apache.org/xalan-j/index.html. Just click the zipped file you want, currently xalan-j_2_0_0.zip for Windows or xalan-j_2_0_0.tar.gz for UNIX.

When you unzip the downloaded file, you get both xalan.jar, the XSLT processor, and xerces.jar, the XML parser you need. You can include both these JAR files in your classpath like this in Windows (modify the paths here as appropriate for your system):

C:\>set classpath=c:\xalan-j_2_0_0\bin\xalan.jar;c:\xalan-
  j_2_0_0\bin\xerces.jar

To then use planets.xsl to transform planets.xml into planets.html, execute the Java class you need, org.apache.xalan.xslt.Process, as follows:

C:\planets>java org.apache.xalan.xslt.Process
  -IN planets.xml -XSL planets.xsl -OUT planets.html

Note that you use the -IN token to specify the name of the input file, -OUT to specify the name of the output file, and -XSL to specify the name of the XSLT stylesheet.

This article has introduced you to the concepts of XSLT transformations. Using the methods I explained here, you can download free XSLT processors to use when working with your XML documents and then transform those documents with your own XSLT stylesheets.

  • + Share This
  • 🔖 Save To Your Account