Project Advice

Project Advice

4. Presenting XML with stylesheets

At the very beginning of this course we looked at HTML, which browsers know how to display by default. When you open an XML file in a browser, however, it knows nothing about how to display the text, and tells you so:

browser XML display message


The way to redress this problem is to link your XML file to a stylesheet. For XML this is part of a family called XSL (Extensible Style Sheet Language). The most important members for our purposes are XPath and XSLT (XSL Transformations).

We can’t go into the syntax of XPath and XSLT here; they are a large topic in themselves. But they constitute a powerful and widespread tool for working with XML documents, so it will be useful to outline what they do and how they work.

XPath is a way of navigating an XML document. Earlier, when using the terms child, ancestor, etc. to describe elements the metaphor of a family tree came readily to mind. XPath takes this idea of a tree and expands it a bit to that of a node tree. A node is, if you like, the equivalent of a person on a family tree. Elements are nodes of the tree but so are attributes and text. In other words everything in an XML document is part of the node tree.

XSLT uses the functionality of XPath to select nodes and then transforms them. A very common thing is to wrap some HTML around whatever you have selected so that it can be presented on the web. For example you might have an instruction in your XSLT to find all the text within a particular element, then output:


The text that you selected


Iterated across the whole document, and across every element you wish to output, this will style the document in whichever ways you like.

This method keeps the text and its presentation wholly separate. As a simple example, you may wish to download an XML file and an accompanying XSL document that we have produced. You'll recognise the XML as the Parish Clerk's Memoranda from earlier in the module. Below the XML declaration we have simply added a link to the XSL file, so if you save them both to the same directory or folder and open the XML file with a browser you will see how it is transformed. We've done three things here:

1. Introduced a very small amount of HTML styling (a background colour) to show how this can be done.

2. Output a list of all the names and dates in the document (in the order in which they appear, including duplicates).

3. Following the lists we've output the content of all the paragraph tags, so that you can see all the running text, without things like the folio numbers.

Exercise. We've kept this intentionally simple, so that you can see how each part works. Try modifying the XSL file to output, say, a list of dates, or to add more HTML styling. If your file is invalid the browser will soon tell you.

XSLT doesn’t just transform documents into HTML. It can transform them into other XML documents or many other formats.

You can find many XSLT tutorials on the web, but if you’re keen on learning it a book may be a better bet. Beginning XSLT, by Jeni Tennison, is one that we have found useful. Tennison also has some web pages on the basics of XSLT that you may find useful.