Rapid XSLT Help Sheet
Quick Introduction to XSLT

If you're new to XSLT, then this very brief overview will give you all the information you need to get started.

XSLT allows you to restructure and format an XML document however you want.  All you need to know beforehand is exactly what it is that you want to do with the XML document.  Actually translating your idea into XSLT code will quickly become intuitive as you become familiar with the language.

In order to carry out transformations, each XSLT element is responsible for a particular function.

 

What you should do… 


Example

Say you have an XML file that contains numerous news_item elements.  Each news_item element has within it various sub-elements that contain information about that news item such as when it was posted, who it was posted by and its body.  How would you go about looping through all the news_items and displaying them in a neat way?

The element we want to use is xsl:for-each.  There are two things that we need to know: 1. The path to the element name we want to search for and 2. What to do once we find that element.

The path to the element name takes the following form and it represents the location of the desired element with respect to the document's first or root element: sports_news/news_item

Now, onto the code: 
 

<xsl:for-each select="sports_news/news_item">
     <b>Posted by <xsl:value-of select="author"/></b><br/>
     <xsl:value-of select="body"/><br/><br/>
</xsl:for-each>

In this example, we assume that news_item has sub-elements author and body.

In the first line we tell the parser to look for the element at the given path.  After it comes across one, it executes the two lines between its tags.  What we're using here is yet another XSLT element called xsl:value-of that simply pulls the text between an element's tags.  So if the first news_item's author has a value of Jon Smith, <xsl:value-of select="author"> returns Jon Smith and outputs it.

For completion, let us tell the parser that we want to look through the entire XML document.  We do that by wrapping our code with a xsl:template and telling it to match from the root downwards.  We denote the root using the "/" character.  So our final code looks like this:
 

<xsl:template match="/">
     <xsl:for-each select="sports_news/news_item">
          <b>Posted by <xsl:value-of select="author"/></b><br/>
          <xsl:value-of select="body"/><br/><br/>
     </xsl:for-each>
</xsl:template> 

At the end of the transformation we may have something that looks like the following assuming that the XML document has only three news_items in it:

 

Posted by Jon Smith
Norwich beat Manchester United today with a surprise 2-0 win at Carrow Road.

Posted by Alex
Bolton moved to fifth place above Liverpool after its 2-1 win victory at Charlton.

Posted by Mark
Joes Edge and Keith Mercer won a thrilling Scottish National at Ayr when they just pipped Cornish Rebel to deny jockey Ruby Walsh a clean sweep of National wins.