XSLT For Dummies - Richard Wagner - E-Book

XSLT For Dummies E-Book

Richard Wagner

0,0
22,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Restructuring information in an XML document so that it works in other formats used to be a time-consuming ordeal involving lots of blood, sweat, and tears. Now XSLT (Extensible Stylesheet Language Transformations) makes the process nearly instantaneous. Just provide an example of the kind of information you'd like to see, and XSLT does the rest. With XSLT you can effortlessly transform XML documents into virtually any kind of output, including other XML documents and HTML pages. But mastering XSLT can be tricky, especially if you've never worked with XML or HTML; and most books on the subject are written for people who have. Here comes XSLT For Dummies to the rescue! XSLT For Dummies is your ticket to quickly mastering XSLT--no matter what your prior programming experience. Writing in easygoing, plain English, XML pro Richard Wagner provides expert advice, step-by-step guidance, and tons of crystal-clear examples to help you harness the power of XSLT to transform documen ts. In no time you'll: * Understand how XSLT works with XSL and XPath * Experiment with templates, stylesheets, and expressions * Perform HTML transformations * Master XPath data types and functions * Combine XSLT stylesheets * Explore cool XSLT programming tricks XSLT For Dummies works from the ground up, starting with a practical introduction of the "X-Team"--XML, XSL, XSLT, and X-Path--and instructions on how to write a XSLT stylesheet. From there it quickly moves onward and upward through the whole range of important XSLT topics, including: * Transforming with stylesheets * Understanding and using template rules * Using XPath to locate nodes in XML documents * Combining XSLT stylesheets and adding processing instructions * Debugging XSLT transformations * Ten XSLT processors available online It doesn't matter whether you're a babe in the woods who can't tell a "tag" from an element, or you're an old pro at creating XML documents, XSLT For Dummies offers you a fun, easy way to explore and take full advantage of Extensible Stylesheet Language Transformations.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 318

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



XSLT For Dummies®

by Richard Wagner

XSLT For Dummies®

Published byWiley Publishing, Inc.111 River St.Hoboken, NJ 07030-5774www.wiley.com

Copyright © 2002 Hungry Minds, Inc. All rights reserved. No part of this book, including interior design, cover design, and icons, may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or otherwise) without the prior written permission of the publisher.

Library of Congress Control Number: 2002100181

ISBN: 0-7645-3651-6

Printed in the United States of America

10 9 8 7 6 5 4 3 2 1

1O/RR/QT/QS/IN

Distributed in the United States by Hungry Minds, Inc.

Distributed by CDG Books Canada Inc. for Canada; by Transworld Publishers Limited in the United Kingdom; by IDG Norge Books for Norway; by IDG Sweden Books for Sweden; by IDG Books Australia Publishing Corporation Pty. Ltd. for Australia and New Zealand; by TransQuest Publishers Pte Ltd. for Singapore, Malaysia, Thailand, Indonesia, and Hong Kong; by Gotop Information Inc. for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft for South Africa; by Eyrolles for France; by International Thomson Publishing for Germany, Austria and Switzerland; by Distribuidora Cuspide for Argentina; by LR International for Brazil; by Galileo Libros for Chile; by Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer Publishing Corporation, Inc., for the Philippines; by Contemporanea de Ediciones for Venezuela; by Express Computer Distributors for the Caribbean and West Indies; by Micronesia Media Distributor, Inc. for Micronesia; by Chips Computadoras S.A. de C.V. for Mexico; by Editorial Norma de Panama S.A. for Panama; by American Bookshops for Finland.

For general information on Hungry Minds’ products and services please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.

For sales inquiries and reseller information, including discounts, premium and bulk quantity sales, and foreign-language translations, please contact our Customer Care Department at 800-434-3422, fax 317-572-4002, or write to Hungry Minds, Inc., Attn: Customer Care Department, 10475 Crosspoint Boulevard, Indianapolis, IN 46256.

For information on licensing foreign or domestic rights, please contact our Sub-Rights Customer Care Department at 212-884-5000.

For information on using Hungry Minds’ products and services in the classroom or for ordering examination copies, please contact our Educational Sales Department at 800-434-2086 or fax 317-572-4005.

For press review copies, author interviews, or other publicity information, please contact our Public Relations Department at 317-572-3168 or fax 317-572-4168.

For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 978-750-4470.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES THAT EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.

Trademarks: Hungry Minds, the Hungry Minds logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Fun and Easy Way, The Dummies Way, Hungry Minds a la Carte and related trade dress are trademarks or registered trademarks of Hungry Minds in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. Hungry Minds, Inc. is not associated with any product or vendor mentioned in this book.

About the Author

Richard Wagner is vice president of Development Tools at Nombas, a JavaScript tools provider, and author of numerous books, including JavaScript Unleashed. He also invented and architected the award-winning NetObjects ScriptBuilder and served as editor for the XML-based ECMAScript Components standard. In his free time, he enjoys writing on his nontech Web site called digitalwalk.net (www.digitalwalk.net). Rich lives with his wife and three boys in Princeton, Massachusetts. He can be reached at [email protected].

Dedication

To the intrepid J-boys — Jordan, Jared, and Justus. In all things, “to the hilt . . .”

Author’s Acknowledgments

In writing this book, I was blessed with an unbeatable editorial team at Hungry Minds. My deepest thanks go to Christine Berman for her flawless management of this project from start to finish. Thanks to Becky Huehls for her keen editing eye and helpful suggestions, driving me to be clearer and more concise in my writing. Thanks also to Bob Dominy for his technical insights that made a strong impact on the book’s overall quality and coverage. I talk about the X-Team in this book, but I’d say the book’s X-Factor is Christine, Becky, and Bob. For without them, XSLT For Dummies would have been far less of a book.

I’d also like to express thanks to Steven Hayes at Hungry Minds for his involvement early on in the project, as well as thank my agent, Chris Van Buren.

Finally, I’d like to express earnest gratitude to my wife, Kimberly, and our three boys, the J-team, for their patience and grace throughout the entire project.

Publisher’s Acknowledgments

We’re proud of this book; please send us your comments through our Hungry Minds Online Registration Form located at www.dummies.com.

Some of the people who helped bring this book to market include the following:

Acquisitions, Editorial, and Media Development

Associate Project Editor: Christine Berman

Senior Acquisitions Editor: Steve Hayes

Copy Editor: Rebecca Huehls

Technical Editor: Robert Dominy

Editorial Manager: Leah Cameron

Media Development Manager: Laura VanWinkle

Media Development Supervisor: Richard Graves

Editorial Assistant: Amanda Foxworth

Production

Project Coordinator: Maridee Ennis

Layout and Graphics: Jackie Nicholas, Barry Offringa, Heather Pope, Jacque Schneider, Betty Schulte, Mary J. Virgin, Erin Zeltner

Proofreaders:

Contents

Title

Introduction

What I Assume About You

How This Book Is Organized

Conventions Used in This Book

Icons Used in This Book

Part I : Getting Started with XSLT

Chapter 1: Introducing the X-Team

XML: Storing Your Data

XSL: Presenting XML Data

XSLT: Transforming Your XML Data

XPath: Seeking Out Your Data

The X-Team through HTML Eyes

Chapter 2: Writing Your First XSLT Stylesheet

Preparing Your XSLT Working Environment

Creating a Simple XML Document

Knowing the End Result

Writing an XSLT Stylesheet

Processing the Stylesheet

Viewing the Results

Part II : Becoming an XSLT Transformer

Chapter 3: Transforming with Style (Stylesheets, That Is)

Structure of a Stylesheet

Constructing Your XSLT Stylesheet

What’s in a Name(space)?

Documents as Trees

Chapter 4: Templates Rule!

A Glorified Mail Merge

Basics of a Template Rule

Common Action Instructions

Built-In Template Rules

Matchmaker, Make Me a Match

Working with Attribute Value Templates

Working with Named Templates

Chapter 5: XPath Espresso

XPath, The X-Team’s Commando

Dancing the Location Step

Defining Relationships with Axes

Matching Nodes with Node Tests

Using Predicates to Get Specific

Take a Walk on the Absolute Side

Putting It All Together

Chapter 6: We Want Results!

XSLT, Like Skinning a Cat

XML Source

Copying an Element

Copying All Elements

Adding a New Element

Adding a Calculated Value

Renaming an Element

Removing an Element

Removing an Attribute

Reordering Elements

Merging Elements

Adding Attributes

Moving an Attribute

Converting Elements into Attributes

Converting Attributes into Elements

Part III : Prime Time XSLT

Chapter 7: Adding Programming Logic Isn’t Just for Propheads

Conditional and Looping Statements

If and Only If

Testing Expressions with Logical Operators

Why Choosy People Choose xsl:choose

Going Loopy with xsl:for-each

Chapter 8: Variables in XSLT: A Breed Apart

What Is a Variable?

Setting Variables

Uses of Variables

Think Globally, Act Locally

Working with Parameters

Chapter 9: Tweaking the Results to Get What You Want

Sorting Elements in the Results Tree

Adding Automatic Numbering

Chapter 10: To HTML and Beyond!

XML and HTML

Outputting to HTML

Creating an HTML Table

Linking an XSLT Stylesheet with an XML Document

Browser Support of XSLT

To Infinity and Beyond (Into Other Formats)

Chapter 11: XPath Data Types and Functions

Playing ‘Heart and Soul’ with Nodes

Working with Strings

Numerically Speaking

Booleans: To Be or Not to Be

General Purpose Functions

Part IV : eXtreme XSLT

Chapter 12: Combining XSLT Stylesheets

Comparing xsl:include and xsl:import

Practical Use of xsl:import

Chapter 13: “Gimme Some Space” and Other Output Issues

Gimme Some Space

Indenting Your Result Document

Adding Comments

Adding Processing Instructions

Chapter 14: Keys and Cross-Referencing

Keys to the Kingdom

Using Keys with Multiple Source Documents

Chapter 15: Namespaces Revisited

Speaking the Namespace Lingo

Adding a Namespace to the Result Document

Working with Multiple Namespaces

Chapter 16: Extending XSLT

Going Beyond the Basics

Using an Extension Element

Using an Extension Function

Ensuring Portability of Your Stylesheets

Chapter 17: Debugging XSLT Transformations

Conditionally Halting Execution

Adding a Conditional Debug Mode

Tracing through Your Code

More Powerful Debugging

Part V : The Part of Tens

Chapter 18: Ten Most Confusing Things About XSLT

Built-In Template: The Man Behind the Screen

Thar’s Trees in Them Documents

Getting to the Root of the Issue

Why the Selected Node Is Not the Same as the Current Node

Those //@.}* Abbreviations

To Apply or Copy, That Is the Question

Walk This Way

Expressions, Paths, and Steps

Those Cute Little Curly Braces

Whitespace, the Final Frontier

Chapter 19: Ten All-Pro XSLT Resources on the Web

Xslt.com

W3C XSLT Recommendation

W3C XPath Recommendation

Dave Pawson’s XSLT FAQ

W3School’s XSL School

MSDN’s XML Center

Xml101.com

Jeni’s XSLT Pages

TopXML: XSLT Reference

Nic Miloslav’s XSLT Tutorial

Chapter 20: Ten XSLT Processors Available Online

Saxon

msxsl

Sablotron

Xalan-C++

Xalan-Java

LotusXSL

XT

jd.xslt

XML::XSLT

libxslt

Glossary

Introduction

Everyone seems to be talking about Extensible Markup Language (XML) these days. You know how mothers are — I can’t even visit my relatives over the holidays without my mom broaching the topic of XML at Thanksgiving dinner. Yes, XML has become quite a buzzword, but Extensible Stylesheet Language Transformations (XSLT) is the power that takes all this XML and turns it into something useful and flexible.

XSLT is a language used to transform XML documents into something new. It can take one XML document and output that document’s information into a completely different structure or turn XML into an HTML document for viewing on the Web. XSLT isn’t a general-purpose programming language, such as Java or Visual Basic; its focus is solely on transforming XML.

Before I get any farther along, I have to point out the “elephant in the room” — XSLT’s long-winded name. Who came up with that name anyway? I think the people responsible should be forced to say, “Extensible Stylesheet Language Transformations,” aloud ten times and hope their tongues don’t fall off! XSLT’s full name may be a mouthful, but this book carves up each piece of XSLT into manageable, chewable morsels.

XSLT can be confusing if you don’t have a solid understanding of its syntax, quirky abbreviations, and the esoteric terminology it sometimes uses. But XSLT For Dummies can help you write XSLT stylesheets and, just as important, grasp why and how transformations work. In addition, see www.dummies.com/extras/xsltfd for code examples as well as a helpful editing tool of my own creation which I call the X-Factor.

Above all, you can use XSLT For Dummies as your guide for quickly developing the know-how of XSLT — without drowning in technical gobbledygook.

What I Assume About You

XSLT For Dummies starts from the bottom and works its way up: It doesn’t assume that you have any previous knowledge of XSLT or XML. If you have some know-how of HTML or programming, I think you can grasp some of the concepts of XSLT quicker than a beginner, but I don’t expect you to know HTML or anything about programming beforehand.

