Tải bản đầy đủ (.pdf) (825 trang)

beginning xslt 2.0

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.08 MB, 825 trang )

www.it-ebooks.info
Beginning XSLT 2.0
From Novice to Professional
JENI TENNISON
3243chFM.qxd 6/20/05 10:06 AM Page i
www.it-ebooks.info
Beginning XSLT 2.0: From Novice to Professional
Copyright © 2005 by Jeni Tennison
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN (pbk): 1-59059-324-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Chris Mills
Technical Reviewer: Norman Walsh
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Assistant Publisher: Grace Wong
Project Manager: Kylie Johnston
Copy Manager: Nicole LeClerc
Copy Editor: Ami Knox
Assistant Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor and Artist: Kinetic Publishing Services, LLC
Proofreader: Elizabeth Berry
Indexer: Kevin Broccoli
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski


Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th
Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17,
69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or visit
. Outside the United States: fax +49 6221 345229, e-mail ,
or visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi-
rectly by the information contained in this work.
The source code for this book is available to readers at in the Downloads section.
3243chFM.qxd 6/20/05 10:06 AM Page ii
www.it-ebooks.info
iii
Contents at a Glance
About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1 Introducing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2 Creating HTML from XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
CHAPTER 3 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
CHAPTER 4 Conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
CHAPTER 5 Manipulating Atomic Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
CHAPTER 6 Variables and Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
CHAPTER 7 Paths and Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
CHAPTER 8 Result Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
CHAPTER 9 Sorting and Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

CHAPTER 10 IDs, Keys, and Numbering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
CHAPTER 11 Named Templates, Stylesheet Functions, and Recursion . . . . . . . 473
CHAPTER 12 Building XSLT Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
CHAPTER 13 Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
CHAPTER 14 Backwards Compatibility and Extensions . . . . . . . . . . . . . . . . . . . . . . 557
CHAPTER 15 Dynamic XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
CHAPTER 16 Creating SVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
CHAPTER 17 Interpreting RSS with XSLT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
APPENDIX A XPath Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
APPENDIX B XSLT Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
3243chFM.qxd 6/20/05 10:06 AM Page iii
www.it-ebooks.info
3243chFM.qxd 6/20/05 10:06 AM Page iv
www.it-ebooks.info
v
Contents
About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1 Introducing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Markup Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Extending HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Meta-Markup Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
XML: The Extensible Markup Language. . . . . . . . . . . . . . . . . . . . . . . . . 6
XML Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
XHTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Elements in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Attributes in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Entities, Characters, and Encodings. . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Other Components of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Moving to XHTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Creating Markup Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Designing Markup Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Validating Markup Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Presenting XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Presenting XML with CSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Associating Stylesheets with XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Limitations of CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
■CHAPTER 2 Creating HTML from XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
XSL: The Extensible Stylesheet Language. . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Using XSLT Processors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Using Saxon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Using MSXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3243chFM.qxd 6/20/05 10:06 AM Page v
www.it-ebooks.info
■CONTENTSvi
Simplified Stylesheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Literal Result Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The <xsl:value-of> Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The XSLT Namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Generating HTML Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Iterating Over Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Generating Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

■CHAPTER 3 Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
XSLT Stylesheet Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Stylesheet Document Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Defining Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Node Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
XSLT Processing Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The Initial Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Matching Elements with Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
The Built-in Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Extending Stylesheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Templates As Mapping Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Processing Document-Oriented XML . . . . . . . . . . . . . . . . . . . . . . . . . 104
Context-Dependent Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Resolving Conflicts Between Templates . . . . . . . . . . . . . . . . . . . . . . 117
Choosing the Next Best Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Processing with Push and Pull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Using Templates with Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Built-in Templates Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Chapter 4 Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Conditional Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Processing Optional Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Using the Ancestry of Source XML . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Using the Location of Result XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Conditional Elements in XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Conditional Expressions in XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
3243chFM.qxd 6/20/05 10:06 AM Page vi
www.it-ebooks.info
■CONTENTS vii

Testing Elements and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Testing for Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Comparing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Testing with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Combining Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Filtering XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Testing Positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 5 Manipulating Atomic Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Atomic Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The Atomic Type Hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Creating Atomic Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Casting Between Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Manipulating Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Splitting and Recombining Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Reformatting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Regular Expression Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Manipulating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Manipulating Dates, Times, and Durations . . . . . . . . . . . . . . . . . . . . . . . . . 215
Extracting Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Adjusting Timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Formatting Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Manipulating Qualified Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Manipulating URIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
■CHAPTER 6 Variables and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Defining Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

Declaring a Variable’s Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Referring to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Sequence Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Temporary Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
3243chFM.qxd 6/20/05 10:06 AM Page vii
www.it-ebooks.info
■CONTENTSviii
Using Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Declaring and Referring to Parameters . . . . . . . . . . . . . . . . . . . . . . . 260
Stylesheet Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
■CHAPTER 7 Paths and Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Node Trees Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Accessing Information About Nodes. . . . . . . . . . . . . . . . . . . . . . . . . . 277
Namespaces in the Node Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Whitespace in Node Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Matching Nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Path Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Step Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Node Tests and Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Selecting Nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Evaluating Location Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Sequence Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Creating Sequences with XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Creating Sequences with XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

Testing Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Iterating Over Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Formatting Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
■CHAPTER 8 Result Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Generating Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Generating Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Generating Namespace Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Generating Text Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Generating Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Generating Comments and Processing Instructions . . . . . . . . . . . . 372
Creating Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Copying Nodes and Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Creating Result Documents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
3243chFM.qxd 6/20/05 10:06 AM Page viii
www.it-ebooks.info
■CONTENTS ix
Controlling Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Output Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Declaring Content Type Information . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Controlling Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
■CHAPTER 9 Sorting and Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Sorting in Different Orders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Sorting Nonalphabetically. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Multiple Sorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Flexible Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Grouping by Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Grouping in Sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Multilevel Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
■CHAPTER 10 IDs, Keys, and Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Generating IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Getting the Number of an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Numbering Sorted and Filtered Items . . . . . . . . . . . . . . . . . . . . . . . . 460
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Numbering Across a Document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Generating Hierarchical Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
■CHAPTER 11 Named Templates, Stylesheet Functions,
and Recursion
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Named Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Stylesheet Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
3243chFM.qxd 6/20/05 10:06 AM Page ix
www.it-ebooks.info
■CONTENTSx
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Recursive Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

Numeric Calculations Using Recursion . . . . . . . . . . . . . . . . . . . . . . . 486
Recursing Over Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Recursing with Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Tail Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
■CHAPTER 12 Building XSLT Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Splitting Up Stylesheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Reusing Stylesheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Accessing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Accessing External Documents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Using Keys in External Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Retrieving Referenced Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Resolving Relative URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Accessing Multiple Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
■CHAPTER 13 Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Validation, Schemas, and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Schemas and Type Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Typed Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Using Schemas Within Stylesheets. . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Importing Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Matching by Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Matching by Named Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Matching by Declared Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Matching by Substitution Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Annotating Node Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Specifying Node Types Explicitly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Validating Against a Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546

Managing Type Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
3243chFM.qxd 6/20/05 10:06 AM Page x
www.it-ebooks.info
■CONTENTS xi
■CHAPTER 14 Backwards Compatibility and Extensions. . . . . . . . . . . . . . . . 557
Backwards Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Testing XSLT Processors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Upgrading XSLT 1.0 Stylesheets to XSLT 2.0 . . . . . . . . . . . . . . . . . . 561
Running XSLT 2.0 Stylesheets in XSLT 1.0 Processors. . . . . . . . . . 564
Same-Version Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Testing Function Availability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Testing Instruction Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Excluding Portions of a Stylesheet . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Sending Messages to the User in XSLT 2.0. . . . . . . . . . . . . . . . . . . . 572
Extensions to XSLT and XPath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Extension Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Extensions to Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Extension Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Extension Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Data Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
■CHAPTER 15 Dynamic XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Dynamic Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Server-Side Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Client-Side Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Client Side or Server Side? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Server-Side Transformations Using Cocoon . . . . . . . . . . . . . . . . . . . . . . . . 592

Installing Cocoon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Configuring Cocoon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Different Stylesheets for Different Browsers. . . . . . . . . . . . . . . . . . . 606
Using Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Client-Side Transformations Using Sarissa. . . . . . . . . . . . . . . . . . . . . . . . . 615
Loading Sarissa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Creating DOMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Performing Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Handling Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Passing Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
3243chFM.qxd 6/20/05 10:06 AM Page xi
www.it-ebooks.info
■CONTENTSxii
■CHAPTER 16 Creating SVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Introducing SVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Lengths and Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Graphic Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Container Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Generating SVG with XSLT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
SVG Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Constructing the Stylesheet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Embedding SVG in HTML Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
■CHAPTER 17 Interpreting RSS with XSLT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
RDF Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Statements, Resources, and Properties. . . . . . . . . . . . . . . . . . . . . . . 670

Representing Statements in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Introducing RSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
RSS Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
RSS Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
Transforming RSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Sample Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Basic Stylesheet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Creating the Program Listing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
Adding Duration Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Adding Rating Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Final Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
■APPENDIX A XPath Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Sequence Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
Regular Expression Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
3243chFM.qxd 6/20/05 10:06 AM Page xii
www.it-ebooks.info
■CONTENTS xiii
■APPENDIX B XSLT Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
XSLT Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
XSLT Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
3243chFM.qxd 6/20/05 10:06 AM Page xiii
www.it-ebooks.info
3243chFM.qxd 6/20/05 10:06 AM Page xiv

www.it-ebooks.info
xv
About the Author
■JENI TENNISON is an independent consultant and author specializing
in XSLT and XML schema development. She trained as a knowledge
engineer, gaining a PhD in collaborative ontology development, and
since becoming a consultant has worked in a wide variety of areas,
including publishing, water monitoring, and financial services. She
is author of XPath and XSLT On The Edge (Hungry Minds, 2001) and
Beginning XSLT (Wrox, 2002) and one of the founders of the EXSLT ini-
tiative to standardize extensions to XSLT and XPath. She is an invited
expert on the XSL Working Group at the W3C. She lives with her family,
cats, and computers in Nottingham, England.
3243chFM.qxd 6/20/05 10:06 AM Page xv
www.it-ebooks.info
3243chFM.qxd 6/20/05 10:06 AM Page xvi
www.it-ebooks.info
xvii
About the Technical Reviewer
■NORMAN WALSH is an XML standards architect at Sun Microsystems, Inc. He is an active par-
ticipant in a number of standards efforts worldwide, including the XML Core and XSL Working
Groups of the World Wide Web Consortium, where he is also an elected member of the Technical
Architecture Group, and the RELAX NG, Entity Resolution, and DocBook Technical Committees
at OASIS. In addition to chairing the DocBook Technical Committee, he is the principal author
of DocBook: The Definitive Guide (O’Reilly & Associates, 1999) and a lead designer of the widely
used DocBook XSL Stylesheets.
3243chFM.qxd 6/20/05 10:06 AM Page xvii
www.it-ebooks.info
3243chFM.qxd 6/20/05 10:06 AM Page xviii
www.it-ebooks.info

xix
Acknowledgments
This book has had a long and drawn-out gestation as it matched the slow progress of XSLT 2.0.
My thanks to all those at Apress who helped in the process, particularly Kylie Johnston, Martin
Streicher, and Ami Knox. My thanks to Norm Walsh for somehow finding the time to do a tech-
nical review; much as I’d like to blame them on him, the remaining errors are mine, of course.
And many thanks to Michael Kay for letting me have a copy of Schema-Aware Saxon to use.
Finally, thanks to those in the XML and XSLT community for their questions, opinions, and
encouragement: I learned all I know from you.
3243chFM.qxd 6/20/05 10:06 AM Page xix
www.it-ebooks.info
3243chFM.qxd 6/20/05 10:06 AM Page xx
www.it-ebooks.info
xxi
Introduction
Welcome to Beginning XSLT 2.0, a comprehensive introduction to the Extensible Stylesheet
Language: Transformations 2.0. This book introduces you to transforming XML with XSLT 2.0,
helping you to create tailored presentations for all the information you have accessible as XML.
I wrote this book, like Beginning XSLT, based on my own experience as an XSLT user, but
also based on my familiarity, from training people in XSLT, with the practical and conceptual
hurdles that newcomers often face. My aim is to provide a step-by-step, how-to manual for
the kind of the real-world transformation problems that you will come across.
Who This Book Is For
This book is primarily for newcomers to XML and XSLT. It’s particularly aimed towards web
developers who have some knowledge of HTML, CSS, and a smattering of JavaScript; but none
of these are essential, and the techniques you learn in this book can just as easily be applied to
transforming between XML-based markup languages as to web pages.
Seasoned users of XSLT 1.0 will learn about the new datatypes, expressions, and functions
of XPath 2.0 and the new facilities in XSLT that make tasks such as text processing, grouping,
and creating multiple documents much easier. Although much will look familiar, there are also

