Preface

Extensible Stylesheet Language Transformations (XSLT) and its companion, the XML Path Language (XPath), are arguably the two most widely used XML-related specifications to come out of the World Wide Web Consortium (W3C) since XML 1.0 (http://www.w3.org/TR/REC-xml.html).

XSLT 1.0 (http://www.w3.org/TR/xslt) and XPath 1.0 (http://www.w3.org/TR/xpath) appeared as W3C recommendations in November 1999, about a year and a half after XML. While XSLT and XPath have detractors, they are generally well-accepted in the XML community. One reason why is that XSLT is a relatively easy-to-learn, declarative language. As a declarative language, XSLT relies on an underlying implementation in a programming language such as Java or C++ to get its work done. This book intends to get you doing useful work with XSLT the same day you start reading it.

Who Should Read This Book?

This book is for anyone who wants to get up to speed quickly with XSLT. It is designed around over 200 XML and XSLT examples files—nearly every XSLT feature that this book explores, in fact, is demonstrated by an example that you can run through yourself with the XSLT processor of your choice (Apache’s Xalan C++ processor is used with most examples; see http://xml.apache.org). It doesn’t matter if you’re an XML neophyte or a seasoned programmer, this book is designed to help make your learning fast-paced and rewarding.

About the Examples

As a writer, I have labored for about 20 years under the assumption that we all learn best by doing. That’s why this book is heavily laden with hands-on examples. All the examples in this book, except for an occasional fragment, are available for download from http://www.oreilly.com/catalog/learnxslt/. The examples are organized into directories that are associated with each of the chapters, as in examples/ch01, examples/ch02, examples/ch03, and so on. The XML documents and XSLT stylesheets used in the examples are intentionally simple so as to not obscure the principles they teach with too much distracting markup. These working examples will provide models for you to do about anything you can do with XSLT.

XSLT and XPath Reference

This book doesn’t contain reference material for XSLT or XPath. Doug Tidwell’s XSLT (O’Reilly) does a good job with its reference material, and I recommend you get a copy of that book. The download for this book offers a small Java program called Ox that gives you access to reference information at the command prompt (in examples/Ox). For example, if you have a recent Java Runtime Environment (JRE) installed on you computer, you can enter a line such as the following at a command or shell prompt:

java -jar ox.jar xsl:text

Ox will then return information about the XSLT instruction element text on your screen. You’ll learn more about how to use Ox in Chapter 18.

How This Book Is Organized

Learning XSLT is organized into 18 chapters. Here is a brief synopsis of each:

Chapter 1

Introduces you to some basic XSLT terminology and the process of transforming documents with XSLT processors on the command line, in a browser, and in a graphical application.

Chapter 2

Shows you how to build a new, transformed XML document by adding elements, attributes, and text using XSLT instruction elements or literal result elements. It also shows you how to create comments and processing instructions with XSLT.

Chapter 3

Explains and demonstrates the differences between XML, XHTML, HTML, and text output. Covers indentation, XML declarations, document type declarations, CDATA sections, and media types. Also discusses whitespace issues.

Chapter 4

Introduces you to XPath, showing you how to use location paths, patterns, and expressions. Explains the seven basic node types, and introduces result tree fragments.

Chapter 5

Shows you how to use XPath and XSLT functions in expressions.

Chapter 6

Demonstrates how to copy nodes using deep or shallow copy techniques.

Chapter 7

Talks you through the use of variables and parameters.

Chapter 8

Reveals how to sort nodes alphabetically and numerically.

Chapter 9

Explains how to display formatted numbers in a result tree, including lists that are numbered either alphabetically, with Roman numerals, or numerically.

Chapter 10

Discusses template priority, shows you how to name templates and later invoke them by name, and also shows you how to use parameters and modes with templates and explains what built-in templates are.

Chapter 11

With XSLT, you can associate a key with a value and then use this key to find nodes in a document. This chapter explains how to use keys, including a grouping technique.

Chapter 12

Illustrates how to process nodes with the if and when instructions.

Chapter 13

Shows how you can use more than one source document for a transformation, as well as how to use more than one stylesheet. Also reveals the difference between including and importing stylesheets.

Chapter 14

Demonstrates several possible alternative stylesheets, such as a literal result element stylesheet and an embedded stylesheet.

Chapter 15

Explores the use of extension elements, attributes, and functions made available with some of the more popular processors.

Chapter 16

The XSLT 2.0 and XPath 2.0 specifications aren’t quite ready for prime time, but they are building momentum and interest, and are nearing completion. This chapter introduces you to some of the more important new features of these new specs.

Chapter 17

Using APIs from Java and C#, you can create a custom wrapper for your preferred XSLT processor. This chapter uses code in both languages to show you how.

Chapter 18

Reviews important XSLT resources and demonstrates how to use the Ox documentation tool for XSLT and XPath reference.

Appendix, XSLT Processors

Helps you find, install, and use a variety of XSLT processors, most of them for free. This appendix also presents some of the basic tenets of using Java processors.

???Glossary

A glossary of general XML, XSLT, and XPath terms.

Conventions Used in This Book

The following font conventions are used in this book:

Plain text

Indicates menu titles, menu options, and menu buttons.

Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix activities.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values.

Tip

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Examples

This book is here to help you get your job done. In general, you may use the code, stylesheets, or documents in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example, "ActionScript: The Definitive Guide, Second Edition by Colin Moock. Copyright 2001 O’Reilly & Associates, Inc., 0-596-0036-X.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O’Reilly & Associates, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)

There is a web page for this book, which lists errata, examples, or any additional information. You can access this page at:

http://www.oreilly.com/catalog/learnxslt/

To comment or ask technical questions about this book, send email to:

For more information about books, conferences, Resource Centers, and the O’Reilly Network, see the O’Reilly web site at:

http://www.oreilly.com

Acknowledgments

I want to thank the editor of Learning XSLT, Simon St. Laurent, for giving me the opportunity to write this book for O’Reilly. I also appreciate the many useful comments provided by the technical reviewers—Michael Kay, Evan Lenz, Jeff Maggard, Sal Mangano, and Dave Pawson. They collectively saved me from a lot of embarrassment! Finally, I want to thank my wife Cristi for her love and support, without which I could not do what I do, nor would I probably want to do what I do.

Get Learning XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.