Skip to main content

If you don't have an IBM ID and password, register here.

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

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. 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.

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.

Tip: Use imports and includes to override XSLT templates

Provide flexibility in your style sheets while still maintaining control

Nicholas Chase (nicholas@nicholaschase.com), President, Chase and Chase Inc.
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, Florida, USA, 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:  Extensible Stylesheet Language Transformations (XSLT) provide two different ways to combine several style sheets in the transformation of a single source document. In the process, it's almost inevitable that more than one template can apply to a single node. Which template is actually applied depends on whether and how the template has been included in the main style sheet. This tip explains how to use these conflict resolution rules to give users control -- but not too much control -- over styling issues.

View more content in this series

Date:  01 Nov 2002
Level:  Introductory

Comments:  

Note: For this tip, you can use any XSLT processor, such as Xalan or Saxon, or a browser-based solution, such as Microsoft Internet Explorer or Mozilla.

The basic documents

This tip describes a style sheet that converts an XML announcement into HTML. The overall goal is to provide default templates, but to allow the user enough latitude to change certain aspects of the style sheet if desired. Other aspects are not changeable. The style sheets work with the following source document:


Listing 1. The source document
                

<?xml version="1.0"?>
<?xml-stylesheet href="style.xsl" version="1.0" 
    type="text/xsl"?>
<announcement>
 <headline>Contest Announcement</headline>
 <description>
   Do you love 
   <product>Pop's Homemade Mashed Potato Mix</product>?
   Are you artistic?  Well, here's your big big chance!  
   Loopy Foods, the company that brings you 
   <product>Pop's Homemade Mashed Potato Mix</product> 
   and <product>Aunt Susie's Squash in a Box</product> 
   is hosting a Mashed Potato Sculpting Contest.  Send your 
   entry today.  Contest rules are on the back of every box 
   of <product>Pop's Homemade Mashed Potato Mix</product>.
 </description>
 <copyright/>
</announcement>

The goal is to create an HTML page with a style sheet such as this:


Listing 2. The basic style sheet
                

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
    <head><title><xsl:value-of select="announcement/headline"/></title></head>
    <body>
       <xsl:apply-templates/>
    </body>
</html>
</xsl:template>

<xsl:template match="headline">
    <h1><xsl:apply-templates/></h1>
</xsl:template>

<xsl:template match="description">
    <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="product">
    <b><xsl:apply-templates/></b>
</xsl:template>

</xsl:stylesheet>

The result is a document that creates a paragraph for each announcement, and puts the product names in bold:


Listing 3. The basic transformation results
                

<html>
<head><title>Contest Announcement</title></head>
<body>

<h1>Contest Announcement</h1>
<p>
       Do you love <b>Pop's Homade Mashed Potato Mix</b>?
       Are you artistic?  Well, here's your big big chance!  Loopy Foods, the
       company that brings you <b>Pop's Homemade Mashed Potato Mix</b>
       and <b>Aunt Susie's Squash in a Box</b> is hosting a Mashed Potato
       Sculpting Contest.  Send your entry today. Contest rules are on the
       back of every box of <b>Pop's Homemade Mashed Potato Mix</b>.
    </p>
</body>
</html>


Creating the template style sheet

Using these templates, an administrator can build a basic style sheet that enables users to use default styles, but allows them to use custom styles if necessary. To do this, you need to include the basic templates in a second style sheet, in this case called import.xsl:


Listing 4. The imported style sheet (import.xsl)
                

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="headline">
    <h1><xsl:apply-templates/></h1>
</xsl:template>

<xsl:template match="description">
    <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="product">
    <b><xsl:apply-templates/></b>
</xsl:template>

</xsl:stylesheet>

From there, it's a matter of creating a style sheet that imports the main styles. With this import in place, a style sheet that contains only the main template still acts as desired, but if an additional template is added, it overrides the corresponding template in the import:


Listing 5. Overriding imported templates
                

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="import.xsl"/>

<xsl:template match="/">
<html>
    <head><title><xsl:value-of select="announcement/headline"/></title></head>
    <body>
       <xsl:apply-templates/>
    </body>
</html>
</xsl:template>

<xsl:template match="product">
    <i><xsl:apply-templates/></i>
</xsl:template>

</xsl:stylesheet>

Imported style sheets have a lower precedence than the main style sheet, so the product template in the main style sheet is used instead of the imported product template:


Listing 6. Overridden template results
                

<html>
<head><title>Contest Announcement</title></head>
<body>
<h1>Contest Announcement</h1>
<p>
       Do you love <i>Pop's Homade Mashed Potato Mix</i>?
       Are you artistic?  Well, here's your big big chance!  Loopy Foods, the
       company that brings you <i>Pop's Homemade Mashed Potato Mix</i>
       and <i>Aunt Susie's Squash in a Box</i> is hosting a Mashed Potato
       Sculpting Contest.  Send your entry today. Contest rules are on the
       back of every box of <i>Pop's Homemade Mashed Potato Mix</i>.
    </p>
</body>
</html>

Even when a template is overridden, you still have the opportunity to use the imported templates, however, using the apply-imports option:


Listing 7. Applying imports
                

...
</html>
</xsl:template>

<xsl:template match="product">
    <i><xsl:apply-imports/></i>
</xsl:template>

</xsl:stylesheet>

As a result, both the original template and the imported template are executed:


Listing 8. Using overridden templates
                

<html>
<head><title>Contest Announcement</title></head>
<body>

<h1>Contest Announcement</h1>

<p>
       Do you love <i><b>Pop's Homade Mashed Potato Mix</b></i>?
       Are you artistic?  Well, here's your big big chance!  Loopy Foods, the
       company that brings you <i><b>Pop's Homemade Mashed Potato Mix</b></i>
       and <i><b>Aunt Susie's Squash in a Box</b></i> is hosting a Mashed Potato
       Sculpting Contest.  Send your entry today. Contest rules are on the
       back of every box of <i><b>Pop's Homemade Mashed Potato Mix</b></i>.
    </p>
</body>
</html>


The difference between importing and including

An import element must always be a top-level element, and must always come before any other elements. This requirement has a specific consequence in terms of precedence. In an XSLT style sheet, the last template processed has precedence over everything that has come before, so imported templates are always overridden. Imported templates also become part of the import tree, so they're available for the apply-imports element.

On the other hand, style sheets can also be included using the include element, which simply adds them to the main style sheet at the point in which they're included. What's more, while a style sheet must be included at the top level, it can be added at any point, so it can easily be added at the bottom of the page, overriding any templates the user might add.

For example, you could create an included style sheet that sets the copyright information, called include.xsl:


Listing 9. The included style sheet (include.xsl)
                

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="copyright">
     <p>Copyright 2003, Loopy Foods</p>
</xsl:template>

</xsl:stylesheet>

When this style sheet is included at the end of the main style sheet, it overrides any other templates:


Listing 10. Including the style sheet
                

...
<xsl:template match="product">
    <i><xsl:apply-imports/></i>
</xsl:template>

<xsl:template match="copyright">
    <p><big>Copyright now, by ME</big></p>
</xsl:template>

<xsl:include href="include.xsl"/>

</xsl:stylesheet>

So even though the main style sheet includes a copyright template, the included template is still used:


Listing 11. Inclusion results:
                

...
       Sculpting Contest.  Send your entry today. Contest rules are on the
       back of every box of <i><b>Pop's Homemade Mashed Potato Mix</b></i>.
    </p>

<p>Copyright 2003, Loopy Foods</p>

</body>
</html>

Remember that the precedence is a matter of position; if the style sheet (include.xsl) had been included at the start of the main style sheet rather than the bottom, the copyright template in the main stylesheet (style.xsl) would have been used instead.


Summary

In addition to the basic rules about imports and includes, XSLT allows fine-grained control over which template is applied using the precedence attribute. With judicious use of all these techniques, you can enable your users to customize specific portions of a style sheet while still maintaining control over the rest.


Resources

About the author

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, Florida, USA, 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

If you don't have an IBM ID and password, register here.


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. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. 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=12184
ArticleTitle=Tip: Use imports and includes to override XSLT templates
publish-date=11012002
author1-email=nicholas@nicholaschase.com
author1-email-cc=

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).