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: Convert attributes with XSLT

Templates are not for elements only

Benoit Marchal (bmarchal@pineapplesoft.com), Consultant, Pineapplesoft
Benoit Marchal is a Belgian consultant. He is the author of XML by Example, Second Edition and other XML books. Benoit is available to help you with XML projects. You can contact him at bmarchal@pineapplesoft.com or through his personal site at marchal.com.

Summary:  Templates are useful for breaking stylesheets into small, manageable units. Another benefit of templates is that the processor selects the most appropriate one automatically, based on the matching condition. Most templates match on elements, but as Benoit Marchal demonstrates in this tip, a template can match on attributes as well.

View more content in this series

Date:  09 Oct 2003
Level:  Introductory

Comments:  

Classes and methods are the tools that object-oriented programmers use to organize a large application into smaller, more manageable units. XSLT programmers use templates for the same purpose. The processor hands control to a template when its pattern matches the current node.

This pattern matching mechanism is particularly well suited to processing trees (like XML documents). It leads to a declarative style of programming because it saves having to write all the tree walking and tree manipulation routines. Instead, the processor walks the input document and automatically selects the most appropriate template for the nodes.

Pattern matching is particularly handy when you don't know in which order the elements appear in the input document or whether they are repeated. Again, the processor takes care of the repetition by calling your template repetitively.

Testing for attributes

Most templates are written to match elements. When a stylesheet processes attributes, the rule is often attached to a template matching an element, as in Listing 1 (all examples in this article are excerpted from a DocBook-to-HTML stylesheet):


Listing 1. Processing an attribute attached to an element
                
<xsl:template match="ulink">
   <a href="{@url}"><xsl:apply-templates/></a>
</xsl:template>

This template transforms the ulink element and url attribute, but it matches against the ulink element only.

Matching against the element makes sense if the attribute is required -- in other words, if it must be present. However, if the attribute is optional you'd want to test whether it exists before generating anything in the output. In some cases, you'd want to test through a condition in the pattern, such as in Listing 2:


Listing 2. Testing for an attribute in a template match
                
<xsl:template match="emphasis[@type='bold']">
   <b><xsl:apply-templates/></b>
</xsl:template>


Matching on attributes

Testing for attributes is not always practical. Take a look at the markup for an image -- imagedata in DocBook. imagedata has optional width and depth attributes that you might want to save in HTML. It would be too difficult to code for all the possible combinations (imagedata[@width], imagedata[@depth], and imagedata[@width and @depth] -- that's three templates for only two attributes). It's more sensible to let the processor walk through the attribute nodes and write templates against them, as shown in Listing 3:


Listing 3. Templates matching attributes
                
<xsl:template match="imagedata">
   <img><xsl:apply-templates select="@*"/></img>
</xsl:template>

<xsl:template match="@width">
   <xsl:attribute name="width">
      <xsl:value-of select="."/>
   </xsl:attribute>
</xsl:template>

<xsl:template match="@depth">
   <xsl:attribute name="height">
      <xsl:value-of select="."/>
   </xsl:attribute>
</xsl:template>

<xsl:template match="@fileref">
   <xsl:attribute name="href">
      <xsl:value-of select="."/>
   </xsl:attribute>
</xsl:template>

Note that by default, the processor does not walk the attribute nodes. You have to ask for that explicitly with a select="@*" attribute.

Two important differences between templates matching elements and templates matching attributes are:

  • Attributes are leafs. An xsl:apply-templates instruction gets you nowhere because attributes have no children for the processor to walk through. Use xsl:value-of instead.
  • Most attributes in the input are also attributes in the output. Use xsl:attribute to insert an attribute in the output tree. Be careful, though -- xsl:attribute must appear before any other children. In other words, the xsl:apply-templates instruction must come before any text or child elements in the caller.

No limits to the templates

Matching on attributes gives you the full expressiveness of XSLT. Templates are not limited to turning an attribute into another attribute. You can add conditions to the pattern or turn attributes into elements. The template in Listing 4 defines a special rule to match fileref as an attribute of videodata. It creates an element instead of an attribute.


Listing 4. Matching on an attribute with a condition
                
<xsl:template match="videodata/@fileref">
   <param name="src" value="{.}"/>
</xsl:template>

Also, in the caller you can apply templates to attributes as well as the element content with a select="@* | node()" attribute, as in Listing 5:


Listing 5. The calling template processes elements and attributes
                
<xsl:template match="para">
   <p><xsl:apply-templates select="@* | node()"/></p>
</xsl:template>

This tip is a practical example of a more generic rule in XSLT coding: To make your stylesheet more maintainable, it pays to divide the work among several templates.


Resources

About the author

Benoit Marchal

Benoit Marchal is a Belgian consultant. He is the author of XML by Example, Second Edition and other XML books. Benoit is available to help you with XML projects. You can contact him at bmarchal@pineapplesoft.com or through his personal site at marchal.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=12326
ArticleTitle=Tip: Convert attributes with XSLT
publish-date=10092003
author1-email=bmarchal@pineapplesoft.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).