ApplicationsBlog

Extensible Stylesheet Language Transformation (XSLT)

By July 23, 2013 2 Comments

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.

Nick Claywell

Nick Claywell

Nick is a Lead Application Developer and has been doing web development for over 10 years. He has worked for Soliant in the Chicago office since 2010. Nick specializes in PHP development and is very familiar with technologies such as PHP, HTML/CSS, JavaScript, and MySQL.

2 Comments

  • Avatar bijay bhushan says:

    i need a urgent help in xslt transform.i have a simple xml file
    what is the template file to transform a xml file.i need to provide a xml template file for that.
    my xsl file is
























    my input xml file is
    < ?xml version="1.0" encoding="UTF-8"?>0:00:00.1047RequisitionXMLhttp://www.taleo.com/ws/tee800/2009/01


    1300000F
    Project Manager


    1300000H
    Project Manager


    1300000T
    Project Manager


    13000018
    Project Manager


    000123
    Project Manager


    1300000R
    Project Manager


    13000016
    Project Manager

    please suggest me wht is template input xml file

  • Avatar James says:

    Thanks for the article
    Regards
    James
    http://www.tilogeo.com

Leave a Reply