This is a simple introduction to XSLT, or eXtensible Stylesheet Language Transformation. It’s a way of applying styles and formatting to XML files, and it’s pretty useful. It’s yet another acronym you see thrown around on job descriptions for Web Developers. Don’t be scared, it’s really easy to understand.
We’ll start out with a simple XML file listing a few movies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
I shouldn’t have to spend a lot of time explaining this file, you should already have a basic familiarity with XML if you want to build XML Stylesheets. This file contains three movies, as part of a “collection” and lists the following attributes:
If we load this file into a browser as it is, the page that displays will look like it does above, just an XML readout. But we added something to our file, notice this line:
Here is where we reference movies.xsl which will be our XML stylesheet. We simply put in an href to the file, and a type.
Our xsl file will be somewhat different, essentially a blend of an XML file and an HTML file. What this sheet is doing is applying presentation to the XML elements. You can build tables, lists or anything else you can do in a standard html webpage. Here is one I built for this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Let’s break down this file a bit. First we find the XML start tag, and then our first XSL tag, which is
This is an important line, because it specifies the version and namespace of the stylesheet. The next important line is line #4:
This specifies which group to start with. Our sample XML file only has one group, “Collection”. But you may have several groups and you can keep them all in one big XML file. You can then use a stylesheet to display them all in different areas, and different ways. We could decide to add a list of favorite TV shows, for instance and put that in another group. This line simply tells us which group this particular template is for.
You’ll notice we start building an HTML page below it, complete with a table to display our data.
Then we have another important line:
This is a basic “for each” loop found in many programming languages. It basically states, “for each movie, we will do ___”. So it creates a loop and goes through every “movie” tag until it’s done. This is how the rows are populated.
After that we close up the table and the html and drop in tags to close the template, and the style sheet:
1 2 3
These are pretty self explanatory, we want to close up any open tags, if you don’t you’ll get an error.
Some things to note:
Your coding has to be really good. - Most of the time just one open tag will break the entire thing.
Not all browsers process XSLT stylesheets, including many mobile devices. You will need to use a XSLT parser for those pages, and I’ll do a tutorial for that soon.
We’re just scratching the surface - XSLT uses XPath to query XML files, there are a lot more cool things you can do with this technology once you get the hang of it. Check out an XPath tutorial and take a look. I’ve seen entire sites built with XML files and XSLT that would surprise you with what they’ve done.
I hope this helps. Till next time, have fun!