Transforming XML with XSLT
- Installation and Setup
- Translating
- XSLT Example 1: XSLT Document Editor
- XSLT Example 2: Custom JSP Tag
- References
XSLT is a language for transforming XML documents into HTML, XML, or other types of documents. When performing a transformation, an XSLT engine converts the XML document according to formatting rules and XPath addresses specified in an XML style sheet (XSL). The XPath information identifies the different parts of the XML document for processing, and the style sheet information identifies the layout of the output.
The benefit of XSLT is that you can define multiple style sheets for transforming a single XML document. For example, a database could return a query in an XML format, and, depending on the client protocol, HTTP or WAP, a servlet could use different style sheets to convert the data into HTML or WML, respectively. As another example of an XSLT application, consider an e-commerce business order; the order could be sent to the supplier in an XML format and then processed by the recipient with XSLT, using different XSL documents to convert the original order into separate billing and shipping documents.
Specifications for XSLT, XSL, and XPath technologies are maintained by the WWW Consortium (W3C) at the following locations:
Upcoming specifications are summarized at http://www.w3.org/Style/XSL/. In addition, GoXML sponsors an excellent XSLT resource site.
To download a zip containing the source files for this article, click here.
Installation and Setup
XSLT is not a standard part of either Java 2 Standard Edition or the servlet and JSP APIs. So, your first step is to download the appropriate classes and configure them for use in your programs. Here is a summary of what's required:
Download an XSLT-compliant transformer. The transformer provides the Java classes that follow the XSLT 1.0 specification as specified by the W3C. You can obtain a list of XSLT parsers at http://www.w3.org/Style/XSL/ or http://www.xslt.com/xslt_tools_engines.htm. We use the Apache Xalan-Java (Xalan-J) transformer in Core Web Programming.
Set your CLASSPATH to include the DOM and SAX classes. XSLT builds on DOM and SAX for handling the document processing. In the case of Apache Xalan-J, you need to include xerces.jar in the CLASSPATH. For example, for desktop application on Windows, you would do this:
set CLASSPATH=xerces_install_dir\xerces.jar; %CLASSPATH%
On Unix/Linux and the C shell, you would do this:
setenv CLASSPATH xerces_install_dir/xerces.jar: $CLASSPATH
Note that xerces.jar is included in the Xalan-J installation directory. Tomcat 4.0 already uses Xerces-J for parsing XML documents, so if using DOM or SAX in your servlets and JavaServer Pages, xerces.jar is already installed and located in the TOMCAT_HOME\common\lib directory.
Set your CLASSPATH to include the XSLT classes. With Xalan, these classes are in xalan_install_dir\xalan.jar. Similarly, for desktop applications on Windows, you would do this:
set CLASSPATH=xalan_install_dir\xalan.jar; %CLASSPATH%
On Unix/Linux and the C shell, you would do this:
setenv CLASSPATH xalan_install_dir/xalan.jar: $CLASSPATH
If you wanted to use XSLT from servlets and JSP, you would copy the appropriate DOM, SAX, and XSLT JAR files to the server's lib directory (if supported); unpack the JAR files (using jar -xvf) into the server's classes directory; or explicitly change the server's CLASSPATH, usually by modifying the server's startup script. If configuring Tomcat 4.0 for XSLT, place xalan.jar in the TOMCAT_HOME\common\lib directory.
Bookmark the XSL 1.0 and XPath 1.0 specifications. The official documentation for these two specifications can be found at http://www.w3.org/Style/XSL/.
Bookmark the XSLT specification. The official XSLT specification can be found at http://www.w3.org/TR/xslt.html. The XSLT specification is implemented in Apache Xalan through the Transformation API for XML (TrAX). The complete TrAX API comes with Xalan-J in Javadoc format and is also available online at http://xml.apache.org/xalan-j/apidocs/.