XSLT: Extensible Stylesheet Language for Transformations


You now have a web service that accepts requests from the user and returns responses formatted in XML. This is useful for clients that wish to retrieve and process content using a program other than a web browser. It might also be helpful to provide HTML-formatted responses to users who wish to use our service as a typical web site. XSLT enables us to write an XML document that defines rules (fairly powerful rules) for transforming our XML result document into HTML. You can also use XSLT to transform XML into XML, or text, or even a format like PDF.

Transforming Content

To transform an XML document into another format, you must pass the source document and the XSLT document to a transformation engine. An XSLT transformation engine applies the rules defined in the XSLT document, and returns the result. The engine works by traversing the source tree and, at each node, determining whether any of the rules in the XSLT document apply. If a rule applies, it spits out the appropriate content.

There are several transformation engines available. For example, you can do transformations via javascript. Java also provides a transformation engine. For the purposes of your next lab, you will use the org.restlet.Transformer class. The Transformer is a Filter that you can apply to resulting resource representations. You'll likely want to associate it with your router in the createRoot method of your main application. However, to debug your stylesheets, you can use the command line transformation engine xsltproc or /usr/bin/4xslt on the lab machines.

Creating Stylesheets

XSLT provides many of the same features you see in other programming languages such as branching (xsl:if) and iteration (xsl:for-each). We'll look at several examples, many inspired by code from here, that show how to use different XSLT elements. Note, you may look at these examples as you work on your lab, but you must also acknowledge any of your sources.

The following examples can be applied to this XML document: schedule.xml

Example 1

Example 2

Example 3

Example 4

Example 5

Example 6

Sami Rollins
Wednesday, 07-Jan-2009 15:13:20 PST