XSLT Powers a New Wave of Web Applications

Cameron introduces XSLT and shows why it's such a hot topic in application development.

Extensible Stylesheet Language for Transformations (XSLT) is a computing language specialized for mapping XML documents into other XML documents.

Explanation of XSLT is no small ambition. The problem has to do with variety; there are many uses for XSLT, many instances of XSLT engines and many cooperating technologies involved in XSLT application, so it's important to focus on the essentials.

Universal XML

The first XSLT essential is its Extensible Markup Language (XML) base (also see the “Glossary of XSLT Terms” Sidebar). XML is the universal data format designed to encode everything: algorithmic data, programs and documents from purchase orders to biblical translations, in any human language, on any kind of computer and operating system. XML looks like HTML except it's a bit more complicated. In fact, one of XML's design goals is to generalize HTML in a way that preserves the comfort of HTML adepts. There's even a flavor of XML called XHTML that permits direct interpretation as HTML. Linux Journal frequently publishes articles on different aspects of XML.

A fully XML-ized world is a simpler one, in many ways. To analyze the operation of an accounts payable department, for example, you don't need to know who reports to whom, who is due for a three-week vacation and all those other messy human details. If you can draw a diagram that shows invoices coming in and payments going out, perhaps with authorization records spawned along the way, then you have abstracted what ought to be the essential information.

This is an intoxicating insight. It promises that a system that can transform one XML document (invoice) into one or more other XML documents (payment check, authorization records) and at least organizes, and possibly solves, all meaningful organization automations. That's why XSLT seems so important now.

Readers with a background in the XML world should generalize their project experience to get a notion of XSLT's true worth. Anyone with practical knowledge of XML knows that it's only the beginning of a solution, not the miracle cure marketing brochures often make it out to be. XSLT is exactly the same: a useful and even powerful way to organize the real work of engineering applications fit for production. The idea of transforming XML documents is an important one; to see whether it's the right idea requires close attention to the technical details.

An Engine of Your Own

To start you on your XSLT career and help you get the proper feel for the language, you'll need an engine, or language processor, of your own. The most widely used are based on Java and/or are proprietary. These often are integrated into larger server products: database servers, application servers and so on.

Rather than any of these, this article presents its examples in terms of the tDOM engine. tDOM has several advantages, among which the most important are that it's available under a liberal open-source license, it's exceptionally thrifty on memory and twice as fast as competing XSLT engines in our benchmarks, its installation is quick and compact and it exposes a scriptable command mode that's convenient for instruction. Moreover, tDOM fits well in the dual-level programming style explained below, and it's robust enough to be in production use at several demanding sites already.

To set up your own copy of tDOM, see the “How to Start XSLT Programming” Sidebar. That Sidebar concludes with a first example of XSLT use, invoked as

tclsh8.3 xslt.tcl example1.xml example1.xsl
example1.html

This command line says, “Use version 8.3 of the Tcl interpreter to launch the xslt.tcl program. The xslt.tcl utility applies the example1.xsl stylesheet to the example1.xml document and produces example1.html as its output.”

Look at this first as a machine that takes example1.xml as its input and produces example1.html, which has only a couple of lines:

<?xml version="1.0"?>
<datum>first message</datum>

Think of example1.html as an expansion of this into well-formatted HTML:

<html><body><h1>first message</h1></body></html>

XML as Data and Code

If all you need is a simple HTML document like example1.html, you can write it directly or use a lightweight macro language, rather than learn XSLT. The value of XSLT begins to appear when you look at more complex examples. You can set up the XSLT transformation to generate example1.html output in a particular style, perhaps with approved fonts or boilerplate site hyperlinks and disclaimers.

XSLT uses the language of stylesheets to specify these transformations. While stylesheets were in use before XSLT's invention, this article ignores other uses and consistently abbreviates XSLT stylesheet as just stylesheet.

On one level, a stylesheet is a program. Just as

int main()
{
    puts("Hello.");
}

is the source for a C program, a stylesheet is the source for an XSLT program. A peculiarity of stylesheets, though, is that they are themselves XML documents. Rather than looking like normal computer programs (in the way C, Java and ksh do, say), XSLT source is a kind of markup text (see Listing 1).

Listing 1. example1.xsl

With a verboseness typical of XML, this says, roughly, “act as a program that pulls out <datum> elements and puts their contents in <h1> headings of well-formatted HTML.” That's how example1.html is generated.

