Home / Blog / Extensible Stylesheet Language Transformation (XSLT)

Extensible Stylesheet Language Transformation (XSLT)

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"?>
    <title>Enders Game</title>
    <author>Orson Scott Card</author>
    <author>Alan Moore</author>

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="/">
  <h2>Nick's Book Collection</h2>
    <xsl:for-each select="library/book">
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="author"/></td>
      <td><xsl:value-of select="yearpublished"/></td>

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.

2 thoughts on “Extensible Stylesheet Language Transformation (XSLT)”

  1. 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

    Project Manager

    Project Manager

    Project Manager

    Project Manager

    Project Manager

    Project Manager

    Project Manager

    please suggest me wht is template input xml file

Leave a Comment

Your email address will not be published.