800 East 96th St., Indianapolis, Indiana, 46240 USA
Sams Teach Yourself XSLT in 21 Days
Copyright © 2002 by Sams
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
International Standard Book Number: 0-672-32318-4
Library of Congress Catalog Card Number: 2001094829
Printed in the United States of America
First Printing: February 2002
07 06 05 5 4 3 2
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
ASSOCIATE PUBLISHER
Paul Boger
ACQUISITIONS EDITOR
Rochelle J. Kronzek
DEVELOPMENT EDITOR
Songlin Qiu
MANAGING EDITOR
Matt Purcell
PROJECT EDITOR
Natalie Harris
COPY EDITOR
Chuck Hutchinson
INDEXER
Kelly Castell
PROOFREADER
Kay Hoskin
TECHNICAL EDITOR
Mike Wooding
TEAM COORDINATOR
Pamalee Nelson
INTERIOR DESIGNER
Dan Armstrong
COVER DESIGNER
Alan Clements
PAGE LAYOUT
Stacey Richwine-DeRome
Gloria Schurick
Day 1 Getting Started with XSLT
6 Conditional and Iterative Processing 139
13 Working with Multifile Stylesheets 319
14 Working with Multiple XML Sources 341
18 Building Computational Stylesheets 441
19 Working with XSLT Extensions
20 Working with Different Processors 485
21 Designing XML and XSLT Applications 507
Appendix A Answers to Quiz Questions and Exercises
B A Quick Reference of XSLT Elements and Functions
C Command-Line Reference for Selected Processors
Who Is This Book’s Intended Audience?
What Do You Need to Know Before You Read This Book?
What Will You Learn from This Book?
What Software Will You Need to Complete the Examples Provided with This Book?
What’s on the Sams Web Site for This Book
DAY 1 Getting Started with XSLT
Understanding Declarative Programming
Processors for XML Transformation with XSLT
DAY 2 Transforming Your First XML
Applying a Stylesheet to an XML Source
Linking a Stylesheet to an XML Source
Embedding a Stylesheet in an XML Source
Executing a Stylesheet Using Code
Understanding the XML Document Tree
The Benefit of Using Templates
Determining Which Template Is Used
Different Templates for Different Cases
DAY 5 Inserting Text and Elements
Inserting Elements and Attributes
Copying Elements from the Source Document
Copying Node-Sets and Tree Fragments
Inserting Comments and Processing Instructions
Inserting Processing Instructions
DAY 6 Conditional and Iterative Processing
Processing Each Node in a Node-Set
Conditional Processing with Multiple Options
Creating Different Output Formats
Creating Other Types of Output
Output Encoding and Output Escaping
Determining the Output Encoding
Encoding Characters That Are Not Supported
Stripping Whitespace from the Source Document
Dealing with Whitespace in the Stylesheet
Creating an Article with a Table of Contents
What Is the Benefit of Variables?
Creating Variables from Expressions
Using Variables to Replace an Expression
Using Variables for Out-of-Context Data
Creating Variables from XSLT Elements
What Is the Benefit of Parameters?
Using Parameters to Alter the Output
Using Parameters to Create Template Functions
Getting Data from Outside the Stylesheet
DAY 10 Understanding Data Types
Checking for Characters in a String
Getting the Length of a String
Controlling the Numbering Output
DAY 13 Working with Multifile Stylesheets
The Benefits of Multiple Files
The Drawbacks of Multiple Files
Duplicate Variables and Parameters
The Difference Between Including and Importing
Import Rules for Other Elements
DAY 14 Working with Multiple XML Sources
Getting Data from an XML Source
Defining Additional Documents Dynamically
Specifying a Different File Location
Accessing the Stylesheet Elements
Creating a Multifile Stylesheet with Parameters
Creating the Second Stylesheet
Creating a Stylesheet for All the Data
DAY 15 Working with Namespaces
Processing XML Sources with Namespaces
Inserting and Removing Namespaces
Inserting Nodes with Namespaces
DAY 16 Advanced Data Selection
Selecting Data with a Unique ID
Using Keys and Generated IDs to Select Distinct Values
Why and When Should You Use Recursion?
DAY 18 Building Computational Stylesheets
Computational Stylesheets Explained
What Is a Computational Stylesheet?
When Do I Use a Computational Stylesheet?
Operators and Functions Used in Computations
Ranking Teams in a Competition
DAY 19 Working with XSLT Extensions
What Are the Benefits of XSLT Extensions?
The Drawbacks of XSLT Extensions
Using Extensions with Other Processors
Creating Your Own Extension Functions
Using Java Functions as Extension Functions
Creating an Extension Function with Script
DAY 20 Working with Different Processors
Dealing with Processor Differences
Dealing with Different XSLT Versions
XML Capabilities of Database Servers
Getting and Transforming XML Data with Oracle
Getting XML Data from Microsoft SQL Server
DAY 21 Designing XML and XSLT Applications
Creating a Shopping Basket in XSLT
A Answers to Quiz Questions and Exercises
B A Quick Reference of XSLT Elements and Functions
XSLT and XPath Function Reference
C Command-Line Reference for Selected Processors
MICHIEL VAN OTEGEM lives and works in the Netherlands. He is the co-founder and Chief Web development Teacher of ASPNL, a consulting and teaching firm targeting the Dutch and European market. He teaches advanced ASP, ASP.NET, and XML/XSLT classes, and writes articles and tutorials for magazines and Web sites, such as ASPNL.com, TopXML.com, ASPAlliance.com, CoDe Magazine, and asp.netPRO magazine. He has had a passion for programming ever since he wrote his first programs in MSX Basic and Z80 assembler, at age 10. Now, nearly two decades later, he is a pioneer in Web development, quick to embrace technologies such as XML and ASP (.NET). He has worked with a wide range of languages and platforms, including ASP (.NET), Visual Basic, Access/SQL Server, C/C++, CGI/Perl, PHP, and, of course, XML and XSLT. He is a long-time contributing member of ASPFriends.com mailing lists, which he now helps to moderate and improve as a valued ASP Ace member.
To the love of my life, Annette, for her patience, understanding, and support, and to my parents for their unconditional support and wisdom.
This book would not have been possible if my partner, Annette, had not run the house during my mental absence needed to write this book. It would also never have been possible if my parents had not told me to follow my dreams, not to mention that they taught me most of what I know about writing and teaching.
I would have never come into a position to write this book if it weren’t for my valuable friends Andreas Kviby and Charles Carroll, who showed me that I could do more than development work alone. In addition, the encouragement and insights of Peter Vogel (and his lovely wife), Kurt Cagle, Michael Corning, and many other speakers and writers made this work interesting and fun.
I would also like to thank the people at Sams Publishing, specifically Songlin Qiu for dealing with all my issues, Natalie Harris for her quickness, Chuck Hutchinson for his fabulous copy edits, and Shelley Kronzek for her belief in me as an author. A special thanks to Mike Wooding, whose technical comments and humor were of enormous value.
Finally, I would like to thank Steely Dan for creating great music that helped me during the writing process.
As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way.
As an Associate Publisher for Sams, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message.
When you write, please be sure to include this book’s title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book.
Fax: | 317-581-4770 |
E-mail: | |
Mail: | Paul Boger |
| Sams |
| 201 West 103rd Street |
| Indianapolis, IN 46290 USA |
XML is one of the biggest things to hit the World Wide Web since the invention of the Web itself. It has the simplicity of HTML, looking much like it, but at the same time it is much more powerful. This power comes from its generic nature, which makes XML useful for a myriad of applications, not just on the Web, but in any (distributed) computing environment.
If you want to manipulate XML, you have several choices. By far the most powerful is XSLT, which enables you to do very powerful things with the data stored in an XML document. What makes XSLT so interesting is that it is remarkably simple, but at the same time very powerful. Operations that require many lines of code with conventional techniques can be solved in XSLT with just a few lines of code because XSLT uses a completely different programming paradigm, one that you’ll learn to love during the course of reading this book.
XSLT doesn’t replace existing programming languages, but rather complements them. In that sense, XSLT is just another tool in your toolbox. However, applications that target XML- and XSLT-enabled Web browsers, such as Internet Explorer, don’t need any additional programming. This means you can create distributed applications with little effort.
Finally, because XML and XSLT are World Wide Web Consortium–endorsed standards, they are truly cross-platform. Any platform equipped with an XSLT processor can run your application.
This book is intended to teach absolute beginners the basics of XSLT and much, much more. This means that this book is also suitable for people with basic knowledge and experience with XSLT, because many of the topics are covered in great detail. In addition, the more advanced topics haven’t been forgotten.
This book starts at the very beginning of XSLT, so you don’t need any prior knowledge of XSLT. Because XSLT operates on and is itself XML, you need a basic knowledge of XML. This means you need to know what XML is, what its syntax is, and how it is structured. Beyond that, you really don’t need anything else. Having a working knowledge of HTML does help, however. Any prior programming experience is not required; in fact, XSLT programming is based on another programming paradigm than that used in languages such as C++, Java, and Visual Basic. Any prior programming experience is therefore of limited use.
This book will teach you anything you need to know about XSLT as a programming language. You will learn how to create XSLT documents and how to use them to transform XML documents to text, HTML, or other XML formats. You also will learn how to use processors to apply XSLT to XML documents and how to use data that is not in the XML document. After you finish the book, you will be able to create complex XSLT documents performing complex transformations of XML documents.
To complete the samples in the book, you need a text editor and an XSLT processor. Unless otherwise specified, the processor used in this book is Saxon version 6.2.2. Other processors discussed are Xalan Java and Microsoft’s MSXML parser/processor component, complemented by the MSXSL command-line tool for this component. You can find information about these processors, including download locations, in Appendix C. At http://xml.startkabel.nl/, you can find links to most processors available, most of which are free.
This book is organized so you learn XSLT in 21 days. Therefore, there are 21 lessons, one lesson for each of the 21 days. The lessons are grouped in equal parts of seven lessons, so one part corresponds to one week. Each week concludes with a Bonus Project, which creates an application from scratch, based on the topics covered in that week. This book also contains several appendixes.
Week 1 aims to build your basic knowledge of XSLT. You will learn about processors, editors, and most importantly, about the structure and elements in XSLT. After completing week 1, you will have a good working knowledge to create basic XSLT documents.
Week 2 extends your knowledge of XSLT, based on what you learned in week 1. In this week, you will learn about the more intricate details of XSLT and how to create more complex and flexible documents. You also will learn how to create applications that span multiple documents.
Week 3 discusses a myriad of different topics that go beyond day-to-day use of XSLT, such as performing computations with XSLT and using processor-specific constructs. The last day also looks back at all that you have learned from an application design point of view, which will help you to design and implement your applications to be flexible and more robust.
This book includes four appendixes:
• Appendix A contains the answers to the questions and exercises in the book.
• Appendix B contains a quick reference to all elements and functions in XSLT.
• Appendix C contains information and a command-line reference on MSXSL, Saxon, and Xalan Java.
• Appendix D contains a list of useful XML and XSLT resources on the Web.
The chapter-by-chapter code files described in this book are available on the Sams Web site at http://www.samspublishing.com/. Enter this book’s ISBN in the Search box and click Search. When the book’s title is displayed, click the title to go to a page where you can download all the code in a chapter-by-chapter zip file format.
The following typographic conventions are used in this book:
• Code lines, commands, statements, variables, and any text you type or see onscreen appear in a mono
typeface. Bold mono
typeface is used to represent the user’s input.
• Placeholders in syntax descriptions appear in an italic mono
typeface. Replace the placeholder with the actual filename, parameter, or whatever element it represents.
NEW TERM
• Italics highlight technical terms when they’re being defined. A paragraph that defines technical terms is marked by an icon.
• The icon is used before a line of code that is really a continuation of the preceding line. Sometimes a line of code is too long to fit as a single line on the page. If you see before a line of code, remember that it’s part of the line immediately above it.
OUTPUT
• Code listings that show output are marked with an output icon.
• The book also contains Notes, Tips, and Cautions to help you spot important or useful information more quickly. Some of them are helpful shortcuts to help you work more efficiently.
3.137.212.124