Ten years ago, when XSLT was relatively new, I struggled to understand it. It was too difficult to use and too challenging to debug. After I came to understand the strengths and weaknesses of XSLT and—more importantly—its paradigm and how and when to use it, however, XSLT is now a favorite tool that's a pleasure to use. XSLT is elegant and extremely powerful when applied well to a task for which it is suited.
Have you ever used a hammer and screwdriver to loosen a nut from a bolt? You place the screwdriver blade on one side of the hex nut and tap the screwdriver's handle with the hammer. Twist around with the nut as it turns. If it doesn't turn, tap harder. Now consider being given a socket wrench. It looks a little like a hammer, but it's not as effective as using an actual hammer. So, put down the hammer and screwdriver, and place the socket over the nut and turn. Unfortunately, the socket is too large, so you struggle to get it to grip. Eventually, you give up and go back to the hammer.
The key to success with a socket wrench is pattern matching: The socket must match the nut. When it does, a socket wrench is much more effective at loosening a nut from a bolt than a hammer. XSLT also depends on patterns: It's a pattern-matching language, whereas the Java™ and C# languages are imperative languages. If XSLT is a socket wrench, imperative languages are hammers. Each has its use.
XSLT is a programming language
Web development requires a mastery of several very different languages. The first, of course, is HTML—a markup language. Then, add CSS and JavaScript on the front end; server scripts such as PHP or Microsoft® ASP.NET; and a back-end language such as Java, C#, or Microsoft Visual Basic.NET. Finally, at the database level, is SQL. If you know SQL, XSLT is easier to learn because both are declarative languages.
If you're like most programmers, you started your career with an imperative language
like C++. These languages are coded by command,
and functions perform the actions. Their names take the form
verb-noun—for example, GetValue
or UpdateContactInfo. Declarative languages are
less about issuing commands and more about stating (or declaring) the desired
output. Table 1 shows several common programming languages categorized as either imperative or declarative.
Table 1. Programming language styles
| Imperative | Declarative | ||
|---|---|---|---|
| Procedural | Object-oriented | Functional | Logic |
| C, Pascal | Java, C++ | LISP, XPath | Prolog, SQL, XSLT |
The Zen of XSLT: pattern matching
XSLT is a declarative language for transforming XML to XML, HTML, or plain text. The transform occurs by matching patterns in the input XML that selects a template with the content to output. The key is pattern matching, as in Figure 1. Instead of coding commands, you code patterns. Some patterns match the input, and other patterns act as templates for the output.
Listing 1 shows a simple XSLT template that matches <item>
elements.
Listing 1. Example XSLT template
<xsl:template match="item">
<li><a href="{link}"><xsl:value-of select="title"/></a></li>
</xsl:template>
|
The match attribute specifies the pattern to match the
input—in this case, an <item> tag.
The markup between the <xsl:template> tags
is the output template. The template has placeholders where values from the
input are substituted. In Listing 1, the value of the
<link> element provides the URL to the
href attribute, and the value of the
<title> element is the visible text of the
hyperlink.
Often, as a developer, you are given a task, such as "display this RSS feed as a list of links." Figure 1 shows how you might envision the look of the final product.
Figure 1. Rendered output
To start coding now, however, would result in frustration, like using a socket wrench with the wrong-sized socket. Before you begin, ask yourself two questions:
- What do I have (input)?
- What do I want (output)?
Determining the input for your task
When answering the question of input, consider the following queries:
- What does the XML look like?
- What are the elements?
- Which are repeatable?
- What is the structure?
- What are the attributes?
- What are the namespaces?
Figure 2 shows common terms in XML structure.
Figure 2. Common XML terms
Get a sample of the input XML. If available, get the schema, too. The schema provides information on all the possible tags, attribute values, and the structure of the XML document, including optional elements and attributes. The sample input document is essential to coding the matching patterns and testing and debugging your XSLT. For example, consider the IBM® developerWorks® technical library RSS feed in Listing 2.
Listing 2. Sample input XML document
<?xml version="1.0"?> <rss version="2.0"> <channel> <title>IBM developerWorks : Technical library</title> <link>http://www.ibm.com/developerworks/</link> <description>The latest content from IBM developerWorks</description> <pubDate>27 Nov 2010 16:48:48 +0000</pubDate> <language>en</language> <copyright>Copyright 2004 IBM Corporation.</copyright> <image> <title>developerWorks</title> <url>http://www.ibm.com/developerworks/i/dwlogo-small.gif</url> <link>http://www.ibm.com/developerworks/</link> </image> <item> <title><![CDATA[IBM Cognos Proven Practices: Enterprise Planning - User Lockdown in DB2]]></title> <description><![CDATA[This document describes methods to limit database permission in DB2 for IBM Cognos Planning 8.]]></description> <link><![CDATA[http://www.ibm.com/developerworks/data/ library/cognos/infrastructure/databases/page535.html?ca=drs-]]></link> <pubDate>25 Nov 2010 05:00:00 +0000</pubDate> </item> <item> <title><![CDATA[Monitoring in DB2 9.7, Part 2: Relational access to XML event monitor data in DB2 9.7]]></title> <description><![CDATA[Learn how you can get relational access to the subset of monitor data that is captured in the form of an XML document by an activity or statistics event monitor in IBM DB2 for Linux, UNIX, and Windows Version 9.7 (DB2).]]></description> <link><![CDATA[http://www.ibm.com/developerworks/data/ library/techarticle/dm-1011db2mpart2/index.html?ca=drs-]]></link> <pubDate>24 Nov 2010 05:00:00 +0000</pubDate> </item> </channel> </rss> |
With a good idea of your input, you can turn your mind to what you want the finished product to look like. Ask yourself these questions:
- Which format should the output be in (XML, HTML, text)?
- What are the tag names and attributes?
- What is the structure?
XSLT transforms one or more XML documents into one of the following formats:
- HTML
- XML, different vocabulary
- XML, same vocabulary (use identity templates)
- XHTML (XML with special considerations)
- XSLT (XSLT is an XML vocabulary but requires the use of namespace-aliases)
- Text (for example, JavaScript)
When designing the output markup, start with a vision of how it should look, then take the next step of crafting the markup. For example, Listing 3. shows the HTML markup for a list of hyperlinks.
Listing 3. The desired output
<html> <head><title>the title</title></head> <body> <ul> <li><a href="some-url">hyperlink text</a></li> <li><a href="some-url">hyperlink text</a></li> </ul> </body> </html> |
With the input and output questions answered, you're ready to write the XSLT. If
most of your experience has been with common programming languages, you
might be tempted to think, "Okay, I'll build the heading, write a
for loop to produce the list, and then build the
footing." Instead, think of the pattern. The input
<item> elements match the output
<li> elements. Within each
<item>, the
<link> element matches the
<a href> attribute, and the
<title> element matches the text between the
<a> tags. Now you are ready to create the
XSLT. Perform these steps:
- The output is HTML, so include the
<xsl:output>element to specify HTML. - Add the root template—that is,
<xsl:template match="/">—and fill it with the pattern of the HTML document, including both the header and the footer. - Place the
<xsl:apply-templates>element where the list belongs. - Code a template to match the
<item>elements to output the<li>element.
Listing 4 shows the complete XSLT.
Listing 4. The complete XSLT
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head><title>Zen of XSLT</title></head>
<body>
<ul>
<xsl:apply-templates select="rss/channel/item"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<li><a href="{link}"><xsl:value-of select="title"/></a></li>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
|
The key to coding XSLT is to think in terms of pattern matching rather than calling functions. Two key questions must be answered:
- What do I have?
- What do I want?
Upon answering these questions, you can write the XSLT templates to match the input and output. Otherwise, you might feel as if you are using a socket wrench as a hammer hitting the handle of a screw driver to loosen a nut and bolt.
Learn
- New to XML?: Get the resources you need to learn XML.
- Extensible Stylesheet Language Transformations (XSLT): Transform XML documents to different forms by the W3C (developerWorks, April 2007): Learn more about XSLT.
- What kind of language is XSLT? (Michael Kay, developerWorks, April 2005): Put XSLT in context as you learn where the language comes from, what it's good at, and why you should use it.
- How an XSLT processor works (Benoît Marchal, developerWorks, March 2004): Compare XSLT with other JSP, PHP, and other web development languages
- XML Path Language (XPath): Create expressions relating to portions of an XML document by the W3C (developerWorks, April 2007): Learn more about the XPath standard.
- Putting XSL transformations to work (Mark Colan, developerWorks, October 2001): Look at several real-world business scenarios that benefit from the use of XSL transformations.
- Tip: How to combine documents with XSLT (Benoît Marchal, developerWorks, May 2003): Explore XPath functions and learn how to combine documents with XSLT.
- XML area on developerWorks: Get the resources you need to advance your skills in the XML arena.
- My developerWorks: Personalize your developerWorks experience.
- IBM XML certification: Find out how you can become an IBM-Certified Developer in XML and related technologies.
- XML technical library: See the developerWorks XML Zone for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks. Also, read more XML tips.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
- developerWorks on Twitter: Join today to follow developerWorks tweets.
- developerWorks podcasts: Listen to interesting interviews and discussions for software developers.
- developerWorks on-demand demos: Watch demos ranging from product installation and setup for beginners to advanced functionality for experienced developers.
Get products and technologies
- IBM product evaluation versions: Download or explore the online trials in the IBM SOA Sandbox and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
Discuss
- XML zone discussion forums: Participate in any of several XML-related discussions.
- The developerWorks community: Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups, and wikis.
Doug Domeny has developed a browser-based, multilingual, business user-friendly XML editor written using XSLT, W3C XML Schema, DHTML, JavaScript, jQuery, regular expressions, and CSS. Holding a bachelor's degree in computer science and mathematics from Gordon College in Wenham, MA, Doug has served for many years on OASIS technical committees such as XML Localization Interchange File Format (XLIFF) and Open Architecture for XML Authoring and Localization (OAXAL). In his roles as a software engineer, he has developed significant skills in software engineering and architecture, UI design, and technical writing.