some fundamental changes in the XPath data model and the XSLT processing model that may
take you by surprise.
How This Book Is Structured
This book starts gently, introducing XML and XSLT bit by bit and gradually demonstrating the
techniques that you need to generate HTML (and other formats) from XML.
The first eight chapters are ideally read in sequence, since they build on each other and
together introduce you to the fundamental concepts involved in transforming XML with XSLT.
Chapters 9 to 15 provide guides to particular facilities in XSLT that you may or may not find
useful depending on your particular project. You can dip into these chapters as you see fit,
although the examples do continue to build on each other.
Because each of these chapters introduces new material, they contain a lot of exercises
that you can follow to try out the new techniques that you’ve read about. In addition, each
chapter has a set of review questions at the end to help reinforce the information that you’ve
taken in.
The final two chapters, 16 and 17, pull together the techniques that you’ve learned in iso-
lation earlier in the book, so that you get a feel for how a stylesheet is developed from scratch.
These chapters round off with a set of ideas for future development of the stylesheet that give
you an opportunity to try out your XSLT skills.
3243chFM.qxd 6/20/05 10:06 AM Page xxi
www.it-ebooks.info
■INTRODUCTIONxxii
• Chapter 1, “Introducing XML”:
This chapter introduces you to XHTML and XML and discusses the design of markup
languages, and how you can validate documents against schemas and apply CSS
stylesheets to XML. If you already know about XML, then you may want to skip this
chapter, although you may find it useful to go through the review questions to refresh
your memory. You’ll also be introduced to the TV guide example that’s used throughout
the book, and here translate an original HTML page to an XHTML equivalent and into
an XML format.
• Chapter 2, “Creating HTML from XML”:

This chapter introduces simplified XSLT stylesheets and describes how to create HTML
pages using them. In this chapter, you’ll create a stylesheet that transforms the TV guide
XML that you generated in the previous chapter into a basic HTML page for a daily listing.
• Chapter 3, “Templates”:
This chapter introduces templates as a way of breaking up larger XSLT stylesheets into
separate components, each handling the generation of a different portion of the HTML
page. Here you’ll create your first full XSLT stylesheet for the TV guide, learn how to process
document-oriented XML, and make your stylesheet more maintainable. XSLT 1.0 users
will learn about the new instruction <xsl:next-match> and using templates with multiple
modes.
• Chapter 4, “Conditions”:
This chapter discusses ways of creating conditional portions of a document, depending
on the information that’s available to the stylesheet. In this chapter, you’ll tackle the
creation of some more complex HTML whose structure depends on the information
that’s available about a program—for example, adding an image to the page if a pro-
gram has been flagged. XSLT 1.0 users will learn about new value comparisons, node
comparisons, and the if XPath statement.
• Chapter 5, “Manipulating Atomic Values”:
This chapter introduces the datatypes that are available in XSLT, including strings,
numbers, and dates and times. It describes the various functions and operators that
you can use to manipulate them (including processing strings using regular expres-
sions), perform calculations, and extract components such as the year of a date.
Almost all the content of this chapter will be new to XSLT 1.0 users.
• Chapter 6, “Variables and Parameters”:
This chapter examines how to store pieces of information in variables so that you can
reuse them, and how to pass parameters between templates and into XSLT stylesheets
in order to change the output that’s created. Learning about variables and parameters
will allow you to simplify your stylesheet, and to create a stylesheet that can be used to
generate guides for different series when passed the name of a series. XSLT 1.0 users
will learn how to declare the types of their variables and about the new concepts of