How This Book Is Organized

My aim is to help you find out how to become productive with XSLT quickly so that you can transform XML documents into virtually any kind of output imaginable. With that goal in mind, XSLT For Dummies is divided into five parts, each of which builds upon the previous ones.

Part I: Getting Started with XSLT

Part I kicks off your journey. You start by finding out about the core concepts of XSLT and how it fits in with HTML and all the other X technologies out there, such as XML, XSL, and XPath. You get your feet wet by writing your first XSLT stylesheet and transforming an XML document.

After you finish that, you can say XSLT to your buddies at work and actually have a grasp of what you’re talking about when you throw around those X terms.

Part II: Becoming an XSLT Transformer

This part takes you into the belly of the beast: stylesheets, template rules, and XPath expressions. But don’t worry — you won’t lose your appetite. You begin by looking at stylesheets and find out, in everyday terms, the often-confusing subject of document trees and recursion. From there, you start pulling information out of XML documents and outputting it into various forms.

By the time you’re done with this part, you’ll be able to say, “Extensible Stylesheet Language Transformations,” without stumbling over any of the twelve syllables.

Part III: Prime Time XSLT

In this part, you dive deeper into the thick of things. You find out how XSLT handles programming concepts such as if/then statements, loops, and variables, and how to include them in stylesheets. Don’t worry if you’ve never programmed before; these concepts become clear as you read through the chapters. You also find out about how to take advantage of the more advanced capabilities of XSLT and XPath to create more powerful transformations.

I predict that after you finish this part, at least once you’ll have unintentionally ordered an XSLT sandwich on rye at your local deli.

Part IV: eXtreme XSLT

As you read through Part IV, you can begin to call yourself a bona fide XSLT Transformer. You find out how to create effective XSLT stylesheets and apply them under various conditions. You find out about how to combine stylesheets and even add your own extensions. You also get the inside scoop on debugging transformations.

A word of warning: By now, all this XSLT will be swimming around in your head. You may find yourself mingling at a social event and leading with the line: “Apply any good templates lately?”

Part V: The Part Of Tens

In this part, I guide you through some practical tips and information that can make your life easier when you work with XSLT. I start out by demystifying the ten most confusing things about XSLT. Then I detail what I consider to be the ten best XSLT resources on the Web. I conclude by giving you the details on ten XSLT processors that you can download online.

Conventions Used in This Book

Snippets of XSLT code are scattered throughout the book and are often used to introduce you to a feature of the markup language. They appear in the chapters like this:

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”> 

  <xsl:template match=”id”> 

     <h1><xsl:apply-templates/></h1>

  </xsl:template>  

</xsl:stylesheet> 

If you type these stylesheets by hand, pay close attention and type the text exactly as shown to ensure that the stylesheet transforms properly. Generally, spaces don’t matter much, although depending on where they are, they could change the look of the output from a transformation. However, case sensitivity is important for any XML or XSLT element. I use lowercase text in all the examples, so I recommend getting used to typing lowercase to avoid confusion or problems down the line.

If XSLT element names or instructions appear within the regular text, they look like this.

Icons Used in This Book

Tips draw attention to key points that can save you time and effort.

Pay close attention to this icon; it highlights something that’s important to your understanding of XSLT or how to use it.

Heed the Warning icon because it can save you from the pitfalls of XSLT pain and agony.

Part I

Getting Started with XSLT

In this part . . .

Y ou watched the X-Files and then you saw X-Men on the big screen, but these were only warm-ups for the real deal — the X-Team. In this part, you find out all about the X-Team members, including XML, XSL, XSLT, and XPath, and how they work together. You also get your feet wet by transforming your first XML document using XSLT.

Chapter 1

Introducing the X-Team

In This Chapter

Finding out about XML, XSL, XSLT, and XPath

Knowing the difference between XSL and XSLT

Looking at the X-Team from an HTML perspective

A s a sports fan, I enjoy watching all kinds of team sports, whether football, basketball, baseball, or an Olympic team competition. I’ve noticed that regardless of the sport, great teams have two things in common. First, they have very talented individuals on them. Second, they function well as a team; I find hardly anything more thrilling in sports than seeing a squad of talented athletes working together cohesively. (Of course, it goes without saying that the most exciting part of sports is the “I’m going to Disneyworld” commercials!)

