Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Using Standalone XSLT Processors

One of the most common ways of making XSLT transformations happen isto 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 of the available standalone XSLT processors available online, in alphabetical order—most (but not all) are free:

The following sections examine four of these XSLT processors in more detail: XT, Saxon, Oracle's XSLT processor, and Xalan. All these programs are available for free online, and can implement the XSLT examples shown in this book. If you want to follow the examples in this book, it will be useful to pick up one or more of these standalone XSLT processors (probably the best known and most widely used is Xalan). To make XSLT transformations happen, I'll use these XSLT processors throughout the book.

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 for 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. All you have to do is download Java for your operating system and follow the installation instructions on the download pages.

Although you need Java to run these XSLT processors, don't panic if you're not a programmer—no programming is required. Although Chapter 10 does go into some Java programming to show you how to create XSLT transformations in code, all these processors—XT, Saxon, Oracle's XSLT processor, and Xalan—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. This way of doing things is covered as well.

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, or 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 will transform 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 java.exe is not in your path, 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 at first you might feel that 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 the Macintosh to UNIX, which means that the XSLT processor can run on all those platforms as well.

Of course, this is all a lot easier if you're running Windows and use the precompiled version of either 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 (XT, Saxon, Oracle's XSLT processor, and Xalan) 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; and although all 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 will use 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 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 download saxon.zip and unzip it, which creates the Java JAR file you need, saxon.jar.

To perform XSLT transformations, you 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 com.icl.saxon.StyleSheet.class, the Saxon XSLT 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 http://users.iclway.co.uk/mhkay/saxon/, 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, you click the XDK for Java link at http://technet.oracle.com/tech/xml/ to get the XSLT processor.

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


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 -IN 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. Xalan is the XSLT processor we'll use most frequently, so here are some more details. The following list includes all the tokens you can use with the org.apache.xalan.xslt.Process class, as printed out by Xalan itself:

  • CR (Use carriage returns only on output—default is CR/LF)
  • DIAG (Output timing diagnostics)
  • EDUMP [optional]FileName (Do stackdump on error)
  • HTML (Use HTML formatter)
  • IN inputXMLURL
  • INDENT (Number of spaces to indent each level in output tree—default is 0)
  • LF (Use linefeeds only on output—default is CR/LF)
  • OUT outputFileName
  • PARAM name value (Set a stylesheet parameter)
  • Q (Quiet mode)
  • QC (Quiet Pattern Conflicts Warnings)
  • TEXT (Use simple text formatter)
  • TG (Trace each result tree generation event)
  • TS (Trace each selection event)
  • TT (Trace the templates as they are being called)
  • TTC (Trace the template children as they are being processed)
  • V (Version info)
  • VALIDATE (Validate the XML and XSL input—validation is off by default)
  • XML (Use XML formatter and add XML header)
  • XSL XSLTransformationURL

You'll see all these processors in this book, but as mentioned, probably the one I'll use most is Xalan. (The reason I use Xalan most often is because it has become the most popular XSTL processor and is the most widespread use). Of course, you can use any XSLT processor, as long as it conforms to the W3C XSLT specification.

That completes your look at standalone XSLT processors. There's another way to transform XML documents without a standalone program—you can use a client program, such as a browser, to transform documents.

  • + Share This
  • 🔖 Save To Your Account