This tip uses JAXP and Xalan-Java from the Apache project. The classes are also part of the Java 2 SDK 1.4, so if you have 1.4 installed, you don't need any additional software.
The process of transforming a document involves creating a Transformer object using a style sheet as its basis, setting the source and result, and performing the transformation. In its simplest form, it looks like the code in Listing 1. Perform a transformation.
The simplest way you can indicate a document's XML style sheet is to add the style sheet processing instruction and associate a style sheet with the document. In this case, you simply add one line to the source document (see Listing 2. Adding a style sheet processing instruction).
From here, the application can retrieve the
information using the getAssociatedStyleSheet() method of the TransformerFactory as shown in Listing 3. Get the associated style sheet.
Notice that this method actually takes four parameters. It is defined as:
getAssociatedStylesheet(Source source,
java.lang.String media,
java.lang.String title,
java.lang.String charset)
|
This allows you to choose a style sheet based on the media and title, as well as the character set. For example, the XML document might read like Listing 4. Multiple style sheets.
In this way, the application can specifically choose, for example, the print version (see Listing 5. Choosing a specific media).
Choosing a style sheet based on the data
On the other hand, you may want to literally choose your style sheet based on the data in the document. In this case, you first parse the document and retrieve the data directly, and then use the data to determine the style sheet.
For example, consider an application that processes multiple pages and chooses the style sheet based on the name of the root element. Accurately transforming the files involves parsing the document and then determining the root element name, as shown in Listing 6. Parse the document.
The operation here takes two major steps. First, parse the document
and get the name of the root element as a String value to carry into the second
try-catch block.
In the second try-catch block, determine what the new style
sheet will be. This example shows only two choices, and if the root element is neither
of them, the style sheet name remains null.
From here, it's a matter of creating the
actual Transformer object. If a style sheet has been chosen, it
should obviously be used to create the style
Source, which you then pass to the newTransformer() method to create the
Transformer object. If, on the other hand, no choice has been made, you have the option to create a Transformer object without a style sheet. This has
the effect of simply passing the source document through
unchanged.
This tip has demonstrated simple ways to use the data in an XML document to determine which style sheet to process. You can extend this principle to include drawing information from other resources, such as a database, to determine the style sheet. Taking things a step further, you can use information from the XML document to set parameters within the style sheet, or even to create a style sheet on the fly and use it to transform the original document.
- Check out the W3C working draft of the XSLT 2.0 specification.
- Download
JAXP or
Xalan-Java.
- Find more XML resources on the developerWorks XML zone.
-
IBM trial software: Build your next development project with trial software available for download directly from developerWorks.

Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, Fla., and is the author of three books on Web development, including Java and XML From Scratch (Que) and the upcoming Primer Plus XML Programming (Sams). He loves to hear from readers and can be reached at: nicholas@nicholaschase.com.