Although this book focuses on eXtensible Stylesheet Language Transformations, or XSLT, you’ll quickly discover that XSLT is an important component of a closely related set of technologies that I affectionately call the X-Team. This “Dream X-Team” consists of: XML, XSL, XSLT, and XPath. (For the techies out there, that’s shorthand for eXtensible Markup Language, eXtensible Stylesheet Language, XSL Transformations, and XML Path Language.) Each of these technologies is powerful, but each gets its true strength through interrelationships. So, although I concentrate much of the attention in this book on XSLT, never think of it as something independent of its teammates.

As you start your XSLT journey, I begin by introducing you to the X-Team members, each of which has a separate but intertwined responsibility.

XML: Storing Your Data

The original member of the X-Team is eXtensible Markup Language (XML), the granddaddy of them all. All other X-Team members are designed to work with or act upon XML. A relatively recent innovation, XML was conceived primarily by Jon Bosak as a way to make working with information delivered over the Web easier. Then in 1998, XML was standardized by the World Wide Web Consortium (W3C), the international standards body for the Web.

Since its beginnings, the Web has used HyperText Markup Language (HTML) to display content. HTML documents are stored on Web servers and then sent on demand to a Web browser, such as Microsoft Internet Explorer or Netscape Navigator. The browser then displays the HTML as a Web page. Figure 1-1 illustrates this process.

Figure 1-1: Displaying information over the Web.

HTML comes up short

HTML has become so wildly popular largely because it’s very easy to learn and work with; heck, even my 7-year-old can create a Web page using Microsoft FrontPage, and my 9-year-old can write HTML by hand. The markup language was originally designed purely as a way to format and lay out information. However, because people have wanted to use the Web for nearly every task under the sun, HTML has been forced to do far more than was ever intended.

Consider a familiar scenario: A company wants to put information stored in a database onto its Web site. A sampling of its data might look something like Table 1-1.

To present this information on the Web, these database records must be converted into HTML text and formatted properly as a table so that they can be viewed in a Web browser.

<table border=”1”>

  <tr> 

    <th>ID</th>

    <th>Name</th>

    <th>City</th>

    <th>St</th>

    <th>Zip</th>

  </tr>

  <tr> 

    <td>100</td>

    <td>Ray Kinsella</td>

    <td>Anderson</td>

    <td>IN</td>

    <td>46011</td>

  </tr>

  <tr> 

    <td>101</td>

    <td>Rick Blaine</td>

    <td>Manchester</td>

    <td>NH</td>

    <td>02522</td>

  </tr>

</table>

Look closely at the above code to see how HTML falls short. I turn meaningful clusters of information into a format that looks good in a browser but isn’t useful for much else. In a database, related fields such as ID, Name, and Address make up a customer record, but after they have been converted to HTML, they’re just row and column formatting instructions and their contents — thus, the concept of a customer is gone.

Such a solution would be acceptable if you only want to display information in a Web browser, but many people are discovering needs that go far beyond that. For example, searching for information within an HTML document is very limited. How would I be able to retrieve from my HTML file the names of all of my customers from Indiana who spend over $1,000 annually? That kind of query is far beyond the scope of HTML. And, even if I were to develop some convoluted way to get this information through JavaScript, I’d have to throw all that away if I ever wanted to move my information to another non-HTML environment, such as a Java application, Windows program, or even a cellular phone.

Think of HTML as a sort of information blender: Add a dash of data and a pinch of formatting instructions into the pitcher, turn the power on high, and out comes a pureed mixture of the two. Like creating a milkshake by mixing ice cream, chocolate syrup, vanilla, and milk in a blender, imagine the impossibility of trying to extract the vanilla from the milkshake after it’s been blended. This no-win backward mobility is the futile attempt to mine useful information from HTML documents.

In other words: Yes, the shake tastes great, but don’t try to use the raw materials again for a different purpose.

XML to the rescue

Developed as a response to the information-blender effect of HTML, XML is simply a practical way to work with structured information on the Web. The motivation of its inventors was to assemble structured data into something that was similar to HTML — so that data could be easily readable by people like you and me — but different enough from HTML so that it’s freely expandable to effectively describe the data that it contains.

Whether you realize it or not, almost all the information used on the Web has a natural structure or organization to it and thus can be expressed using XML. Some everyday examples include:

The contents of a letter

<letter> 

  <date>March 31, 2002</date>

  <salutation>Dear Sir:</salutation>

  <text>Thanks for your recent article on Swiss Cheese chips. 

  However, I don’t think you gave enough credit to the farmer 

  who invented the Swiss Cheese chip - Charley Cowley.</text> 

  <closing>Warm Regards,</closing>

  <signature>Mrs. Charlie Cowley</signature>

</letter>

Dialogue from a movie

<dialogue>

  <rick>I’m saying it because it’s true. Inside of us, we both know you belong with Victor. You’re part of his work, the thing that keeps him going. If that plane leaves the ground and you’re not with him, you’ll regret it. Maybe not today. Maybe not tomorrow, but soon and for the rest of your life.</rick>

  <ilsa>But what about us?</ilsa>

  <rick>We’ll always have Paris. We didn’t have, we, we lost it until you came to Casablanca. We got it back last night.</rick>

  <ilsa>When I said I would never leave you.</ilsa>

  <rick> And you never will. But I’ve got a job to do, too. Where I’m going, you can’t follow. What I’ve got to do, you can’t be any part of. Ilsa, I’m no good at being noble, but it doesn’t take much to see that the problems of three little people don’t amount to a hill of beans in this crazy world. Someday you’ll understand that. Now, now... Here’s looking at you kid.</rick>

</dialogue>

Those customer records of Ray and Rick

<customers>

  <customer>

    <id>100</id>

    <name>Ray Kinsella</name>

    <city>Anderson</city>

    <state>IN</state>

    <zip>46011</zip>

  </customer>

  <customer>

    <id>101</id>

    <name>Rick Blaine</name>

    <city>Manchester</city>

    <state>NH</state>

    <zip>02522</zip>

  </customer>

</customers>

A Web page

<html>

  <head>

    <title>My Home Page</title> 

  </head>

  <body>

    <h1>Heading</h1>

  </body> 

</html>

From these examples, you can see that XML describes information in a very logical and straightforward manner. Put descriptive tags before and after the text values and you’ve just about got an XML document. XML isn’t rocket science!

HTML is standardized with a fixed set of formatting tags or elements to define different parts of a document. An <h1> element identifies a Level 1 Header, and <b> denotes bolded text. In contrast, the only thing standardized about XML is its syntax rules, not its actual tags; this is what makes XML so flexible. For example, a bank can define a set of XML tags to describe its financial data:

<account id=”10001010”>

  <type>Checking</type>

  <rating level=”-5”/>

  <customer preferred=”no way, hosea”>

    <firstname>John</firstname>

    <lastname>Charles</lastname>

    <address>123 Main Street</address>

    <city>Fremont</city> 

    <state>CA</state>

    <zip>94425</zip>

  </customer>

</account>

Or, a pizza store chain can come up with its own set of XML elements that describes their pizzas.

<pizza>

  <size value=”Mega”/>

  <crust type=”Thick and Chewy”/>

  <toppings>Olives, Sausage, Pepperoni, Lima Beans</toppings> 

  <cookingtime>30</cookingtime>

</pizza>

A set of defined XML tags used for a particular purpose is an XML vocabulary.

However, as great as it is at organizing information, XML by its very nature is a raw material. XML is of little use by itself and needs help from its X-Team teammates to actually make its information usable in the real world.

XSL: Presenting XML Data

Time to pass the baton to the second member of the X-Team: Extensible Stylesheet Language (XSL). XSL is charged with styling or laying out XML documents into a form that makes sense to its intended audience. As shown in Figure 1-2, you use XSL to define a set of formatting rules that are referred to when an XML document is processed.

For example, if I want to format the letter from the preceding “XML to the rescue” section, I use XSL to create a few rules, such as

When a <date> element is found, italicize the date’s text.

When a <salutation> element is found, indent salutation’s text.

When a <closing> element is found, add an extra line after it.

Figure 1-2: Using XSL to apply formatting to XML documents.

XSL rules like these are contained in an XSL Stylesheet, which is just a plain text file filled with formatting instructions that look like the following example.

     <fo:page-sequence master-name=”easy”> 

          <fo:flow flow-name=”xsl-region-body”> 

               <fo:block font-family=”Serif”>Serif font</fo:block> 

          </fo:flow> 

     </fo:page-sequence> 

Note that this XSL is written in something that resembles XML. That is more than mere coincidence because, ironically, XSL is actually written in XML and is itself an XML vocabulary.

If your head is spinning around, hang on. XSL is used to format XML, which in turn is used as the language for defining XSL. The circular logic can be confusing, but fortunately, you don’t need to worry about the particulars of how that all works — just know that it does. Actually, the fact that XSL uses XML to describe its instructions makes it far easier to learn than trying to grasp yet another language syntax.

