XSLT

XSLT

XSLT stands for Extensible Stylesheet Language Transformations
XSLT is used to transform XML documents into other kinds of documents--usually, but not necessarily, XHTML
XSLT uses two input files:
The XML document containing the actual data
The XSL document containing both the “framework” in which to insert the data, and XSLT commands to do so

How XSLT works

The XML text document is read in and stored as a tree of nodes
The template is used to select the entire tree
The rules within the template are applied to the matching nodes, thus changing the structure of the XML tree
If there are other templates, they must be called explicitly from the main template
Unmatched parts of the XML tree are not changed
After the template is applied, the tree is written out again as a text document

Where XSLT can be used

With an appropriate program, such as Xerces, XSLT can be used to read and write files
A server can use XSLT to change XML files into HTML files before sending them to the client
A modern browser can use XSLT to change XML into HTML on the client side
This is what we will mostly be doing in this class
Most users seldom update their browsers
If you want “everyone” to see your pages, do any XSL processing on the server side
Otherwise, think about what best fits your situation

Modern browsers

Internet Explorer 6 best supports XML
Netscape 6 supports some of XML
Internet Explorer 5.x supports an obsolete version of XML
IE5 is not good enough for this course
If you must use IE5, the initial PI is different (you can look it up if you ever need it)

Modularization

Modularization--breaking up a complex program into simpler parts--is an important programming tool
In programming languages modularization is often done with functions or methods
In XSL we can do something similar with xsl:apply-templates
For example, suppose we have a DTD for book with parts titlePage, tableOfContents, chapter, and index
We can create separate templates for each of these parts

xsl:apply-templates

The element applies a template rule to the current element or to the current element’s child nodes
If we add a select attribute, it applies the template rule only to the child that matches
If we have multiple elements with select attributes, the child nodes are processed in the same order as the elements

When templates are ignored

Templates aren’t used unless they are applied
Exception: Processing always starts with select="/"
If it didn’t, nothing would ever happen
If your templates are ignored, you probably forgot to apply them
If you apply a template to an element that has child elements, templates are not automatically applied to those child elements

Thoughts on XSL

XSL is a programming language--and not a particularly simple one
Expect to spend considerable time debugging your XSL 
These slides have been an introduction to XSL and XSLT--there’s a lot more of it we haven’t covered
As with any programming, it’s a good idea to start simple and build it up incrementally: “Write a little, test a little”
This is especially a good idea for XSLT, because you don’t get a lot of feedback about what went wrong write (or change) a line or two, check for syntax errors, then jump to IE and reload the XML file.