What Is XSLT?

Extensible Stylesheet Language Transformations or XSLT is a language that allows you to transform XML documents into XML, HTML, XHTML, or plain text documents. It relies on a companion technology called XPath. XPath helps XSLT identify and find nodes in XML documents; nodes are things like dements, attributes, and other objects in XML. With XSLT and XPath, you can do things like transform an XML document into HTML or XHTML so it will easily display in a web browser; convert from one XML markup vocabulary to another, such as from Docbook to XHTML (see www.docbook.org); extract plain text out of an XML document for use in some other application, like a text editor; or build a new Spanish language document by pulling and repurposing all the Spanish text from a multilingual XML document. This is only a start of what you can do with XSLT.

How Does XSLT Work?

The quickest way to get you acquainted with how XSLT works is through a simple example. Consider this ridiculously brief XML document contained in a file I'll call msgxml:

There isn't much to this document, but it's legal, well-formed XML: just a single, empty element tag with no content (that is, nothing between a pair of tags). For our purposes, its the source document for the XSLT processing we'll do in a minute. Now you can use the very simple XSLT stylesheet msg.xsl to transform msg.xml:

The attribute xmlns on stylesheet is a special attribute for declaring a namespace. It's value is http://www.w3.org/999/XSL/Transform, which is the official namespace for XSLT. An XSLT stylesheet must always have such a namespace declaration in order for it to work. (XSLT stylesheets usually use the xsl pre fix, as in xsl.:stylesheet, but I am setting the prefix aside for simplicity at the moment. You'll want to use xsl when your stylesheets get only slightly more complex.)

The stylesheet element is followed by the output element which is optional. The value text for the method attribute signals that you want the output of the stylesheet to just be plain text:

Two other possible values for method in XSLT 1.0 are xml and html. (The output element actually has ten attributes, all of which are optional.)

The next element in msgxsl is the...

To continue reading