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

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

XSLT Transformations

📄 Contents

  1. Using XSLT Stylesheets
  2. Making an XSLT Transformation Happen
  3. About This Article
Like this article? We recommend

Like this article? We recommend

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.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020