When XSL was conceived by the W3C, the original intention of XSL was simply to provide a way to format XML data. However, after people began to use XML in the real world, it was discovered that something more was needed besides assigning font colors and margin indentions to the content. True, developers needed to be able to style XML in a way that was easily readable, but they also discovered a need to change an XML document from one XML structure to another, as well as to have the ability to easily convert XML into HTML and other output options. Taking up this charge, the W3C expanded the scope of XSL to support transforming, and in doing so, gave birth to XSL Transformations (XSLT).

XSLT: Transforming Your XML Data

The third member of the X-Team is XSL Transformations (XSLT). XSLT is analogous to that high-priced rookie on a professional sports team that unseats the veteran player: XSL was supposed to be the killer technology to enable XML to achieve widespread adoption, but XSLT’s ability to convert XML data into any wanted output has proven so compelling that it essentially swallowed up XSL. In fact, when people today talk about XSL, they’re almost always referring to XSLT.

XSL is actually composed of two independent parts: XSLT for transforming XML from one structure to another; and XSL Formatting Objects and Formatting Properties for formatting XML documents.

The key reason for all this enthusiasm in support of XSLT is that XML documents often need to serve multiple audiences. The XML needs to be formatted so that it can be viewed in a Web browser, and the same XML may need to be tweaked to conform to a new trading partner’s own XML structure. See Figure 1-3 for an illustration of this relationship.

Figure 1-3: XSLT transforms XML into a variety of outputs.

To illustrate, suppose that I want to change the XML definition of a customer from the original format of

<customer>

<id>101</id>

<name>Rick Blaine</name>

<city>Manchester</city>

<state>NH</state>

<zip>02522</zip>

</customer>

into this:

<customer id=”101”>

<fullname>Rick Blaine</fullname>

<address city=”Manchester” state=”NH”

zipcode=”02522”/>

</customer>

Before XSLT came along, I’d have to dust off my programming software, bury myself in a cave for a day, and write a program to do this migration process. However, with XSLT, I can transform the data from one XML format to another nearly instantly, with no true programming required.

XSLT is not a programming language as such. In fact, when written out, it doesn’t even look anything like C++, Java, or Visual Basic. Like its XSL parent, XSLT rules and templates are defined by using XML.

Most programming languages transform data structures through blood, sweat, and tears. In contrast, XSLT does this work in what can best be described as transforming by example — you provide an example of what kind of information you’d like to see, and XSLT does the rest. For example, the following XSLT snippet changes the name element to fullname in the output document.

<xsl:template match=”name”>

  <fullname>

  <xsl:apply-templates/>

  </fullname>

</xsl:template>

(I get into the specifics of how XSLT template rules work in Chapter 4.)

However, as powerful as XSLT is, it needs help to do its transformational magic from our last X-Team member: XPath. XPath specializes in picking out the specific nuggets of information from one XML document in order for XSLT to fit it neatly into another one.

XPath: Seeking Out Your Data

XPath is the spy or seeker of the X-Team who is charged with going into an XML document and picking out the requested information for XSLT. Without the ability to precisely locate information in an XML document, the ability to transform or do anything special with XML is minimal.

Any XSLT transformation must be set up to answer two questions:

Input: What information from the original XML document do you want?

Output: How would you like that information structured in the output document?

XSLT relies on XPath to answer the first question, as shown in Figure 1-4.

Figure 1-4: XSLT uses XPath to retrieve data from XML documents.

XSLT looks at an XML document element by element, so XPath expressions are used to tell what your XSLT stylesheet should look for as it goes through the XML document. Looking closer at the preceding XSLT example, the XPath expression name tells XSLT what information to look for, which in this case is to look for all name elements.

<xsl:template match=”name”>

  <fullname>

  <xsl:apply-templates/>

  </fullname>

</xsl:template>

This XPath expression is intuitive and easy to understand, but for more hearty needs, the syntax can be quite arcane and challenging. (I discuss XPath in detail in Chapter 5.)

Interestingly, much of the effort required to develop XSLT stylesheets is related to the input side of the equation, so throughout this book, I spend a sizeable amount of time on how to use XPath.

The X-Team through HTML Eyes

You may be approaching the X-Team after having already worked with HTML. If so, when you look at XML and XSLT, it’s natural to view these new technologies through HTML eyes. Having a knowledge of HTML definitely gives you a head start in learning XML syntax; noting the similarities and differences between them is important.

