Chapter 5.  The XSLT stylesheet

Prev don't be afraid of buying books Next

Chapter 5. The XSLT stylesheet

There are two births; the one when light

First strikes the new awaken'd sense;

The other when two souls unite,

     And we must count our life from thence:

When you loved me and I loved you

Then both of us were born anew.


This is the largest chapter of the book, and for a reason: We now come to the implementation of all the magic that was hinted at by our site's source definition. You have your source nicely marked up, but it is of little use if there's no software to understand this markup. This chapter (which could just as well be named after the entire book, XSLT 2.0 Web Development ) shows you how to build such software with XSLT.

Compared to some other languages, XSLT is simple and logical, so if you have at least some programming experience, perhaps all you need to learn basic XSLT is examples. There are many of them in this chapter, and if you need more, look into the specification [1] (those green boxes are fascinating reading!).


I'll try to cover as many interesting and nontrivial XSLT bits as possible; it does not mean, of course, that your stylesheet will need all of them. Most examples are simple and rarely require deep knowledge of XSLT. What you do need to know before you are able to read this chapter's examples is XPath. If you can parse the content of the numerous select attributes of a typical stylesheet, the rest of the code is in most cases self-explanatory thanks to the clear element type names and the nesting structure of XSLT instructions.

Focusing on what's important. For the sake of clarity, I have cut some material from the stylesheet examples. Literal result elements (which are mostly HTML layout constructs) are often dropped or replaced with comments, making the remaining XSLT code easier to understand. At first, we'll focus on the high-level structure of the stylesheet and the general principles of clean and modular XSLT processing; a complete working stylesheet example is given in the last section ( 5.7 ).

Not only XSLT. Even with all the glory of XSLT 2.0, some neat things are only possible if you write your own extensions in a different language. This is simpler than you may think. This chapter presents several extension functions written in Java for the Saxon XSLT processor. You can easily adapt them for other Java-based processors supporting custom extensions ( 6.4.1 ).

Does this mean you have to know Java? Not really. When you program in XSLT, the extension language you use, be it Java or something else, may well be a black box for you. If you need the functionality described in this chapter, you can copy-and-paste its Java examples [2] they will work as advertised. If you want more, find a Java expert or search for free code on the Webmost likely, your needs will not exceed a couple of dozen lines of code.

[2] All code from this book is also available online at


XSLT 2.0 Web Development
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2006
Pages: 90

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: