0% found this document useful (0 votes)
23 views60 pages

Sams Teach Yourself XML in 24 Hours 3rd Ed Edition Morrison: - Click The Link Below To Download

Uploaded by

meuthmegli2y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views60 pages

Sams Teach Yourself XML in 24 Hours 3rd Ed Edition Morrison: - Click The Link Below To Download

Uploaded by

meuthmegli2y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Visit https://ebookfinal.

com to download the full version and


explore more ebook

Sams teach yourself XML in 24 hours 3rd ed Edition


Morrison

_____ Click the link below to download _____


https://ebookfinal.com/download/sams-teach-yourself-
xml-in-24-hours-3rd-ed-edition-morrison/

Explore and download more ebook at ebookfinal.com


Here are some recommended products that might interest you.
You can download now and explore!

Sams Teach Yourself C in 24 Hours Complete Starter Kit 3rd


Edition Sams Teach Yourself in 24 Hours Jesse Liberty

https://ebookfinal.com/download/sams-teach-yourself-c-in-24-hours-
complete-starter-kit-3rd-edition-sams-teach-yourself-in-24-hours-
jesse-liberty/
ebookfinal.com

Sams teach yourself networking in 24 hours 3rd ed Edition


Habraken

https://ebookfinal.com/download/sams-teach-yourself-networking-
in-24-hours-3rd-ed-edition-habraken/

ebookfinal.com

Sams Teach Yourself PHP in 24 Hours 3rd Edition Matt


Zandstra

https://ebookfinal.com/download/sams-teach-yourself-php-
in-24-hours-3rd-edition-matt-zandstra/

ebookfinal.com

Sams Teach Yourself Adobe Premiere Pro in 24 Hours Sams


Teach Yourself 1st Edition Jeff Sengstack

https://ebookfinal.com/download/sams-teach-yourself-adobe-premiere-
pro-in-24-hours-sams-teach-yourself-1st-edition-jeff-sengstack/

ebookfinal.com
SAMS Teach Yourself Adobe Photoshop CS2 in 24 Hours 3rd
Edition Carla Rose

https://ebookfinal.com/download/sams-teach-yourself-adobe-photoshop-
cs2-in-24-hours-3rd-edition-carla-rose/

ebookfinal.com

Sams Teach Yourself Adobe Illustrator 10 in 24 Hours Peter


Bauer

https://ebookfinal.com/download/sams-teach-yourself-adobe-
illustrator-10-in-24-hours-peter-bauer/

ebookfinal.com

Sams teach yourself DVD authoring in 24 hours 1st Edition


Jeff Sengstack

https://ebookfinal.com/download/sams-teach-yourself-dvd-authoring-
in-24-hours-1st-edition-jeff-sengstack/

ebookfinal.com

Sams Teach Yourself Macromedia Fireworks MX in 24 Hours


1st Edition Jackson West

https://ebookfinal.com/download/sams-teach-yourself-macromedia-
fireworks-mx-in-24-hours-1st-edition-jackson-west/

ebookfinal.com

Sams teach yourself Adobe Photoshop 7 in 24 hours 1st


Edition Carla Rose

https://ebookfinal.com/download/sams-teach-yourself-adobe-
photoshop-7-in-24-hours-1st-edition-carla-rose/

ebookfinal.com
Sams teach yourself XML in 24 hours 3rd ed Edition
Morrison Digital Instant Download
Author(s): Morrison, Michael
ISBN(s): 9782005933163, 2005933163
Edition: 3rd ed
File Details: PDF, 5.92 MB
Year: 2006
Language: english
Teach
Yourself

XML
Michael Morrison
in 24
Hours
THIRD EDITION

800 East 96th Street, Indianapolis, Indiana, 46240 USA


Sams Teach Yourself XML in 24 Hours, Third Edition Publisher
Paul Boger
Copyright  2006 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in Acquisitions Editor
a retrieval system, or transmitted by any means, electronic, mechanical, Loretta Yates
photocopying, recording, or otherwise, without written permission from
the publisher. No patent liability is assumed with respect to the use of Development Editor
the information contained herein. Although every precaution has been Mark Renfrow
taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions. Nor is any liability assumed for Managing Editor
damages resulting from the use of the information contained herein. Charlotte Clapp
International Standard Book Number: 0-0672-32797-X
Senior Project Editor
Library of Congress Catalog Card Number: 2005933163 Matthew Purcell
Printed in the United States of America
Copy Editor
First Printing: November 2005
Kate Givens
08 07 06 05 4 3 2 1
Indexer
Trademarks Chris Barrick
All terms mentioned in this book that are known to be trademarks or
Proofreader
service marks have been appropriately capitalized. Sams Publishing
Susan Eldridge
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
Technical Editor
or service mark.
Alan Moffet

Warning and Disclaimer Publishing Coordinator


Every effort has been made to make this book as complete and as Cindy Teeters
accurate as possible, but no warranty or fitness is implied. The
information provided is on an “as is” basis. The author(s) and the Designer
publisher shall have neither liability nor responsibility to any person Gary Adair
or entity with respect to any loss or damages arising from the
information contained in this book.

Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered
in quantity for bulk purchases or special sales. For more information,
please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsoned.com
Contents at a Glance

Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Part I: XML Essentials

1 Getting to Know XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7


2 Creating XML Documents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Part II: Defining XML Data

3 Defining Data with DTD Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43


4 Digging Deeper into XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5 Putting Namespaces to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6 Using SVG to Draw Scalable Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7 Using XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8 Validating XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Part III: Formatting and Displaying XML Documents

9 XML Formatting Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173


10 Styling XML Content with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
11 Getting Started with XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
12 Transforming XML with XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
13 Access Your iTunes Music Library via XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
14 Formatting XML with XSL-FO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
15 Using XML to Hack Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

Part IV: Processing and Managing XML Data

16 Parsing XML with DOM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329


17 SAX: The Simple API for XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
18 Querying XML Data with Xquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
19 Using XML with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
20 Using XML to Assist Search Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Part V: XML’s Impact on HTML

21 Adding Structure to the Web with XHTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421


22 Addressing and Linking XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
23 Going Wireless with WML and XHTML Mobile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
24 Syndicating the Web with RSS News Feeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

Part VI: Appendix

A XML Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Table of Contents

Introduction 1
How This Book Is Structured . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
How to Use This Book ............................................................................4

Part I: XML Essentials

HOUR 1: Getting to Know XML 7


The What and Why of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
XML and Web Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Real-World XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

HOUR 2: Creating XML Documents 21


A Quick XML Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Selecting an XML Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Constructing Your First XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Viewing Your XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Part II: Defining XML Data

HOUR 3: Defining Data with DTD Schemas 43


Creating Your Own Markup Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Schemas and XML Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45


Comparing Schema Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

The Importance of Document Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

DTD Construction Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Digging Deeper into Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55


Putting Attributes to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

A Complete DTD Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64


vi
Sams Teach Yourself XML in 24 Hours

HOUR 4: Digging Deeper into XML Documents 71


Leaving a Trail with Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Characters of Text in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

The Wonderful World of Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

The Significance of Notations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Working with CDATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Using XML to Create an Online Radio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

HOUR 5: Putting Namespaces to Use 89


Understanding Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Naming Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Declaring and Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

HOUR 6: Using SVG to Draw Scalable Graphics 101


What Is SVG? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
SVG and Related Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Inside the SVG Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Creating an SVG Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Embedding an SVG Drawing in a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

HOUR 7: Using XML Schema 129


XML Schema Construction Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

Working with Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133


Digging into Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Namespaces and XSD Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
A Complete XML Schema Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

RELAX NG and the Future of XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

HOUR 8: Validating XML Documents 157


Document Validation Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Validation Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

Repairing Invalid Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165


vii
Contents

Part III: Formatting and Displaying XML Documents

HOUR 9: XML Formatting Strategies 173


Style Sheets and XML Formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Leveraging CSS, XSLT, and XSL-FO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Style Sheets in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

HOUR 10: Styling XML Content with CSS 189


Getting to Know CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

Wiring a Style Sheet to an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

Your First CSS Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

Inside CSS Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

The Ins and Outs of Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

Your Second Complete Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

HOUR 11: Getting Started with XSL 223


XSL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

The Pieces and Parts of XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

An XSLT Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Wiring an XSL Style Sheet to an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Your First XSLT Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

HOUR 12: Transforming XML with XSLT 245


A Closer Look at XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Pattern Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

Putting Expressions to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

A Complete XSLT Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257


Yet Another XSLT Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

HOUR 13: Access Your iTunes Music Library via XML 271
The Role of XML in iTunes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

Digging Into the iTunes Library File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273


Linking to Tracks in the iTunes Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

Building an iTunes Web Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278


viii
Sams Teach Yourself XML in 24 Hours

HOUR 14: Formatting XML with XSL-FO 287


What Is XSL-FO? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Working with the XSL-FO Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Validating an XSL-FO Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Converting an XSL-FO Document to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

A More Complete XSL-FO Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

HOUR 15: Using XML to Hack Google Maps 305


Getting to Know Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Google Maps Customization Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

Brainstorming a Custom Mapping Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

Developing a Custom Map Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

Hacking Together a Custom Google Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

Part IV: Processing and Managing XML Data

HOUR 16: Parsing XML with the DOM 329


What Is the DOM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

How the DOM Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330


DOM Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

Binding XML Data to a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

Using the DOM to Access XML Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336


A Complete DOM Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Updating the DOM Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

HOUR 17: SAX: The Simple API for XML 349


What Is SAX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Writing Programs That Use SAX Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Obtaining a SAX Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Using SAX with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

Inside the SAX Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356


ix
Contents

HOUR 18: Querying XML Data with XQuery 367


What Is XQuery? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

XML Data Querying 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

Processing XQuery Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

Getting to Know Saxon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Practical XML Querying with XQuery and Saxon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

HOUR 19: Using XML with Databases 383


A Quick Relational Database Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

The World’s Shortest Guide to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

Databases and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390

Exporting an XML Document from a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392

Accessing Data from a Database as XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

HOUR 20: Using XML to Assist Search Engines 401


Web Crawling Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

Getting to Know Google Sitemaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

Inside the Google Sitemap Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

Creating Your Own Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

Validating Your Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410


Submitting Your Sitemap to Google . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

Using an Automated Sitemap Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

Part V: XML’s Impact on HTML

HOUR 21: Adding Structure to the Web with XHTML 421


XHTML: A Logical Merger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421

Comparing XHTML and HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

Creating and Validating XHTML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424


Migrating HTML to XHTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
x
Sams Teach Yourself XML in 24 Hours

HOUR 22: Addressing and Linking XML Documents 439


Understanding XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440

Navigating a Document with XPath Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442


Using XPath Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
The Role of XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
HTML, XML, and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Addressing with XPointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

Linking with XLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459

A Complete XLink Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

HOUR 23: Going Wireless with WML and XHTML Mobile 471
XML and the Wireless Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
WML Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Creating WML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476

Blending WML with XHTML Mobile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

HOUR 24: Syndicating the Web with RSS News Feeds 499
A Brief History of RSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Using an RSS News Aggregator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502

Inside the RSS 2.0 Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504


Creating and Validating a News Feed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Displaying a News Feed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509

Part VI: Appendix

APPENDIX A: XML Resources 519


General XML Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

XML Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

XML-Based Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521


XML Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522

Index 525
About the Author

Michael Morrison is a writer, developer, toy inventor, and author of a variety of technolo-
gy books, including BlackBerry In a Snap, Beginning Mobile Phone Game Programming, and
Teach Yourself HTML and CSS in 24 Hours. In addition to his primary profession as a writer
and freelance nerd for hire, Michael is the creative lead at Stalefish Labs, an entertainment
company he co-founded with his wife, Masheed. The first web-based project for Stalefish
Labs is an online music game called Guess That Groove (http://www.guessthatgroove.com/).
When not glued to his computer, playing hockey, skateboarding, or watching movies with
his wife, Michael enjoys hanging out by his koi pond. You can visit Michael on the web
and discuss this book at http://www.michaelmorrison.com/.
Dedication

To my wife, Masheed, the love of my life.

Acknowledgments

Thanks to Loretta Yates, Mark Renfrow, Charlotte Clapp, Matt Purcell, and the rest of the
gang at Sams Publishing for giving me yet another writing opportunity, and for being
such a pleasure to work with, as always.

A big thanks also goes to my buddy Stephen Tallent, who graciously offered up his
considerable XML knowledge in the wee hours of the night as I peppered him with
email questions.

And finally, the biggest thanks of all goes to my wonderful family and friends.
We Want to Hear from You!

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 publisher for Sams Publishing, I welcome your comments. You can email 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 better.

Please note that I cannot help you with technical problems related to the topic of this book. We
do have a User Services group, however, where I will forward specific technical questions related
to the book.

When you write, please be sure to include this book’s title and author as well as your name,
email address, and phone number. I will carefully review your comments and share them
with the author and editors who worked on the book.

Email: feedback@samspublishing.com

Mail: Paul Boger


Publisher
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA

For more information about this book or another Sams Publishing title, visit our website at
www.samspublishing.com. Type the ISBN (excluding hyphens) or the title of a book in the
Search field to find the page you’re looking for.
This page intentionally left blank
Introduction
Of all the software technologies that have come and gone in the relatively short time since
we all plugged in to the Web, few have been as far-reaching yet misunderstood as XML.
Even with its catchy name that conjures up images of extreme sports, the true nature of
XML continues to elude many technical people. The reason has to do with the fact that
XML is very much a behind-the-scenes technology that helps to ensure that data is
structured in an orderly fashion. There are very few situations where an end-user can see
XML at work in a practical application. In this way, XML is a lot like residential building
codes. When a house is built, thousands of building codes are used to guide contractors so
that the house goes up safe and sound. As a homeowner, it’s difficult to look at a finished
house and grasp how all these building codes impacted the wood, shingles, and brick that
you can see and feel. The building codes are abstract in a sense that you can’t touch them,
but they play a critical role in the construction process all the same. XML plays a similar
role in software, including web sites, operating systems, and distributed applications.

I often hear people describe XML as “the new HTML,” which sounds good but is not very
accurate. XML, unlike HTML, is an extremely broad data-structuring standard that has impli-
cations far beyond web pages. For example, consider this question: HTML is to web pages as
XML is to what? This is a difficult question to answer because XML isn’t really geared toward
any one solution. Instead, XML provides the framework for creating customized solutions to a
wide range of problems. This is made possible through XML-based markup languages, which
are custom markup languages that you create using XML. If you want to chart the statistics
of your child’s baseball team, you could create your own Little League Markup Language, or
LLML, which includes custom tags and attributes for keeping up with important stats such as
hits, runs, errors, and parental outbursts. The high degree of structure in your Little League
data would allow it to be easily sorted, manipulated, and displayed according to your needs;
the data would have the mathematical flexibility of a spreadsheet along with the visual
accessibility of a web page. XML makes all this possible.

Maybe you have bigger plans for your XML knowledge than just tracking stats for a Little
League team. If so, you’ll be glad to know that XML is the enabling technology behind all
kinds of interesting software applications. Practically all of the big Internet players have
invested heavily in XML. As an example, Amazon.com uses XML to expose its product data
so that developers can build custom shopping applications. Another interesting application
of XML that has caused quite a stir recently is Google Maps, which is Google’s innovative
online mapping application. Google Maps relies on XML for map data. In fact, in Hour
15 of this book, “Using XML to Hack Google Maps,” you learn how to “hack” Google Maps
2
Teach Yourself in 24 Hours

to use your own XML-based maps. One last example of how XML may have sneakily
entered your life already is iTunes, Apple’s incredibly popular online music store. iTunes
uses XML to store information about your music library locally on your computer. With
a little bit of effort, you can access your iTunes music library via XML and view or
manipulate it any way you choose. This task is covered in Hour 13, “Access Your iTunes
Music Library via XML.”

XML is worth learning because it is an excellent back-end technology for storing and
sharing data in a highly structured manner. Another reason for learning XML has to do
much more directly with the web: XML is very much shaping the future of HTML. As you
may know, HTML is somewhat unstructured in the sense that web developers take great
liberties with how they use HTML code. Although this isn’t entirely HTML’s fault, HTML
shares a considerable amount of the blame because it doesn’t have the structured set of
rules that are part of XML. In an attempt to add structure and consistency to the Web,
a reformulated version of HTML known as XHTML was created that adds the structure of
XML to HTML. It may still be quite a while before XHTML fully unseats HTML, but web
developers are busy making the move to a more structured Web thanks to XHTML.

This book, in many ways, is a testament to the fact that XML is a technology for both the
present and the future. The majority of the book focuses on XML in the present and how it
can be used to do interesting things today. My goal was to strike a careful balance between
giving you practical knowledge for the present along with some foreshadowing of what
might lie ahead for XML.

How This Book Is Structured


As the title suggests, this book is organized into 24 lessons that are intended to take about
an hour each to digest. Don’t worry, there are no penalties if you take more than an hour
to finish a given lesson, and there are no special prizes if you speed through them faster!
The hours themselves are grouped together into five parts, each of which tackles a different
facet of XML:

. Part I, “XML Essentials”—In this part, you get to know the XML language and
what it has to offer in terms of structuring data. You also learn how to create XML
documents.

. Part II, “Defining XML Data”—In this part, you will find out how to define the
structure of XML documents using schemas. You learn about the two major types of
schemas (DTDs and XSDs), as well as how to use namespaces and how to validate
XML documents. You even learn about a real XML language, SVG, along with how
to use it to create vector graphics for the web.
3
Introduction

. Part III, “Formatting and Displaying XML Documents”—In this part, you will learn
how to format XML content with style sheets so that it can be displayed. XML
formatting is explored using several different style sheet technologies—CSS, XSLT,
and XSL-FO. This part of the book also guides you through some interesting practical
applications, including how to access your iTunes music library using XML and how
to create your own XML-based maps for Google Maps.

. Part IV, “Processing and Managing XML Data”—In this part, you will find out how to
process XML documents and manipulate their contents using the Document Object
Model (DOM), which provides access to the inner workings of XML documents. You
will also learn about SAX, which is a popular programming interface used to access
XML documents. Databases are also tackled, including how to access data from a
database via XML. And finally, this part of the book shows you how to use XML to
mine Google for information.

. Part V, “XML’s Impact on HTML”—In this part, you will explore XML’s relationship to
HTML and the Web. You will learn about XHTML, which is the merger of XML
and HTML, along with advanced XML linking technologies. You will also learn how
XML is being used to provide a means of creating web pages for wireless devices via
a language called WML, as well as how to syndicate the web using RSS news feeds.

What You’ll Need


This book assumes you have some familiarity with a markup language, such as HTML.
You don’t have to be an HTML guru by any means, but it definitely helps if you understand
the difference between a tag and an attribute. Even if you don’t, you should be able to
tackle XML without too much trouble. It will also help if you have experience using a web
browser. Even though there are aspects of XML that reach beyond the web, this book focuses
a great deal on using web browsers to view and test XML code. For this reason, I encourage
you to download and install the latest release of a major web browser such as Internet
Explorer, Mozilla Firefox, Opera, or Safari.

In addition to web browsers, there are a few other tools mentioned throughout the book
that you may consider downloading or purchasing based upon your individual needs. At
the very least, you’ll need a good text editor to edit XML documents. Windows Notepad is
sufficient if you’re working in a Windows environment, and I’m sure you can find a suit-
able equivalent for other environments. If you want to check into a more full-featured XML
editor, it certainly won’t hurt you. I mention several editors to consider in Hour 2 of the
book, “Creating XML Documents.” That’s really all you need; a web browser and a trusty
editor will carry you a long way toward becoming proficient in XML.
4
Teach Yourself in 24 Hours

How to Use This Book


In code listings, line numbers have been added for reference purposes. These line numbers
aren’t part of the code. The code used in this book is also on this book’s web site at
http://www.samspublishing.com.

This book uses different typefaces to differentiate between code and regular English. Text
that you type and text that appears on your screen is presented in monospace type.

It will look like this to mimic the way text looks on your screen.

Placeholders for variables and expressions appear in monospace italic font. You should
replace the placeholder with the specific value it represents.

In addition, the following elements appear throughout the book:

By the Notes provide you with comments and asides about the topic at hand.
Way
PART I
XML Essentials

HOUR 1 Getting to Know XML 7

HOUR 2 Creating XML Documents 21


This page intentionally left blank
HOUR 1
Getting to Know XML

World domination isn’t my thing, but if it was, I’d be using XML.


—Norman Walsh

As you undoubtedly know, the World Wide Web has grown in leaps and bounds in the past
several years, both in magnitude and in technologies. The fact that people who, only
a few short years ago had no interest in computers are now “net junkies”, is a testament
to how quickly the Web has infiltrated modern culture. Just as the usefulness and appeal of
the Web have grown rapidly, so have the technologies that make the Web possible. It all
pretty much started with HTML (HyperText Markup Language), but a long list of acronyms,
buzzwords, pipedreams, and even a few killer technologies have since followed. XML
(eXtensible Markup Language) is one of the rare technologies that actually progressed from
bleeding edge hype to misunderstood buzzword to standard building block. XML has
officially arrived, and is used behind the scenes in countless applications and web services.
Even so, I’ll leave it to you to decide whether it is truly a killer technology as you progress
through this book.

XML’s usage is continuing to grow quickly as both individuals and companies realize its
potential. However, in many ways XML is still a relatively new technology, and many
people, possibly you, are just now learning what it can do for them. Unlike some other
software technologies such as HTML or even Java, XML is a little fuzzier in terms of how it
is applied in different scenarios. Just as it’s difficult to look at a person and grasp how their
DNA makes them who they are, it can also be challenging to look at an application and
get a grasp for how XML fits into the equation. This hour introduces you to XML and gives
you some insight as to why it was created and what it can do.

In this hour, you’ll learn

. Exactly what XML is


. The relationship between XML and HTML
. How XML fits into web browsers
. How XML is impacting the real world
8 HOUR 1: Getting to Know XML

The What and Why of XML


With the universe expanding, human population increasing at an alarming rate
across the globe, and a new boy band created every week, was it really necessary to
introduce yet another web technology with yet another cryptic acronym? In the
case of XML, the answer is yes. Next to HTML itself, XML is positioned to have
the most widespread and long-term ramifications of any web technology to date.
The interesting thing about XML is that its impact has gone and will continue to
go largely unnoticed by most web users. Unlike HTML, which reveals itself in flashy
text and graphics, XML is more of an under-the-hood kind of technology. If HTML
is the fire engine red paint and supple leather interior of a sports car, XML is the
turbocharged engine and sport suspension. Okay, maybe the sports car analogy is
a bit much, but you get the idea that XML’s impact on the Web is hard to see with
the naked eye. However, the benefits are directly realized in all kinds of different
ways. More specifically, if you’ve ever shopped on Amazon.com, purchased music
from Apple iTunes, or read a syndicated news feed via RSS (Really Simple
Syndication), you’ve used XML without realizing it.

By the By the way, you might as well get used to seeing loads of acronyms. Virtually every
Way technology associated with XML has its own acronym, so it’s impossible to learn
about XML without getting to know a few dozen acronyms. Don’t worry, I’ll break
them to you gently!

A Quick History of HTML


To understand the need for XML, at least as it applies to the Web, you have to first
consider the role of HTML. In the early days of the Internet, some European
physicists created HTML by simplifying another markup language known as SGML
(Standard Generalized Markup Language). I won’t get into the details of SGML, but
let’s just say it was overly complicated, at least for the purpose of sharing scientific
documents on the Internet. So, pioneering physicists created a simplified version of
SGML called HTML that could be used to create what we now know as web pages.
The creation of HTML represented the birth of the World Wide Web—a layer of visual
documents that resides on the global network known as the Internet.

HTML was great in its early days because it allowed scientists to share information
over the Internet in an efficient and relatively structured manner. It wasn’t until
later that HTML started to become an all-encompassing formatting and display
language for web pages. It didn’t take long before web browsers caught on and
HTML started being used to code more than scientific papers. HTML quickly went
from a tidy little markup language for researchers to a full-blown online publishing
The What and Why of XML 9

language. And once it was established that HTML could be jazzed up simply by
adding new tags, the creators of web browsers pretty much went crazy by adding lots
of nifty features to the language. Although these new features were neat at first, they
compromised the simplicity of HTML and introduced lots of inconsistencies when it
came to how browsers rendered web pages. HTML had started to resemble a bad
remodeling job on a house that really should’ve been left alone.

As with most revolutions, the birth of the Web was very chaotic, and the modifica-
tions to HTML reflected that chaos. More recently, a significant effort has been made
to reel in the inconsistencies of HTML and to attempt to restore some order to the
language. The problem with disorder in HTML is that web browsers have to guess
at how a page is to be displayed, which is not a good thing. Ideally, a web page
designer should be able to define exactly how a page is to look and have it look
the same regardless of what kind of browser or operating system someone is using.
This utopia is still off in the future somewhere, but XML is playing a significant role
in leading us toward it, and significant progress has been made.

Getting Multilingual with XML


XML is a meta-language, which is a fancy way of saying that it is a language used
to create other markup languages. I know this sounds a little strange, but it really
just means that XML provides a basic structure and set of rules to which any
markup language must adhere. Using XML, you can create a unique markup
language to model just about any kind of information, including web page content.
Knowing that XML is a language for creating other markup languages, you could
create your own version of HTML using XML. You could also create a markup
language called VPML (Virtual Pet Markup Language), for example, which you
could use to create and manage virtual pets. The point is that XML lays the ground
rules for organizing information in a consistent manner, and that information can
be anything from web pages to virtual pets.

Throughout this book you will learn about several of the more intriguing markup By the
languages that are based on XML. For example, you will find out about SVG and Way
RSS, which allow you to create vector graphics and syndicate news feeds from web
sites, respectively.

You might be thinking that virtual pets don’t necessarily have anything to do with
the Web, so why mention them? The reason is because XML is not entirely about
web pages. In fact, XML in the purest sense really has nothing to do with the Web,
and can be used to represent any kind of information on any kind of computer. If
you can visualize all the information whizzing around the globe between computers,
10 HOUR 1: Getting to Know XML

mobile phones, televisions, and radios, you can start to understand why XML has
much broader ramifications than just cleaning up web pages. However, one of the
first applications of XML is to restore some order to the Web, which is why I’ve
provided an explanation of XML with the Web in mind. Besides, one of the main
benefits of XML is the ability to develop XML documents once and then have them
viewable on a range of devices, such as desktop computers, handheld computers,
mobile phones, and Internet appliances.

One of the really awesome things about XML is that it looks very familiar to anyone
who has used HTML to create web pages. Going back to our virtual pet example,
check out the following XML code, which reveals what a hypothetical VPML
document might look like:
<pets>
<pet name=”Maximillian” type=”pot bellied pig” age=”3”>
<friend name=”Augustus”/>
<friend name=”Nigel”/>
</pet>
<pet name=”Augustus” type=”goat” age=”2”>
<friend name=”Maximillian”/>
</pet>
<pet name=”Nigel” type=”chipmunk” age=”2”>
<friend name=”Maximillian”/>
</pet>
</pets>

This XML (VPML) code includes three virtual pets: Maximillian the pot-bellied pig,
Augustus the goat, and Nigel the chipmunk. If you study the code, you’ll notice that
tags are used to describe the virtual pets much as tags are used in HTML code to
describe web pages. However, in this example the tags are unique to the VPML
language. It’s not too hard to understand the meaning of the code, thanks to the
descriptive tags. In fact, an important design parameter of XML was for XML content
to always be human-readable. By studying the VPML code for a few seconds, it
becomes apparent that Maximillian is friends with both Augustus and Nigel, but
Augustus and Nigel aren’t friends with each other. Maybe it’s because they are the
same age, or maybe it’s just that Maximillian is a particularly friendly pig. Either
way, the code describes several pets along with the relationships between them. This
is a good example of the flexibility of the XML language. Keep in mind that you
could create a virtual pet application that used VPML to share information with
other virtual pet owners.

By the Unlike HTML, which consists of a predefined set of tags such as <head>, <body>,
Way and <p>, XML allows you to create custom markup languages with tags that are
unique to a certain type of data, such as virtual pets.
The What and Why of XML 11

The virtual pet example demonstrates how flexible XML is in solving data structur-
ing problems. Unlike a traditional database, XML data is pure text, which means it
can be processed and manipulated very easily, in addition to being readable by
people. For example, you can open up any XML document in a text editor such as
Windows Notepad (or TextEdit on Macintosh computers) and view or edit the code.
The fact that XML is pure text also makes it very easy for applications to transfer
data between one another, across networks, and also across different computing
platforms such as Windows, Macintosh, and Linux. XML essentially establishes
a platform-neutral means of structuring data, which is ideal for networked
applications, including web-based applications.

XML isn’t just for web-based applications, however. As an example, the entire By the
Microsoft Office line of products use XML under the hood to store and share Way
document data.

The Convergence of HTML and XML


Just as some Americans are apprehensive about the proliferation of spoken
languages other than English, some web developers initially feared XML’s role in the
future of the Web. Although I’m sure a few HTML purists still exist, is it valid to view
XML as posing a risk to the future of HTML? And if you’re currently an HTML expert
and have yet to explore XML, will you have to throw all you know out the window
and start anew with XML? The answer to both of these questions is a resounding no!
In fact, once you fully come to terms with the relationship between XML and HTML,
you’ll realize that XML actually complements HTML as a web technology. Perhaps
more interesting is the fact that XML is in many ways a parent to HTML, as opposed
to a rival sibling—more on this relationship in a moment.

Earlier in the hour I mentioned that the main problem with HTML is that it got
somewhat messy and unstructured, resulting in a lot of confusion surrounding the
manner in which web browsers render web pages. To better understand XML and its
relationship to HTML, you need to know why HTML has gotten messy. HTML was
originally designed as a means of sharing written ideas among scientific researchers.
I say “written ideas” because there were no graphics or images in the early versions
of HTML. So, in its inception, HTML was never intended to support fancy graphics,
formatting, or page-layout features. Instead, HTML was intended to focus on the
meaning of information, or the content of information. It wasn’t until web browser
vendors got excited that HTML was expanded to address the presentation of
information. In fact, HTML was in many ways changed to focus entirely on how
information appears, which is what ultimately prompted the creation of XML.
12 HOUR 1: Getting to Know XML

You’ll learn throughout this book that one of the main goals of XML is to separate the
meaning of information from the presentation of it. There are a variety of reasons
why this is a good idea, and they all have to do with improving the organization
and structure of information. Although presentation plays an important role in any
web site, modern web applications have evolved to become driven by data of very
specific types, such as financial transactions. HTML is a very poor markup language
for representing such data. With its support for custom markup languages, XML
makes it possible to carefully describe data and the relationships between pieces of
data. By focusing on content, XML allows you to describe the information in web
documents. More importantly, XML makes it possible to precisely describe informa-
tion that is shuttled across the Net between applications. For example, Amazon.com
uses XML to describe products on its site and allow developers to create applications
that intelligently analyze and extract information about those products.

By the You might have noticed that I’ve often used the word “document” instead of
Way “page” when referring to XML data. You can no longer think of the web as a bunch
of linked pages. Instead, you should think of it as linked documents. Although this
may seem like a picky distinction, it reveals a lot about the perception of web
content. A page is an inherently visual thing, whereas a document can be anything
ranging from a stock quote to a virtual pet to a music CD on Amazon.com.

If XML describes data better than HTML, does it mean that XML is set to upstage
HTML as the markup language of choice for the Web? Not exactly. XML is not
a replacement for HTML, or even a competitor of HTML. XML’s impact on HTML
has to do more with cleaning up HTML than it does with dramatically altering
HTML. The best way to compare XML and HTML is to remember that XML
establishes a set of strict rules that any markup language must follow. HTML is
a relatively unstructured markup language that could benefit from the rules of
XML. The natural merger of the two technologies is to make HTML adhere to the
rules and structure of XML. To accomplish this merger, a new version of HTML
has been formulated that adheres to the stricter rules of XML. The new XML-
compliant version of HTML is known as XHTML. You learn a great deal more
about XHTML in Hour 21, “Adding Structure to the Web with XHTML.” For now,
just understand that one long-term impact XML will have on the Web has to do
with cleaning up HTML.

By the Most standardized web technologies, such as HTML and XML, are overseen by the
Way W3C, or the World Wide Web Consortium, which is an organizational body that
helps to set standards for the Web. You can learn more about the W3C by visiting
its web site at http://www.w3.org/.
XML and Web Browsers 13

XML’s relationship with HTML doesn’t end with XHTML, however. Although XHTML
is a great idea that is already making web pages cleaner and more consistent for
web browsers to display, we’re a ways off from seeing a Web that consists of cleanly
structured XHTML documents (pages). It’s currently still too convenient to take
advantage of the freewheeling flexibility of the HTML language. Where XML is
making a significant immediate impact on the Web is in web-based applications
that must shuttle data across the Internet. XML is an excellent medium for
representing data that is transferred back and forth across the Internet as part of
a complete web-based application. In this way, XML is used as a behind-the-scenes
data transport language, whereas HTML is still used to display traditional web
pages to the user. This is evidence that XML and HTML can coexist happily both
now and into the future.

XML and Web Browsers


One of the stumbling blocks to learning XML is figuring out exactly how to use
it. You now understand how XML complements HTML, but you still probably
don’t have a good grasp on how XML data is used in a practical scenario. More
specifically, you’re probably curious about how to view XML data. Because XML is
all about describing the content of information, as opposed to the appearance of
information, there is no such thing as a generic XML viewer, at least not in the sense
that a web browser is an HTML viewer. In this sense, an “XML viewer” is simply an
application that lets you view XML code, which can be a simple text editor or a
visual editor that shows how XML data is structured. To view XML code according to
its actual meaning, you must use an application that is specially designed to work
with a specific XML language. If you think of HTML as an XML language, then a
web browser is an application designed specifically to interpret the HTML language
and display the results. This is, in fact, exactly what happens when you view an
XHTML web page in a browser.

Another way to view XML documents is with style sheets using either XSL
(eXtensible Stylesheet Language) or CSS (Cascading Style Sheets). Style sheets have
finally reached the mainstream and are established as a better approach to
formatting web pages than many of the outdated HTML presentation tags. Style
sheets work in conjunction with HTML code to describe in more detail how HTML
data is to be displayed in a web browser. Style sheets play a similar role when used
with XML. Most modern web browsers (Internet Explorer, Firefox, Opera, Safari,
and so on) support CSS, as well as providing some level of support for XSL. You
learn a great deal more about style sheets in Part III, “Formatting and Displaying
XML Documents.”
14 HOUR 1: Getting to Know XML

By the In addition to popular commercial web browsers, the W3C offers its own open
Way source web browser that can be used to browse XML documents. The Amaya web
browser supports the editing of web documents and also serves as a decent
browser. However, Amaya is intended more as a means of testing XML documents
than as a commercially viable web browser. You can download Amaya for free from
the W3C web site at http://www.w3c.org/Amaya/.

In addition to style sheets, there is another important XML-related technology that is


supported in major web browsers. I’m referring to the DOM (Document Object Model),
which allows you to use a scripting language such as JavaScript to programmatically
access the data in an XML document. The DOM makes it possible to create web
pages that intelligently access and display XML data based upon scripting code. You
learn how to access XML documents using JavaScript and the DOM in Hour 16,
“Parsing XML with DOM.”

One last point to make in regard to viewing XML with web browsers is that some
browsers allow you to view XML code directly. This is a neat feature because it
automatically highlights the code so that the tags and data are easy to see and
understand. Additionally, an XML document is usually displayed as a hierarchical
tree that allows you to expand and collapse sections of the data just as you expand
and collapse folders in a file manager such as Windows Explorer. This hierarchical
user interface reveals the tree-like structure of XML documents. Figure 1.1 shows the
virtual pets XML document as viewed in Internet Explorer.

FIGURE 1.1
You can view
the code for an
XML document
by opening the
document in a
web browser
that supports
XML, such as
Internet
Explorer.
Real-World XML 15

Although this black and white figure doesn’t reveal it, Internet Explorer actually uses
color to help distinguish the different pieces of information in the document. To expand
or collapse a section in the document, just click anywhere on the tag. Figure 1.2 shows
the document with the first pet element (Maximillian) collapsed; notice that the minus
sign changes to a plus sign (+) to indicate that the element can be expanded.

FIGURE 1.2
In addition to
highlighting
XML code for
easier viewing,
XML-supported
web browsers
make it possi-
ble to expand
and collapse
sections of
a document.

Keep in mind that the web browser in this case is only showing the XML document as
a tree of data because it doesn’t know anything else about how to render it. You can
provide a style sheet that lays out the specifics of how the data is to be formatted and
displayed, and the browser will carefully format the data instead of displaying it as a
tree. You will tackle this topic in Part III. This approach is commonly used to style XML
data for viewing on the Web. Even so, it can be handy opening an XML document in
a browser without any styling applied (as shown in Figures 1.1 and 1.2) and studying it
as a tree of data. Although browsers provide a neat approach to viewing XML code in
a tree-like structure, you’ll probably rely on an XML editor to view most of the XML
code that you develop. Or you can use a simple text editor such as Windows Notepad.
You learn about XML editors in the next hour, “Creating XML Documents.”

Real-World XML
Hopefully by now you understand some of the reasons XML came into being, as
well as how it will likely fit in with HTML as the future of the web unfolds. What
I haven’t explained yet is how XML is impacting the real world with new markup
16 HOUR 1: Getting to Know XML

languages. Fortunately, a lot of work has been done to make XML a technology that
you can put to work immediately, and there are numerous XML-related technologies
that are being introduced as I write this. Following is a list of some of the major
XML-based languages that are supported either on the web or in major XML-based
applications, along with the kinds of information they represent:

. WML (Wireless Markup Language)—Web pages for mobile devices


. OFX (Open Financial Exchange)—Financial information (electronic funds
transfer, for example)

. RDF (Resource Description Framework)—Descriptions of information in web


pages

. RSS (Really Simple Syndication)—Syndicated web site updates (news feeds and
blog entries, for example)

. MathML (Mathematical Markup Language)—Mathematical symbols and


formulas

. OeB (Open eBook)—Electronic books


. OpenDocument—Open file format for office applications (word processing,
spreadsheet, and so on)

. OWL (Web Ontology Language)—Semantic web pages (an extension of RDF)


. P3P (Platform for Privacy Preferences)—Web privacy policies
. SOAP (originally Simple Object Access Protocol)—Distributed application
communication

. SVG (Scalable Vector Graphics)—Vector graphics


. SMIL (Synchronized Multimedia Integration Language)—Multimedia
presentations

. UDDI (Universal Description, Discovery, and Integration)—Business registries


. WSDL (Web Services Description Language)—Web services
. XAML (eXtensible Application Markup Language)—Graphical user interfaces
(used by Microsoft in the new version of Windows, codenamed Longhorn)

. XBRL (eXtensible Business Reporting Language)—Business and financial data

I told you earlier that XML people love acronyms! And as the brief descriptions of
each language suggest, these XML languages are as varied as their acronyms. A few
of these languages are supported in the latest web browsers, and the remaining
Summary 17

languages have special applications that can be used to create and share data in
each respective format. To give you an idea regarding how these languages are
impacting the real world, consider the fact that the next major release of the
Windows operating system, codenamed Longhorn, is using XAML (pronounced
“zammel”) throughout to describe its user interfaces in XML. Additionally, Microsoft
and Intuit have invested heavily in OFX (Open Financial eXchange) as the future of
electronic financial transactions. OFX is already supported by more than 2,000
banks and brokerages, in addition to payroll-processing companies. In other words,
your paycheck may already depend on XML!

Another interesting usage of an XML language is SVG, which is used to code plats By the
for real estate. A plat is an overhead map that shows how property is divided. Plats
Way
play an important role in determining divisions of land for ownership (and taxation)
purposes and comprise the tax maps that are managed by the property tax
assessor’s office in each county in the U.S. SVG is actually much more broad than
just real estate plats and allows you to create virtually any vector graphics in XML.
You learn more about SVG in Hour 6, “Using SVG to Draw Scalable Graphics.”

I could go on and on about how different XML languages are infiltrating the real
world, but I think you get the idea. You’ll get to know several of the languages
listed throughout the remainder of the book. More specifically, Hour 23, “Going
Wireless with WML and XHTML Mobile,” shows you how to code web pages for
mobile devices, while Hour 24, “Syndicating the Web with News Feeds via RSS,”
shows you how to use the RSS language to efficiently stay up to date with your
favorite web sites.

As more evidence of the importance that major technology players have placed By the
on XML, consider the fact that Microsoft’s .NET development platform is based Way
entirely upon XML.

Summary
Although it doesn’t solve all of the world’s problems, XML has a lot to offer the
web community and the computing world as a whole. Not only does XML
represent a path toward a cleaner and more structured HTML, it also serves as
an excellent means of transporting data in a consistent format that is readily
accessible across networks and different computing platforms. A variety of
different XML-based languages are available for storing different kinds of
information, ranging from financial transactions to mathematical equations to
multimedia presentations.
18 HOUR 1: Getting to Know XML

This hour introduced you to XML and helped to explain how it came to be as well as
how it fits into the future of the Web. You also learned that XML has considerable
value beyond its impact on HTML. This hour, although admittedly not very hands
on, has given you enough knowledge of XML to allow you to hit the ground running
and begin creating your own XML documents in the next hour.

Q&A
Q. Why isn’t it possible to create custom tags in HTML, as you can in XML?

A. HTML is a markup language that consists of a predefined set of tags that each
has a special meaning to web browsers. If you were able to create custom tags
in HTML, web browsers wouldn’t know what to do with them. XML, on the
other hand, isn’t necessarily tied to web browsers, and therefore has no notion
of a predefined set of tags. When you do tie an XML document to a web
browser, you typically couple it with a style sheet that styles or transforms the
XML data into a format more easily displayed in the browser.

Q. Is it necessary to create a new XML-based markup language for any kind of


custom data that I’d like to store?
A. No. Although you may find that your custom data is unique enough to
warrant a new markup language, you’ll find that a variety of different XML-
based languages are already available. In fact, most major industries have
developed or are working on standardized markup languages to handle the
representation of industry-specific data. OFX (Open Financial eXchange) is
a good example of an industry-specific markup language that is already being
used widely by the financial industry. Additionally, you may find that your
XML needs are so simple that you can format data in XML without the
formality of creating your own full-blown XML language.

Workshop
The Workshop is designed to help you anticipate possible questions, review what
you’ve learned, and begin learning how to put your knowledge into practice.

Quiz
1. What is meant by the description of XML as a meta-language?

2. What is XHTML?
Workshop 19

3. What organizational body oversees standardized web technologies such as


HTML and XML?

Quiz Answers
1. When XML is referred to as a meta-language, it means that XML is a language
used to create other markup languages. Similar to a meta-language is meta-
data, which is data that is used to describe other data. XML relies heavily on
metadata to add meaning to the content in XML documents. RDF and OWL
are examples of XML vocabularies that expand on the concept of metadata by
attempting to add meaning to web pages.

2. XHTML is the XML-compliant version of HTML, which you will learn about in
Hour 21.

3. Most standardized web technologies, such as HTML and XML, are overseen by
the World Wide Web Consortium, or W3C, which is an organizational body
that helps to set standards for the Web.

Exercises
1. Consider how you might construct a custom markup language for data of your
own. Do you have a collection of movie posters you’d like to store in XML, or
how about your Wiffle ball team’s stats? What kind of custom tags would you
use to code this data?

2. Visit the W3C Web site at http://www.w3.org/ and browse around to get a feel
for the different web technologies overseen by the W3C.
This page intentionally left blank
HOUR 2
Creating XML Documents

Plain Old Text still reigns supreme, despite the many predictions of its demise.
—Bob Foster

Similar to HTML, XML is a technology that is best understood by working with it. I could
go on and on for pages about the philosophical ramifications of XML, but in the end I’m
sure you just want to know what you can do with it. Most of your XML work will consist of
developing XML documents, which are sort of like HTML web pages, at least in terms of
how the code is generally structured. Keep in mind, however, that XML documents can be
used to store any kind of information. After you’ve created an XML document, you will no
doubt want to see how it appears in a web browser or how it is used in a functioning
application. Because there is no standard approach to viewing an XML document according
to its meaning, you must either find or develop a custom application for viewing the
document or use a style sheet to view the document in a web browser. This hour uses the
latter approach to provide a simple view of an XML document that you create.

In this hour, you’ll learn

. The basics of XML


. How to select an XML editor
. How to create XML documents
. How to view XML documents

A Quick XML Primer


You learned in the previous hour that XML is a markup language used to create other
markup languages. Because HTML is a markup language, it stands to reason that XML
documents should in some way resemble HTML documents. In fact, you saw in the
previous hour how an XML document looks a lot like an HTML document, with the
obvious difference that XML documents can use custom tags. So, instead of seeing <head>
and <body> you saw <pet> and <friend>. Nonetheless, if you have some experience with
coding web pages in HTML, XML will be very familiar. You will find that XML isn’t nearly
as lenient as HTML, so you may have to unlearn some bad habits carried over from HTML.
22 HOUR 2: Creating XML Documents

Of course, if you don’t have any experience with HTML you probably won’t even
realize that XML is a somewhat rigid language.

XML Building Blocks


Like some other markup languages, XML relies heavily on three fundamental
building blocks: elements, attributes, and values. An element is used to describe or
contain a piece of information; elements form the basis of all XML documents.
Elements consist of two tags: an opening tag and a closing tag. Opening tags appear
as words contained within angle brackets (<>), such as <pet> or <friend>. Closing
tags also appear within angle brackets, but they have a forward-slash (/) just before
the tag name. Examples of closing tags are </pet> and </friend>. Elements always
appear as an opening tag, followed by optional data, followed by a closing tag:
<pet>
</pet>

In this example, there is no data appearing between the opening and closing tags,
which illustrates that the data is indeed optional. XML doesn’t care too much about
how whitespace appears between tags, so it’s perfectly acceptable to place tags
together on the same line:
<pet></pet>

Keep in mind that the purpose of tags is to denote pieces of information in an


XML document, so it is rare to see a pair of tags with nothing between them, as
the previous two examples show. Instead, tags typically contain text content or
additional tags. Following is an example of how the pet element can contain
additional content, which in this case is a couple of friend elements:
<pet>
<friend />
<friend />
</pet>

By the It’s important to note that an element is a logical unit of information in an XML
Way document, whereas a tag is a specific piece of XML code that comprises an
element. That’s why I always refer to an element by its name, such as pet, whereas
tags are always referenced just as they appear in code, such as <pet> or </pet>.

You’re probably wondering why this code broke the rule requiring that every element
has to consist of both an opening and a closing tag. In other words, why do the
friend elements appear to involve only a single tag? The answer to this question is
that XML allows you to abbreviate empty elements. An empty element is an element
that doesn’t contain any content within its opening and closing tags. The earlier pet
A Quick XML Primer 23

examples you saw are empty elements. Because empty elements don’t contain any
content between their opening and closing tags, you can abbreviate them by using
a single tag known as an empty tag. Similar to other tags, an empty tag is enclosed
by angle brackets (<>), but it also includes a forward slash (/) just before the closing
angle bracket. So, the empty friend element, which would normally be coded as
<friend></friend> can be abbreviated as <friend />. The space before the />
isn’t necessary but is a standard style practice among XML developers.

Any discussion of opening and closing tags wouldn’t be complete without pointing By the
out a glaring flaw that appears in most HTML documents. I’m referring to the <p> Way
tag, which is used to enclose a paragraph of text, and is often found in HTML
documents with an opening tag but no closing tag. The p element in HTML is not
an empty element, and therefore should always have a </p> closing tag, but most
HTML developers make the mistake of leaving it out. This kind of freewheeling
coding will get you in trouble quickly with XML!

All this talk of empty elements brings to mind the question of why you’d want to
use an element that has no content. The reason for this is because you can still
attach attributes to empty elements. Attributes are small pieces of information that
appear within an element’s opening tag. An attribute consists of an attribute name
and a corresponding attribute value, which are separated by an equal symbol (=).
The value of an attribute appears to the right of the equal symbol and must appear
within quotes. Following is an example of an attribute named name that is
associated with the friend element:
<friend name=”Augustus” />

Attributes represent another area where HTML code is often in error, at least from By the
the perspective of XML. HTML attributes are regularly used without quotes, which Way
is a clear violation of XML syntax. Always quoting attribute values is another habit
you’ll need to learn if you’re making the progression from free-spirited HTML
designer to ruthlessly efficient XML coder.

In this example, the name attribute is used to identify the name of a friend. Attributes
aren’t limited to empty elements—they are just as useful with nonempty elements.
Additionally, you can use several different attributes with a single element. Following
is an example of how several attributes are used to describe a pet in detail:
<pet name=”Maximillian” type=”pot bellied pig” age=”3”>

As you can see, attributes are a great way to tie small pieces of descriptive information
to an element without actually affecting the element’s content.
24 HOUR 2: Creating XML Documents

Inside an Element
A nonempty element is an element that contains content within its opening and
closing tags. Earlier I mentioned that this content could be either text or additional
elements. When elements are contained within other elements, they are known as
nested elements. To understand how nested elements work, consider an apartment
building. Individual apartments are contained within the building, whereas individ-
ual rooms are contained within each apartment. Within each room there may be
pieces of furniture that in turn are used to store belongings. In XML terms, the
belongings are nested in the furniture, which is nested in the rooms, which are
nested in the apartments, which are nested in the apartment building. Listing 2.1
shows how the apartment building might be coded in XML.

LISTING 2.1 An Apartment Building XML Example


1: <apartmentbldg>
2: <apartment>
3: <room type=”bedroom”>
4: <furniture type=”armoire”>
5: <belonging type=”t-shirt” color=”navy” size=”xl” />
6: <belonging type=”sock” color=”white” />
7: <belonging type=”watch” />
8: </furniture>
9: </room>
10: </apartment>
11: </apartmentbldg>

If you study the code, you’ll notice that the different elements are nested according
to their physical locations within the building. It’s important to recognize in this
example that the belonging elements are empty elements (lines 5–7), which is
evident by the fact that they use the abbreviated empty tag ending in />. These
elements are empty because they aren’t required to house (no pun intended!) any
additional information. In other words, it is sufficient to describe the belonging
elements solely through attributes.

It’s important to realize that nonempty elements aren’t just used for nesting purposes.
Nonempty elements often contain text content, which appears between the opening
and closing tags. Following is an example of how you might decide to expand the
belonging element so that it isn’t empty:
<furniture type=”desk”>
<belonging type=”letter”>
Dear Michael,
I am pleased to announce that you may have won our sweepstakes. You are
one of the lucky finalists in your area, and if you would just purchase
five or more magazine subscriptions then you may eventually win some
money. Or not.
</belonging>
</furniture>
A Quick XML Primer 25

In this example, my ticket to an early retirement appears as text within the belonging
element. You can include just about any text you want in an element, with the
exception of a few special symbols, which you learn about a little later in the hour.

XML’s Five Commandments


Now that you have a feel for the XML language, it’s time to move on and learn
about the specific rules that govern its usage. I’ve mentioned already that XML is
a more rigid language than HTML, which basically means that you have to pay
attention when coding XML documents. In reality, the exacting nature of the XML
language is actually a huge benefit to XML developers—you’ll quickly get in the
habit of writing much cleaner code than you might have been accustomed to writing
in HTML, which will result in more accurate and reliable code. The key to XML’s
accuracy lies in a few simple rules, which I’m calling XML’s five commandments:

1. Tag names are case sensitive.

2. Every opening tag must have a corresponding closing tag (unless it is abbrevi-
ated as an empty tag).

3. A nested tag pair cannot overlap another tag.

4. Attribute values must appear within quotes.

5. Every document must have a root element.

Admittedly, the last rule is one that I haven’t prepared you for, but the others should
make sense to you. First off, rule number one states that XML is a case-sensitive
language, which means that <pet>, <Pet>, and <PET> are all different tags. If you’re
coming from the world of HTML, this is a very critical difference between XML and
HTML. It’s not uncommon to see HTML code that alternates back and forth between
tags such as <b> and <B> for bold text. In XML, this mixing of case is a clear no-no.
Generally speaking, XML standards encourage developers to use either lowercase
tags or mixed case tags, as opposed to the uppercase tags commonly found in HTML
web pages. The same rule applies to attributes. If you’re writing XML code in a spe-
cific XML-based markup language, the language itself will dictate what case you
should use for tags and attributes.

The second rule reinforces what you’ve already learned by stating that every open-
ing tag (<pet>) must have a corresponding closing tag (</pet>). In other words, tags
must always appear in pairs (<pet></pet>). Of course, the exception to this rule is
the empty tag, which serves as an abbreviated form of a tag pair (<pet />). Rule
three continues to nail down the relationship between tags by stating that tag pairs
Exploring the Variety of Random
Documents with Different Content
his cannibal's thought-pattern, torrents of perfectly stable electricity,
slowly influencing his own pattern as he had expected to influence
the impulse, influencing his into a frozen, stable, inflexible pattern of
continuous hunger-satisfaction.
His crystalloid jaws spread wide, Rahll floated in the black void of
space, motionless, unable to move, unable to twist into new thought-
patterns, and therefore unable to think. The inflexible current of
electricity poured steadily into his body....

Slowly the effects of the morphine wore off, and the haze lifted from
Brenner's mind. As his power to think returned, he began to realize
that his plan had worked, and that the alien had been overcome; and
he was glad of this only because it removed him, Brenner, from
danger. He no longer cared that the creature might have absorbed
the impulses of every living being in existence; the thing was frozen,
and he was safe from it—that was all that mattered to him.
Of course he could not see, or feel, or hear the motionless crystalloid
pattern outside. But he knew it was there. He could sense it.
He could sense it because his heretofore unmethodical hub-impulse
pattern, in its close association with Rahll's, had been slowly twisted
and molded into a form very much like the alien's, giving him Rahll's
powers of sightless observation of other impulses and objects.
Brenner no longer had any need for his lost powers of sight, hearing,
taste, smell and touch.
He groped his way back to the nose seat, sensing his way as he
could not feel it. He would find Base—he would be able to sense the
way back to it as soon as it was close enough. He would go back to
Base, because, naturally, his basic thought-form had also been
molded into the shape of Rahll's; and Brenner was hungry, and he
knew he could find food at Base.
Food. He pictured Hale, and the other men at the station.
Food. His jagged cannibal's pattern contracted in ecstacy at the
thought of it.
He activated the atomic generator and whirled the ship around.
THE END
*** END OF THE PROJECT GUTENBERG EBOOK PATTERN ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying copyright
royalties. Special rules, set forth in the General Terms of Use part of
this license, apply to copying and distributing Project Gutenberg™
electronic works to protect the PROJECT GUTENBERG™ concept
and trademark. Project Gutenberg is a registered trademark, and
may not be used if you charge for an eBook, except by following the
terms of the trademark license, including paying royalties for use of
the Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is very
easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund from
the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law in
the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms of
this agreement by keeping this work in the same format with its
attached full Project Gutenberg™ License when you share it without
charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears, or
with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.
1.F.6. INDEMNITY - You agree to indemnify and hold the
Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookfinal.com

You might also like