Although I compare HTML and XML in this section, remember that XSL and XSLT stylesheets are both written using XML, so the same rules apply to them as to XML.

XML looks a lot like HTML . . .

If you can read HTML, you quickly see that XML looks an awful lot like HTML in terms of its syntax. For example, a document title in HTML is defined as

<title>My Document Title</title> 

Like HTML, the element is the primary building block of XML. Therefore, a book title in XML might be defined to look something like this:

<book>War and Peace</book> 

Additionally, XML follows HTML in using name-value pairs inside elements to provide additional descriptive information about an element.

<invoice id=”110”>

  <company>Polar Salsa Corporation</company>

</invoice> 

In this XML snippet, the id attribute provides additional information related to the invoice element.

But XML isn’t the same as HTML . . .

HTML and XML have a definite likeness, but you should watch out for some significant variations in syntax rules. The three most important are as follows.

XML is well-formed

HTML has always been lenient in some of its syntax requirements, not always forcing you to have closing tags on some of the elements, such as the paragraph (<p>) element. For example, both of the following lines are valid HTML:

<p>Hello. My name is Inigo Montoya. You killed my father. Prepare to die.

and

<p>Hello. My name is Inigo Montoya. You killed my father. Prepare to die.</p>

In contrast, XML is much more rigid: All XML documents must be well-formed, meaning that every begin tag needs to have a matching end tag:

<president>Andrew Jackson</president>

XML allows shortcuts

Although XML requires any element to have a start and end tag pair, it does allow you to combine the two tags if the element is empty, meaning that no text is provided between the start and end tags. For example, the following two lines are equivalent.

<device id=”3838-2020”></device>

and

<device id=”3838-2020”/>

XML is case sensitive

HTML is case insensitive, so long as you spell out the tag syntax correctly, the document is processed appropriately. Each of the following are valid HTML statements.

<body bgcolor=”#FFFFFF”></body>

<BODY BGCOLOR=”#FFFFFF”></BODY>

<Body Bgcolor=”#FFFFFF”></Body>

On the other hand, XML is case sensitive, so the following statements aren’t considered equal.

<quote>Get used to disappointment.</quote>

<QUOTE>Get used to disappointment.</QUOTE>

To avoid confusion, you should consistently use either all lower- or uppercase characters for the XML, XSL, and XSLT documents that you create. However, I recommend consistently using lowercase characters because this is the convention that nearly everyone follows.

Chapter 2

Writing Your First XSLT Stylesheet

In This Chapter

Preparing your XSLT working environment

Creating a simple XML document

Writing an XSLT stylesheet

Running an XSLT processor

Creating a result tree

G rasping XSLT is kind of like putting together a jigsaw puzzle. Even my 7-year-old knows that the best way to approach a puzzle is to first connect all the pieces of the outer border and then work your way inside, eventually filling in all the missing spaces.

In a sense, this book follows that same pattern. Chapter 1 defines the “conceptual” edges of the XSLT puzzle by talking about the cast of characters, the X-team. This chapter fills in the remaining edge pieces as it deals with the “mechanics” of XSLT — the materials you need and the process you follow for doing transformations. After you finish this chapter, the puzzle edge will all be connected; the rest of the book then focuses on filling in all that’s left of this jigsaw puzzle.

Preparing Your XSLT Working Environment

Every craftsman needs tools to perform his or her job adequately, and as a new XSL transformer, you are no exception. You need two primary software programs to add to your tool belt — a text editor and an XSLT processor.

Some software tools actually combine the capabilities of the text editor and XSLT processor within a single application. X-Factor, a tool I describe in the next section, is such an example.

Text editor

XML and XSLT documents are ordinary text files, so finding software that allows you to create and edit them is a breeze. You simply need a text editor. If you are Windows user, you can use Notepad. Or if you already have a favorite editor, then feel free to use it.

However, to help make the task of learning XSLT easier, I recommend you start by using X-Factor as your editor. X-Factor is an easy-to-use software program specifically designed to be a learning tool for XSLT. The application enables you to open up your XML source and XSLT stylesheet at the same time, perform transformations with the click of a button, and view the results of the transformation inside the application window (see Figure 2-1).

X-Factor is available as a free download from the XSLT For Dummies Web site at www.dummies.com/extras/xsltfd or from Nombas, Inc. at www.nombas.com