temporary trees and sequence constructors.
3243chFM.qxd 6/20/05 10:06 AM Page xxii
www.it-ebooks.info
■INTRODUCTION xxiii
• Chapter 7, “Paths and Sequences”:
This chapter looks at how to extract information from XML documents and create
sequences of values. While this chapter has a lot of theoretical content, it will equip
you with the skills to move around XML information with ease. XSLT 1.0 users will
learn about the new properties of nodes and the functions to access them, and how
to create and process sequences of atomic values (such as numbers).
• Chapter 8, “Result Trees”:
This chapter explores the various methods of creating parts of an HTML document.
In this chapter, you’ll learn how to create conditional attributes, how to add comments
within the HTML page, and several techniques that give you more control over the pre-
cise look of the HTML that you generate. XSLT 1.0 users will be particularly pleased to
learn how to create multiple output documents from a single transformation.
• Chapter 9, “Sorting and Grouping”:
This chapter introduces methods for sorting and grouping together the components
that you generate in the HTML page. For example, you’ll see how to list programs alpha-
betically or by the time that they’re shown, and how to group episodes according to the
series they belong to. XSLT 1.0 users will be introduced to the new <xsl:perform-sort>
and <xsl:for-each-group> instructions.
• Chapter 10, “IDs, Keys, and Numbering”:
This chapter shows you how to follow links between separate pieces of information
and how to generate identifiers, such as numbers, that can be used within the HTML
page you generate. While trying these techniques out on the TV guide, you’ll see how
to manage when data about series is kept separate from the data about individual pro-
grams, and you’ll learn how to assign each program a unique number so that you can
link between them.
• Chapter 11, “Named Templates, Stylesheet Functions, and Recursion”:

This chapter introduces you to how to create named templates or functions that you
can call to carry out a series of XSLT instructions. You’ll learn how to use recursion
(when a template or function calls itself) within XSLT. Here you’ll develop a number
of utility templates or functions that allow you to perform sophisticated calculations.
XSLT 1.0 users will find the ability to create user-defined functions with XSLT code
particularly helpful.
• Chapter 12, “Building XSLT Applications”:
This chapter discusses how to manage XSLT stylesheets that are divided between
several files, and how to generate HTML based on information from multiple separate
XML or text documents. Here you’ll learn how to create stylesheets that hold utility
code that you can use in all the stylesheets for the TV Guide web site. You’ll also learn
what to do when the TV guide information is divided between several physical files.
XSLT 1.0 users will learn about the new unparsed-text() function, amongst others.
3243chFM.qxd 6/20/05 10:06 AM Page xxiii
www.it-ebooks.info
■INTRODUCTIONxxiv
• Chapter 13, “Schemas”:
This chapter describes how to use schemas to make your stylesheets simpler by using
type annotations on nodes. You’ll also learn how to invoke validation of the output that
your stylesheet generates, which can help ensure that the result of the transformation is
as it should be. All this chapter will be new to XSLT 1.0 users.
• Chapter 14, “Backwards Compatibility and Extensions”:
This chapter discusses how to write stylesheets that can be run with both XSLT 1.0 and
XSLT 2.0 processors and how to deal with partial implementations of XSLT 2.0. It also
discusses the use of extension attributes, functions, and instructions that are available
in different processors and some of the ways in which you can debug your code.
• Chapter 15, “Dynamic XSLT”:
This chapter discusses how to use XSLT in two environments that have built-in support
for running transformations: client side in browsers such as Internet Explorer or Firefox
(with the Sarissa library), and server side in Cocoon (a Java servlet). You’ll learn how to

create dynamic XSLT applications that provide different presentations depending on
a user’s input. For example, you’ll learn how to create forms that let users request sum-
maries of particular TV series, so that the series guides can be created dynamically on
demand.
• Chapter 16, “Creating SVG”:
This chapter introduces you to SVG, Scalable Vector Graphics, which is a markup lan-
guage that represents graphics. You’ll learn the basics of SVG, and experiment with it to
create a pretty, printable image displaying the programs showing during a particular
evening.
• Chapter 17, “Interpreting RSS with XSLT”:
This chapter examines RSS, or RDF Site Summaries, as a way of receiving syndicated
information from other sites. We’ll examine how to use TV listings and news received
from other online sources in our own TV guide.
Conventions
You will encounter various styles of text and layout as you browse through this book. These
have been used deliberately in order to make important information stand out. These styles
are as follows:
Exercises
Exercises provide you with practical examples that you can step through using the source code that’s available
online via the Apress website.
3243chFM.qxd 6/20/05 10:06 AM Page xxiv
www.it-ebooks.info

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×