The application that implements this XSLT interpretation is itself a Tcl program. There's little you need to learn about Tcl at this point. tDOM exposes its XSLT engine with Tcl bindings, and the xslt.tcl script simply treats command-line variables as the filenames of XML documents and passes them on to the engine.

Let's review the example invocation. In

tclsh8.3 xslt.tcl example1.xml example1.xsl
example1.html

tclsh8.3 is the name of the executable program we're launching, and xslt.tcl is a minimal Tcl script that wraps the tDOM XSLT engine. If we wanted to improve the error handling of this utility, refinement of xslt.tcl would be the natural place to start.

Running xslt.tcl creates an XSLT processor that receives three filenames. The example1.xml file is a sample XML source document. This file names the stylesheet we apply to example1.xml. The process writes the resulting output document to example1.html. Select different logical contents for example1.html by naming a different XML source, perhaps example2.xml. To change the style of the output, rewrite example1.xsl.

You've now successfully run an XSLT program. All that's left to learn are the details of XSLT as a language and how it's applied to real-world problems. Before more on the syntax and semantics of XSLT, let's look at its uses.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: XSLT Powers a New Wave of Web Applications

Anonymous's picture

While this article is an interesting introduction, I am very surprised that it would manage to not even mention libxslt which is probably one of the fastest, most reliable, and most frequently deployed processors, and AxKit which is similar to Cocoon in various ways, but less overdesigned and implemented within Apache using a powerful combination of C and Perl. Both are extremely succesful projects, with constantly growing user bases.

As for XSLT being more used by Tcl and Java folks than by Perl, Python, and C folks I think that's simply wrong, and is no more than an impression gathered from an incomplete sample.

Finally, as the author mentions benchmarks it would be nice to see them! I have no doubt that tDOM beats any pure Java XSLT processor flat, but what of the others? Beating libxslt (both for memory and for speed) appears to me to be a tough task. Even if tDOM were faster, I very much doubt it would be "twice as fast" as claimed rather boldly in this article.

Mod_xslt - ultimate XSLT engine for the web

Anonymous's picture

Check out http://freshmeat.net/projects/mod_xslt_ln/

and http://linuxnews.pl as an example of XML/XSLT technology.

With the mod_xslt you can for example pass the arguments to the XSLT programs.

Example of this is forum system on Linuxnews.pl

regards,

Pumpkin

mod_xobjex for Apache 1.3.x uses libxslt

Daniel Bibbens's picture

Similar to mod_xslt, mod_xobjex is a lighter, faster alternative that uses Daniel Veillard's libxslt (which is blazingly fast). With mod_xobjex, web applications are built with pure XSLT. mod_xobjex converts the HTTP Request into XML to which the requested XSL template is applied. Download it at xobjex.com.

XSLT, DOM, SQL and the web

Anonymous's picture

This paper may be of interest to those looking to publish data from relational databases on the net using XSLT.

Re: XSLT Powers a New Wave of Web Applications

Anonymous's picture

www.zvon.org hosts an excellent XSLT reference - a lot easier than reading the W3C documents :-)

If you just want to try out some XSLT examples, here's (http://www.julien-dubois.com) a server-side XSLT transformer, so that you don't need anything on your PC to get you started.

Re: XSLT Powers a New Wave of Web Applications

Anonymous's picture

The "How To Start XSLT Programming" link mentions the 0.63 code snap shot. The xslt engine in tDOM-0.7test is much improoved in XSLT compliance. Grab it from

http://sdf.lonestar.org/~loewerj/tdom.cgi

rolf ade

Re: XSLT Powers a New Wave of Web Applications

Anonymous's picture

And for those who want a fast and compliant XSLT
engine, use xsltproc available from the libxslt package on
most distributions. It is used by KDE and Gnome to generate
the HTML version of their DocBook docs.
Check the web page
and download area for further
informations, packages and the source !
Daniel Veillard

Re: XSLT Powers a New Wave of Web Applications

Anonymous's picture

Yes! Daniel Veillard's libraries and utilities are definitely the way to go for production-quality work with XSLT; they chew through huge groves with speed. It's a true open-source success and deserves more appreciation.

If you're interested in tracking XSLT's growth and maturation, check out the newest version of Michael Kay's SAXON - larger, slower, but also very cool.

Paul Ford

Re: XSLT Powers a New Wave of Web Applications

Anonymous's picture

The way I got started with XSLT was Sablotron; which is available here.. If you use perl, it is nice because it's a perl module; and it also has a command line vesion similar to the tcl command mentioned above. You don't need TCL though; just configure && make && make install.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState