Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Tip: Set an XSL style sheet based on XML content

Nicholas Chase (nicholas@nicholaschase.com), President, Chase and Chase, Inc.
Photo of Nicholas Chase
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.

Summary:  Using Extensible Stylesheet Language Transformations (XSLT) is often the easiest way to transform your XML data from one form into another, as it allows you a great deal of flexibility without having to change an application. On occasion, however, you may run into a snag because the style sheet to be used is based on the content of the document itself. This tip discusses two ways to base your choice of style sheet on the XML document -- the style sheet processing instruction and the data itself.

View more content in this series

Date:  01 May 2002
Level:  Introductory
Also available in:   Japanese

Activity:  4856 views
Comments:  

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.

Transforming a document

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.


Associating a style sheet

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.


Next steps

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.


Resources

About the author

Photo of Nicholas Chase

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12101
ArticleTitle=Tip: Set an XSL style sheet based on XML content
publish-date=05012002
author1-email=nicholas@nicholaschase.com
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers