I recently had a reason to use Extensible Stylesheet Language Transformations (XSLT) on a project and thought I would give a little tutorial and share some information on the topic.
XSLT is a templating language used to create and transform XML documents, similar to the way that CSS files are used to transform HTML documents. A notable difference, however, is that HTML tags are defined by the standard.
When I want my CSS to target an HTML <span> tag, the CSS knows what to look for because a <span> tag will always look the same. In XML, tags are not predefined. That is to say, I can create an XML tag with whatever name I choose. This forces XSLT to be more dynamic, and it achieves this dynamism through a tool called XPATH, which is used to traverse elements and attributes in XML.
In short, templates are created using XPATH that will match parts of the source XML document. When it finds a match, XSLT will change the matched section of the source XML into your destination XML or XHTML document.
Now that you have a basic understanding of the concept behind XSLT, let’s get into a brief example. Let’s say you have some software that tracks a library of books. This software can give you an XML output of the library.
You want to transform that XML dump into a format that can be published on your website. We can use XSLT to accomplish this fairly easily. A sample of the XML output that we have looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?> <library> <book> <title>Enders Game</title> <author>Orson Scott Card</author> <yearpublished>1985</yearpublished> </book> <book> <title>Watchmen</title> <author>Alan Moore</author> <yearpublished>1987</yearpublished> </book> </library>
The first thing to note is how to declare an XSL style sheet:
<xsl:stylesheet version="1.0" >
Putting this declaration at the top of our document will give access to the necessary XSLT features. Then we will create an XSL Style Sheet to transform our XML into XHTML:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" > <xsl:template match="/"> <html> <body> <h2>Nick's Book Collection</h2> <table> <tr> <th>Title</th> <th>Author</th> <th>Year</th> </tr> <xsl:for-each select="library/book"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="author"/></td> <td><xsl:value-of select="yearpublished"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
This creates a fairly simple template that contains a foreach loop that will iterate each <book> tag, retrieving the title, author, and year published values while placing them into HTML <table> rows. The only thing left would be to link to your XSL stylesheet in your XML document with a tag that looks something like this:
<?xml-stylesheet type=”text/xsl” href=”xslstylesheet.xsl”?>
Now when the browser loads your page, it will be a nicely formatted XHTML document.
I hope this beginner example is helpful. There is a lot more to XSLT and it can do some surprisingly powerful stuff. This post should help you get a basic understanding of how it works so that you can start transforming your XML however you need.