Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596009748/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
ExtensibleStylesheet Language Transformations (XSLT) is a powerful technology for transformingXMLdocuments into other useful forms, butit is sometimes considered difficult to learn. Its template-basedapproach makes it a prime candidate for learning by example, and XSLTexamples are often easily repurposed. XSLT 2.0 greatly increases thepower and elegance of XSLT but also increases its complexity.
When I first began working with XSLT (and again when learning XSLT2.0), I longed for a cookbook that would accelerate my productivityby providing ready-made solutions to the challenges I faced. My firstexperience with such a book wasO'Reilly's PerlCookbook . This book was more influential to my reluctantlearning and ultimate appreciation of Perl than the original camelbook ( Programming Perl ) by LarryWall. I believecookbooks are important because most software developers are notsatisfied with simply figuring out how to make something work: theyare interested in mastering the technology and using the best-knowntechniques, and they want answers fast. There is no better way tomaster a subject than by borrowing from those who have alreadydiscovered better ways to do things.
Longing for a cookbook soon turned into a desire to write one,especially since I collected several useful recipessome thatwere developed by others and some that I created. However, I did notwant to write an XSLT book simply packaged in an alternate form; Iwanted to provide a useful resource that also highlighted someless-obvious ways to apply XSLT. In the process, I hoped to attractXML developers who have not yet been motivated to learn XSLT and who,in my opinion, are missing out on one of XML's bestproductivity tools. If you are one of these folks who has not yetexperienced XSLT, please bear with me for a few more paragraphs whileI pitch the value of XSLT and the role of this book in helping yourealize its potential.
XSLT is a language that lives simultaneously on the fringes and inthe mainstream of current software-development technology. Whileworking on the first edition of this project, I often found myselfexplaining to friends what XSLT was and why it was important enoughto spend time writing a whole book about it. These same friends hadheard of Java, Perl, and even XML, but not XSLT. I also observed anincreasing number of requests for XSLT assistance on XSLT mailinglists and more industry attention in the form of books, articles, andsophisticated XSLT development tools. The XSLT user base is clearlygrowing daily; however, many software professionals and technologyenthusiasts do not understand what it is and why it is important.With the release of new XSLT 2.0 implementations, I hope adoption ofXSLT will accelerate, but this is not certain, partly due tocompetition from XQuery 1.0 and other XML manipulation methodologies.One thing is certain: mastering XSLT 2.0 is a worthwhile endeavorbecause its use will certainly increase, even if it never explodes.Further, learning XSLT will give you a deeper insight into XMLprocessing even if you favor an alternative solution.
Although XSLT 1.0 is a mature language and XSLT 2.0 is not farbehind, I would still guess that more that half of all companies andindividuals working with XML do not use XSLT. Not so long ago, acolleague who is otherwise well-versed in all the latest technologiesdescribed XSLT as just another styling language. Thismisunderstanding is forgivable because XSLT advertises itself throughthe first three words in its name (Extended Stylesheet Language) andwith the keyword that begins most XSLT programs(xsl:stylesheet
). However, the last word in theXSLTacronym, Transformations , is what makes XSLT soimportant and is what drew me to the language in the first place. Oneof my goals in writing this book is to show how XSLT is relevant to awide variety of problems. I also want to provide both novice andintermediate users of XSLT a one-stop shopping place for some of themost commonly requested XSLT techniques. Finally, I want to push theenvelope of what one can do with XSLT so current users can go evenfurther and the unconvinced can join the fold of highly productiveXML transformers.
Over the years, I have heard many sweeping statements about computerscience. Opinions like, "All computation is simplyfancy bit manipulation," "Computersare really just sophisticated number crunchers," or"Everything a computer does can be understood interms of symbol manipulation" are true to someextent. However, I would like to make a sweeping generalization of myown: "Every problem we solve with software can beunderstood in terms of transformations." Mastery ofcomputer science is mastery of transformation. Transformation is whatCPUs do, it is what algorithms do, and it is what software developersdo. And transformation is what XSLT does, at least when the input isXML (and sometimes when it is not). Of course, XSLT is not the onlytransformational game in town, and as with the thousands of languagesthat came before it, it is unclear whether it will evolve as anindependent language or be absorbed into the next"big thing." What is clear is thatthe ideas behind XSLT will not go away because many of these ideasare as old as computer science itself. This book helps the readermaster and apply these ideas to specific problems.
Structure of This Book
To make this book useful to the broadest possible audience, I haveretained most of the XSLT 1.0 solutions presented in the firstedition. To these, I have added XSLT 2.0 solutions when 2.0 provideda significantly simpler or more elegant solution to the same problem.I also occasionally show 2.0 solutions to problems that would havebeen next to impossible to solve in 1.0. I have used separatesubheadings to distinguish the 1.0 solution from the 2.0 solution. Ihope this makes it easy for readers interested in one or the other toeasily find what they are looking for. In a number of recipes, I donot provide a special 2.0 solution. Most of the time, this wasbecause I felt the 1.0 solution would work as well in 2.0 or becauseI felt a 2.0 solution was obvious or would add very little value. Isincerely hope my desire to save trees and time does not overlyfrustrate the reader in this regard.
Both XSLT 1.0 and 2.0 rest firmly on the foundation provided by XPath1.0 and 2.0, respectively. Some readers of the first edition took meto task for my lack of direct coverage of XPath. was created partly to appease them andpartly in response to the greater sophistication and complexity ofXPath 2.0.
One of transformation's most primitive forms is itsprocessingshows that almost anything one wants to do with strings can be donewithin the confines of XSLT and then shows how the new features of2.0 make it that much easier.