With this book, you’ll learn how to write documents in XML and how to use style sheets to convert those documents into HTML so that legacy browserscan read them.. You’ll also learn how t
Trang 2XML 1.1 Bible
3rd Edition
Elliotte Rusty Harold
Trang 4XML 1.1 Bible
3rd Edition
Trang 6XML 1.1 Bible
3rd Edition
Elliotte Rusty Harold
Trang 7Copyright 2004 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
permcoordinator@wiley.com.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS
A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS
IT MAY MAKE FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ
For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S at (800) 762-2974, outside the U.S at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2004101453
Trademarks: Wiley and and the Wiley Publishing logo are trademarks or registered trademarks of John Wiley & Sons, Inc.
and/or its affiliates in the United States and other countries, and may not be used without written permission All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Trang 8About the Author
Elliotte Rusty Harold is an internationally respected writer, programmer, and
educator, both on the Internet and off He got his start writing FAQ lists for theMacintosh newsgroups on Usenet and has since branched out into books, Websites, and newsletters He’s an adjunct professor of computer science atPolytechnic University in Brooklyn, New York His Cafe con Leche Web site at
http://www.cafeconleche.org/has become one of the most popular dent XML sites on the Internet
indepen-Elliotte is originally from New Orleans, to which he returns periodically in search of
a decent bowl of gumbo However, he currently resides in the Prospect Heightsneighborhood of Brooklyn with his wife Beth, and his cats Charm (named after thequark) and Marjorie (named after his mother-in-law) When not writing books, heenjoys working on genealogy, mathematics, free software, and quantum mechanics
His previous books include The Java Developer’s Resource, Java Network
Programming, Java Secrets, JavaBeans, Java I/O, XML: Extensible Markup Language, XML in a Nutshell, Processing XML with Java, and Effective XML.
Trang 9Mary Beth Wakefield
Vice President & Executive Group Publisher
Quality Control Technicians
Laura AlbertSusan MoritzCarl William Pierce
Permissions Editor
Laura Moss
Media Development Specialist
Greg Stafford
Proofreading and Indexing
TECHBOOKS Production Services
Trang 10Welcome to the third edition of the XML 1.1 Bible When the first edition was
published about five years ago, XML was a promising technology with asmall but growing niche In the last half decade, it has absolutely exploded XML nolonger needs to be justified as a good idea In fact, the question developers are ask-ing has changed from “Why XML?” to “Why not XML?” XML has become the dataformat of choice for fields as diverse as stock trading and graphic design More newprograms today are using XML than aren’t A solid understanding of just what XML
is and how to use it has become a sine qua non for the computer literate.
The XML 1.1 Bible, 3rd Edition is your introduction to the exciting and fast-growing
world of XML With this book, you’ll learn how to write documents in XML and how
to use style sheets to convert those documents into HTML so that legacy browserscan read them You’ll also learn how to use document type definitions (DTDs) andschemas to describe and validate documents You’ll encounter a variety of XMLapplications in many domains, ranging from finance to vector graphics to geneal-ogy And you’ll learn how to take advantage of XML for your own unique projects,programs, and web pages
What’s New in the Third Edition
The French philosopher and mathematician Blaise Pascal once wrote in a letter, “Ihave only made this longer because I have not had the time to make it shorter.” I
know how he felt The first edition of the XML Bible was written under great time
pressure, was finished well after deadline, and totaled more than 1000 pages, thelargest book I had written up to that point My favorite reader comment about thatedition was, “It would seem to me that if you asked the author to write 10,000 wordsabout the colour blue, he would be able to do it without breaking into a sweat.”While I probably could write 10,000 words about blue, for the third edition, I did try
to restrain myself and take the time to write more concisely I rewrote the bookfrom the ground up; and while I retained the basic flavor and outline that proved sopopular with the first edition, I tightened up the writing and cut many examplesdown to size With the benefit of five years of hindsight, I have also been able toexpand coverage of promising new technologies (schemas, XInclude, XHTML, SVG,XML Base, and RDDL) while eliminating coverage of applications that proved to beless useful than they initially appeared (WML, VML, CDF, HTML+TIME, RDF, and soon) The result is a more concise, approachable volume that covers more of whatyou need to know and less of what you don’t If you liked the first or second edition,you’re going to like the third edition even more I’m confident you’ll find this aneven more useful tutorial and reference
Trang 11Who You Are
Unlike most other XML books on the market, the XML 1.1 Bible, 3rd Edition
dis-cusses XML from the perspective of a web page author, not from the perspective of
a software developer I don’t spend a lot of time discussing BNF grammars or ing element trees Instead, I show you how you can use XML and existing toolstoday to more efficiently produce attractive, exciting, easy-to-use, easy-to-maintainweb sites that keep your readers coming back for more
pars-This book is aimed directly at web site developers I assume you want to use XML
to produce web sites that are difficult or impossible to create with raw HTML You’ll
be amazed to discover that in conjunction with style sheets and a few free tools,XML enables you to do things that previously required either custom software cost-ing thousands of dollars per site or extensive knowledge of programming languagessuch as Perl None of the software discussed in this book will cost you more than afew minutes of download time None of the tricks require any programming
What You Need to Know
XML does build on top of the underlying infrastructure of the Internet and the Web.Consequently, I will assume you know how to FTP files, send e-mail, and load URLsinto your web browser of choice I will also assume you have a reasonable knowl-edge of HTML On the other hand, when I discuss newer aspects of HTML that arenot yet in widespread use, such as Cascading Style Sheets, I discuss them in depth
To be more specific, in this book I assume that you can do the following:
✦ Write a basic HTML page, including links, images, and text, using a text editor
✦ Place that page on a web server
On the other hand, I do not assume that you
✦ Know SGML In fact, this preface is almost the only place in the entire bookyou’ll see the word SGML used XML is supposed to be simpler and morewidespread than SGML It can’t be that if you have to learn SGML first
✦ Are a programmer, whether of Java, Perl, C, or some other language XML is amarkup language, not a programming language You don’t need to be a pro-grammer to write XML documents
Trang 12What You’ll Learn
This book has one primary goal: to teach you to write XML documents for the Web
Fortunately, XML has a decidedly flat learning curve, much like HTML (and unlikeSGML) As you learn a little, you can do a little As you learn a little more, you can
do a little more Thus, the chapters in this book build steadily on one another Theyare meant to be read in sequence Along the way you’ll learn the following:
✦ How to author XML documents and deliver them to readers
✦ How semantic tagging makes XML documents easier to maintain and developthan their HTML equivalents
✦ How to post XML documents on web servers in a form everyone can read
✦ How to make sure your XML is well formed
✦ How to write with international characters such as and Æ
✦ How to validate documents against DTDs and schemas
✦ How to build large documents from smaller parts using entities and XInclude
✦ How to merge different XML vocabularies with namespaces
✦ How to format your documents with CSS and XSL style sheets
✦ How to connect documents with XLinks and XPointers
In the final part of this book, you’ll see several practical examples of XML beingused for real-world applications, including the following:
✦ Web site design
✦ Schemas
✦ Vector graphics
✦ Genealogy
How the Book Is Organized
This book is divided into five parts:
Trang 13By the time you finish reading this book, you’ll be ready to use XML to create pelling web pages
com-Part I: Introducing XML
Part I (Chapters 1 through 6) begins with the history and theory behind XML andthe goals XML is trying to achieve It shows you how the different pieces of the XMLequation fit together to enable you to create and deliver documents to readers.You’ll see several compelling examples of XML applications to give you some idea
of the wide applicability of XML, including Scalable Vector Graphics (SVG), theOpen Financial Exchange (OFX), the Mathematical Markup Language (MathML), theExtensible Forms Description Language (XFDL), and many others Then you’ll learn
by example how to write XML documents with tags that you define that make sensefor your document You’ll learn how to edit them in a text editor, attach style sheets
to them, and load them into a web browser such as Internet Explorer 5.0 or Mozilla
Part II: Document Type Definitions
Part II (Chapters 7 through 11) focuses on document type definitions (DTDs) ADTD specifies which elements are and are not allowed in an XML document, and theexact context and structure of those elements A validating parser can read a docu-ment, compare it to its DTD, and report any mistakes it finds DTDs enable docu-ment authors to ensure that their work meets any necessary criteria
In Part II, you’ll learn how to attach a DTD to a document, how to validate your uments against their DTDs, and how to write new DTDs that solve your own prob-lems You’ll learn the syntax for declaring elements, attributes, entities, andnotations You’ll learn how to use entity declarations and entity references to buildboth a document and its DTD from multiple, independent pieces This allows you tomake long, hard-to-follow documents much simpler by separating them into relatedmodules and components And you’ll learn how to use namespaces to mix togetherdifferent XML vocabularies in one document
doc-Part III: Style Languages
Part III (Chapters 12 through 16) teaches you everything you need to know aboutstyle sheets XML markup only specifies what’s in a document Unlike HTML, it doesnot say anything about what that content should look like Instead, style shheetsprovide all necessary information about an XML document’s appearance whenprinted, viewed in a web browser, or otherwise displayed Different style sheets can
be applied to the same document You might, for example, want to use one stylesheet that specifies small fonts for printing, another one with larger fonts for on-screen presentation, and a third with absolutely humongous fonts to project thedocument on a wall at a seminar You can change the appearance of an XML docu-ment by choosing a different style sheet without touching the document itself
Trang 14Part III describes in detail the two style sheet languages in broadest use today,Cascading Style Sheets (CSS) and the Extensible Stylesheet Language (XSL) CSS is asimple style sheet language originally designed for use with HTML It applies fixedstyle rules to the contents of particular elements.
XSL, by contrast, is a more complicated and more powerful style language that canapply styles to the contents of elements, as well as rearrange elements, add boiler-plate text, and transform documents in almost arbitrary ways XSL is divided intotwo parts: a transformation language for converting XML trees to alternative trees,and a formatting language for specifying the appearance of the elements of an XML tree
Part IV: Supplemental Technologies
Part IV (Chapters 17 through 20) introduces some XML-based languages and taxes that layer on top of basic XML to provide additional functionality and fea-tures XLink provides multidirectional hypertext links that are far more powerfulthan the simple HTML <A>tag XPointers introduce a new syntax you can attach tothe end of URLs to link not only to particular documents but also to particular parts
syn-of particular documents XInclude enables you to build large XML documents out syn-ofmultiple smaller XML documents XML Schemas provide a more complete valida-tions language that includes data typing and range checking All of these can beadded to your own XML-based markup languages to extend their power and utility
Part V: XML Applications
Part V (Chapters 21 to 25) demonstrates several practical uses of XML in differentdomains XHTML is a reformulation of HTML 4.0 as valid XML RDDL is an XHTML-and XLink-based language for documents containing meta-information placed at theend of namespace URLs Scalable Vector Graphics (SVG) is a standard XML formatfor drawings recommended by the World Wide Web Consortium (W3C) Finally, acompletely new application is developed for genealogical data to show you not justhow to use XML tags and technologies, but why and when to choose them
Combining all of these different applications, you’ll develop a good sense of howXML applications are designed, built, and used in the real world
What You Need
XML is a platform-independent technology You’ll notice that screen shots in thisbook have been captured from Windows, Mac OS 9, Mac OS X, and Linux Almost allthe examples work equally well across all common platforms You will need a webbrowser that supports XML, such as Mozilla, Netscape 6.0 or later, or InternetExplorer 6.0
Trang 15Furthermore, much of the best software for working with XML is written in Java andcan run on multiple platforms Much of this is freely available on the Internet Youwill need a Java 1.2 or later virtual machine (Java 1.1 can do in a pinch.) You won’tneed to write any programs to use this book You’ll just need it to run programswritten in Java.
How to Use This Book
This book is designed to be read more or less cover to cover Each chapter builds
on the material in the previous chapters in a fairly predictable fashion Of course,you’re always welcome to skim over material that’s already familiar to you I alsohope you’ll stop along the way to try out some of the examples and to write someXML documents of your own It’s important to learn not just by reading, but also bydoing Before you get started, I’d like to make a couple of notes about grammaticalconventions used in this book
Unlike HTML, XML is case-sensitive <FATHER>is not the same as <Father>or
<father> The fatherelement is not the same as the Fatherelement or the
FATHERelement Unfortunately, case-sensitive markup languages have an annoyinghabit of conflicting with standard English usage On rare occasion, this means thatyou may encounter sentences that don’t begin with a capital letter More commonly,you’ll see capitalization used in the middle of a sentence where you wouldn’t normally expect it Please don’t get too bothered by this All XML and HTML codeused in this book is placed in a monospaced font, so most of the time it will beobvious from the context what is meant
I have also adopted the British convention of placing punctuation inside quotemarks only when it belongs with the material quoted Frankly, although I learned towrite in the American educational system, I find the British system far more logical,especially when dealing with source code where the difference between a comma
or a period and no punctuation at all can make the difference between perfectlycorrect and perfectly incorrect code
What the Icons Mean
Throughout the book, I’ve used icons in the left margin to call your attention topoints that are particularly important
Note icons provide supplemental information about the subject at hand, but erally something that isn’t quite the main idea Notes are often used to elaborate
gen-on a detailed technical point
Note
Trang 16Tip icons indicate a more efficient way of doing something, or a technique that maynot be obvious.
Caution icons warn you of a common misconception or that a procedure doesn’talways work quite like it’s supposed to The most common reason for a Cautionicon in this book is to point out the difference between what a specification saysshould happen and what actually does
The Cross-Reference icon refers you to other chapters that have more to say about
a particular subject
Reach Out
Feedback on past editions has had a significant positive effect on the structure andcontent of this edition, and I encourage you to let me know what you think of it so Ican continue to improve future editions After you have had a chance to read thisbook, please take a moment to send me an e-mail at elharo@metalab.unc.edu Besure to include the title of this book in your e-mail Please be honest in your evalua-tion If you thought a particular chapter didn’t tell you enough, let me know Ofcourse, I would prefer to receive comments such as “This is the best book I’ve everread,” “Thanks to this book, my web site won Cool Site of the Year,” or “Because Iwas reading this book on the beach, I met a stunning swimsuit model who thought Iwas the hottest thing on feet,” but I’ll take any comments I can get J
You should also feel free to send me specific questions regarding the material inthis book I’ll do my best to help you out and answer your questions, but I can’tguarantee a reply Generally, more specific questions (How do I change the value of
a variable in XSLT?) are more likely to receive timely, useful answers than verygeneric, broad questions (How is XML used in the legal profession?)
Also, I invite you to visit my Cafe con Leche web site at http://www.cafeconleche
org, which contains a lot of XML-related material and is updated almost daily Despite
my persistent efforts to make this book perfect, some errors have doubtless slipped
by Even more certainly, some of the material discussed here will change over time I’llpost any necessary updates and errata on my web site at http://www.cafeconleche
org/books/bible3/ Please let me know via e-mail of any errors that you find thataren’t already listed
I hope you enjoy the book Happy XMLing!
Elliotte Rusty Harold
elharo@metalab.unc.eduhttp://www.cafeconleche.org
New York City, December 11, 2003
Cross-Reference
Caution Tip
Trang 18The folks at Wiley Publishing have all been great The acquisitions editors, John
Osborn, Grace Buechlein on the second edition, and Jim Minatel on this tion deserve special thanks for arranging the unusual scheduling this book required
edi-to hit the moving target that XML presents Marcia Ellett shepherded this bookthrough the development process She managed the shifting outline and schedulethat a book based on unstable specifications and software requires with poise andgrace Angela Smith proved equally adept on shepherding this book through itsfinal production Terri Varveris edited the first edition and Sharon Nash the secondedition Without them, there could never have been a third edition
Steven Champeon brought his SGML experience to the book, and provided manyinsightful comments on the text My brother, Thomas Harold, put his command ofchemistry at my disposal when I was trying to grasp the Chemical MarkupLanguage Carroll Bellau provided me with the parts of my family tree, which you’llfind in Chapter 18 Piroz Mohseni and Heather Williamson served as technical edi-tors on the first edition and corrected many of my errors Ken Cox performed thesame service for the second edition, and B.K Delong for the Gold edition DavidSchultz stepped up to the plate for this edition
I also greatly appreciate all the comments, questions, and corrections sent in by
readers of the first and second editions and XML: Extensible Markup Language I
hope that I’ve managed to address most of those comments in this book They’ve
definitely helped make the XML 1.1 Bible, 3rd Edition a better book Particular
thanks are due to Michael Dyck, Alan Esenther, and Donald Lancon, Jr for theirespecially detailed comments
The agenting talents of David and Sherry Rogelberg of the Studio B Literary Agency(http://www.studiob.com/) have made it possible for me to write effectively full-time I recommend them highly to anyone thinking about writing computer books.And, as always, thanks go to my wife, Beth, for her endless love and understanding
Trang 19Contents at a Glance
Preface vii
Acknowledgments xv
Part I: Introducing XML 1
Chapter 1: An Eagle’s Eye View of XML 3
Chapter 2: XML Applications 17
Chapter 3: Your First XML Document 55
Chapter 4: Structuring Data 63
Chapter 5: Attributes, Empty-Element Tags, and XSL 103
Chapter 6: Well-formedness 145
Part II: Document Type Definitions 187
Chapter 7: Validity 189
Chapter 8: Element Declarations 205
Chapter 9: Attribute Declarations 229
Chapter 10: Entity Declarations 249
Chapter 11: Namespaces 287
Part III: Style Languages 309
Chapter 12: CSS Style Sheets 311
Chapter 13: CSS Layouts 335
Chapter 14: CSS Text Styles 381
Chapter 15: XSL Transformations 423
Chapter 16: XSL Formatting Objects 507
Trang 20Part IV: Supplemental Technologies 577
Chapter 17: XLinks 579
Chapter 18: XPointers 617
Chapter 19: XInclude 647
Chapter 20: Schemas 667
Part V: XML Applications 731
Chapter 21: XHTML 733
Chapter 22: Modular XHTML 787
Chapter 23: The Resource Directory Description Language 833
Chapter 24: Scalable Vector Graphics 849
Chapter 25: Designing a New XML Application 907
Index 983
Trang 22Preface vii Acknowledgments xv
Chapter 1: An Eagle’s Eye View of XML 3
What Is XML? 3XML is a meta-markup language 3XML describes structure and semantics, not formatting 5Why Are Developers Excited About XML? 6Domain-specific markup languages 6Self-describing data 7Interchange of data among applications 8Structured data 8The Life of an XML Document 9Editors 9Parsers and processors 10Browsers and other applications 10The process summarized 10Related Technologies 11HTML 11CSS 12XSL 12URLs and URIs 13XLinks and XPointers 14Unicode 15Putting the pieces together 15Summary 16
Chapter 2: XML Applications 17
What Is an XML Application? 17Chemical Markup Language 18Mathematical Markup Language 19RSS 22Classic literature 25Synchronized Multimedia Integration Language 26
Trang 23Open Software Description 26Scalable Vector Graphics 27MusicXML 29VoiceXML 34Open Financial Exchange 36Extensible Forms Description Language 36HR-XML 40XML for XML 43XSL 43XLinks 44Schemas 45Behind-the-Scene Uses of XML 46Microsoft Office 2003 46Netscape’s What’s Related 49UPS 52Summary 54
Chapter 3: Your First XML Document 55
Hello XML 55Creating a simple XML document 56Saving the XML file 56Loading the XML file into a web browser 57Exploring the Simple XML Document 58Meaning in Markup 59Writing a Style Sheet for an XML Document 60Attaching a Style Sheet to an XML Document 61Summary 62
Chapter 4: Structuring Data 63
Examining the Data 64XMLizing the Data 67The Advantages of the XML Format 87Preparing a Style Sheet for Document Display 88Linking to a style sheet 89Assigning style rules to the root element 90Assigning style rules to titles 91The complete style sheet 98Summary 101
Chapter 5: Attributes, Empty-Element Tags, and XSL 103
Attributes 103Attributes versus Elements 109Structured metadata 109Meta-metadata 113
Trang 24What’s your metadata is someone else’s data 113Elements are more extensible 114Good times to use attributes 114Empty Elements and Empty-Element Tags 116XSL 119Templates 120The body of the document 121The title 123Stations 126Shows 129Sorting 134Tables 137CSS or XSL? 143Summary 144
Chapter 6: Well-formedness 145
Well-formedness Rules 145XML Documents 146The XML declaration 147Single root element 148Text in XML 148Elements and Tags 149Element names 149Every start-tag must have a corresponding end-tag 150Empty-element tags 151Elements may nest but may not overlap 152Attributes 154Attribute names 154Attribute values 154Predefined attributes 155Entity References 160Comments 162Processing Instructions 164CDATA Sections 165Unicode 167Character encodings 168The encoding declaration 168Numeric character references 169XML 1.1 171Well-formed HTML 173Rules for HTML 173Tools 183Summary 185
Trang 25Part II: Document Type Definitions 187
Chapter 7: Validity 189
Document Type Definitions 189Element Declarations 190DTD Files 192Document Type Declarations 192Internal DTDs 194Internal and external DTD subsets 195Public DTDs 196DTDs and style sheets 197Validating against a DTD 199Command-line validators 200Web-based validators 201Summary 203
Chapter 8: Element Declarations 205
Analyzing the Document 205ANY 208
#PCDATA 211Child Elements 213+ One or More Children 215
? Zero or One Child 215
* Zero or More Children 216Choices 216Parentheses 217Mixed Content 220Empty Elements 221Comments in DTDs 223Summary 227
Chapter 9: Attribute Declarations 229
What Is an Attribute? 229Declaring Attributes 230Declaring Multiple Attributes 231Alternatives to Default Attribute Values 232
#REQUIRED 232
#IMPLIED 233
#FIXED 234Attribute Types 235The CDATA attribute type 235The NMTOKEN attribute type 236
Trang 26The NMTOKENS attribute type 236The enumerated attribute type 237The ID attribute type 238The IDREF attribute type 238The IDREFS attribute type 239The ENTITY attribute type 240The ENTITIES attribute type 241The NOTATION attribute type 241
A DTD for Attribute-Based Television Listings 242Declaring SCHEDULE attributes 243Declaring STATION attributes 243Declaring SHOW attributes 244Declaring person attributes 245The complete DTD for the television listings example 246Summary 248
Chapter 10: Entity Declarations 249
What Is an Entity? 249Internal General Entities 250Defining an internal general entity reference 251Using general entity references in the DTD 254Predefined general entity references 255External General Entities 256Text declarations 258Nonvalidating parsers 260Internal Parameter Entities 260External Parameter Entities 263Building a Document from Pieces 269Non-XML Data 274Notations 275Unparsed entities 278Conditional Sections 283Summary 284
Chapter 11: Namespaces 287
The Need for Namespaces 287Namespace Syntax 289Defining namespaces with xmlns attributes 291Multiple namespaces 294Attributes 298Default namespaces 300Namespaces and Validity 304Summary 307
Trang 27Part III: Style Languages 309
Chapter 12: CSS Style Sheets 311
What Are Cascading Style Sheets? 311
A simple CSS style sheet 312Comments 312Attaching style sheets to documents 313DTDs and style sheets 316CSS1 versus CSS2 316CSS3 317Selecting Elements 317The universal selector 320Grouping selectors 320Hierarchy selectors 321Attribute selectors 323
ID selectors 324Pseudo-elements 324Pseudo-classes 326Inheritance 328Cascades 329Different Rules for Different Media 330Importing Style Sheets 332Character Sets 333Summary 333
Chapter 13: CSS Layouts 335
CSS Units 335Length values 337URL values 339Color values 340System colors 341Keyword values 343Strings 343The Display Property 343Inline elements 347Block elements 348None 348Compact and run-in elements 350Marker 350Tables 350List items 351Box Properties 356Margin properties 356Border properties 358Padding properties 363
Trang 28Size 364The width and height properties 365The min-width and min-height properties 367The max-width and max-height properties 367The overflow property 368Positioning 369The position property 369Stacking elements with the z-index property 373The float property 373The clear property 374Formatting Pages 375
@page 375The size property 376The margin property 376The mark property 377The page property 377Controlling page breaks 377Widows and orphans 379Summary 379
Chapter 14: CSS Text Styles 381
Fonts 381Choosing the font family 382Choosing the font style 384Small caps 385Setting the font weight 385Setting the font size 386The font shorthand property 390Color 392Text 392Word spacing 393The letter-spacing property 394The text-decoration property 395The vertical-align property 396The text-transform property 397The text-align property 398The text-indent property 399The line-height property 400The white-space property 402Backgrounds 404The background-color property 404The background-image property 405The background-repeat property 406The background-attachment property 408The background-position property 409The background shorthand property 413
Trang 29Visibility 413Content 414Quotes 415Attributes 416URIs 416Counters 417Summary 421
Chapter 15: XSL Transformations 423
What Is XSL? 423Overview of XSL Transformations 424Trees 424XSLT style sheet documents 428Where does the XML transformation happen? 430Using xsltproc 430Browser display of XML files with XSLT style sheets 433XSL Templates 434The xsl:apply-templates element 435The select attribute 437Computing the Value of a Node with xsl:value-of 437Processing Multiple Elements with xsl:for-each 439Patterns for Matching Nodes 440Matching the root node 440Matching element names 442Wildcards 444Matching children with / 445Matching descendants with // 446Matching by ID 447Matching attributes with @ 447Matching comments with comment( ) 449Matching processing instructions with processing-instruction( ) 450Matching text nodes with text( ) 451Using the or operator | 451Testing with [ ] 452XPath Expressions for Selecting Nodes 454Node axes 455Expression types 461The Default Template Rules 471The default rule for elements 471The default rule for text nodes and attributes 472The default rule for processing instructions and comments 472Implications of the default rules 473Attribute Value Templates 473Deciding What Output to Include 475Inserting elements into the output with xsl:element 475Inserting attributes into the output with xsl:attribute 476Defining attribute sets 477
Trang 30Generating processing instructions with xsl:processing-instruction 478Generating comments with xsl:comment 479Generating text with xsl:text 479Copying the Context Node with xsl:copy 480Counting Nodes with xsl:number 482Default numbers 483Number to string conversion 485Sorting Output 486Modes 490Defining Constants with xsl:variable 492Named Templates 493Passing Parameters to Templates 495Stripping and Preserving White Space 496Making Choices 497xsl:if 498xsl:choose 498Merging Multiple Style Sheets 499Importing with xsl:import 499Inclusion with xsl:include 500Output Methods 500XML Declaration 501Document Type Declaration 502Indentation 503CDATA sections 504Summary 504
Chapter 16: XSL Formatting Objects 507
Formatting Objects and Their Properties 507Formatting properties 510Transforming to formatting objects 514Using FOP 516Page Layout 518The root element 518Simple page masters 518Regions 520Page sequences 522Page sequence masters 530Content 533Block-level formatting objects 534Inline formatting objects 534Table formatting objects 535Out-of-line formatting objects 536Leaders and Rules 536Graphics 539fo:external-graphic 539fo:instream-foreign-object 541Graphic properties 544
Trang 31Links 545Lists 547Tables 551Inlines 556Footnotes 557Floats 558Formatting Properties 559The id property 559The language property 559Paragraph properties 560Character properties 563Sentence properties 565Area properties 567Summary 575
Chapter 17: XLinks 579
XLinks versus HTML Links 579Linking Elements 580Declaring XLink Attributes in Document Type Definitions 582Descriptions of the Remote Resource 584Link Behavior 585The xlink:show attribute 585The xlink:actuate attribute 587Extended Links 589Extended Link Syntax 590Arcs 595Out-of-Line Links 601XML Base 607Summary 614
Chapter 18: XPointers 617
Why XPointers? 617XPointer Examples 618
A Concrete Example 621Location Paths, Steps, and Sets 623The Root Node 625Axes 626The child axis 627The descendant axis 628The descendant-or-self axis 628The parent axis 628The self axis 628The ancestor axis 629
Trang 32The ancestor-or-self axis 629The preceding axis 629The following axis 629The preceding-sibling axis 630The following-sibling axis 630The attribute axis 630The namespace axis 630Node Tests 631Predicates 633Functions That Return Node-Sets 635id( ) 636here( ) 637origin( ) 638Points 638Ranges 640Range functions 640String ranges 641Child Sequences 643Summary 644
Chapter 19: XInclude 647
Use Cases for XInclude 648Non-Solutions 651DTDs 651Embedded XLinks 652Server-side includes 653The xinclude:include Element 655Validating Documents That Use XInclude 658XPointers in XInclude 660Unparsed Text 662Fallbacks 663Summary 665
Chapter 20: Schemas 667
What’s Wrong with DTDs? 667What Is a Schema? 669The W3C XML Schema Language 671Hello Schemas 671The greeting schema 672Validating the document against the schema 673Complex Types 675minOccurs and maxOccurs 677Element content 680Sharing content models 682Anonymous types 683Mixed content 685
Trang 33Grouping 686The xsd:all Group 688Choices 689Sequences 690Simple Types 690Numeric data types 692Time data types 695XML data types 696String data types 697Binary types 698Miscellaneous data types 699Deriving Simple Types 700Deriving by restriction 700Facets 702Facets for strings: length, minLength, maxLength 702The whiteSpace facet 704Facets for decimal numbers: totalDigits and fractionDigits 705The enumeration facet 705The pattern facet 707Unions 714Lists 714Empty Elements 715Attributes 716Namespaces 720Schemas for default namespaces 721Multiple namespaces, multiple schemas 724Annotations 727Summary 728
Chapter 21: XHTML 733
Why Validate HTML? 733Moving to XHTML 735Making the document well-formed XML 738Making the document valid 745The strict DTD 753The frameset DTD 767HTML Tidy 767Setting the MIME media type 771What’s New in XHTML 772Character references 772Custom entity references defined in DTD 776Encoding declarations 781The xml:lang attribute 782CDATA sections 783Summary 785
Trang 34Chapter 22: Modular XHTML 787
The Modules of XHTML 787
A Sample DTD Module 790Element names 793Element-specific content models 794Generic content models 795Generic attribute models 796INCLUDE and IGNORE blocks 796Using XHTML entities in other applications 797The Framework 798The notations framework module 802The data types framework module 804The namespace-qualified names module 805The common attributes module 807The character entity modules 808The Driver DTD 809The Document Model 816The XHTML Basic document model 817
A minimal document model 823
A Sample Schema Module 828Summary 831
Chapter 23: The Resource Directory Description Language 833
What Does a Namespace URL Locate? 834The Solution 835The resource Element 836Natures 843Purposes 845Summary 847
Chapter 24: Scalable Vector Graphics 849
What Is SVG? 850Scalability 851Vector versus bitmapped graphics 852
A Simple SVG Document 852Embedding SVG Pictures in Web Pages 855Simple Shapes 858The rect element 859The circle element 861The ellipse element 862The line element 863Polygons and polylines 864Paths 866Arcs 869Curves 872
Trang 35Text 873Strings 874Text on a path 876Fonts and text styles 878Text spans 879Bitmapped Images 879Coordinate Systems and Viewports 881The viewport 882Coordinate systems 883Grouping Shapes 888Referencing Shapes 889Transformations 892Linking 899Metadata 900SVG Editors 903Summary 904
Chapter 25: Designing a New XML Application 907
Organization of the Data 907Listing the elements 909Identifying the fundamental elements 909Establishing relationships among the elements 912Choosing a Namespace 914Persons 915
A sample person 915The person DTD 918The person schema 924Families 932The family DTD 933The Family Schema 935Sources 937The Family Tree 940The Family Tree DTD 946The family tree schema 948Modularizing the DTDs 950Designing a Style Sheet for Family Trees 960
A RDDL document for family trees 968Summary 980Index 983
Trang 36Chapter 2
XML Applications
Chapter 3
Your First XMLDocument
Chapter 4
Structuring Data
Chapter 5
Attributes, Empty-Element Tags,and XSL
Chapter 6
Well-formedness
I
Trang 38An Eagle’s Eye View of XML
This chapter introduces you to XML, the Extensible
Markup Language It explains, in general terms, whatXML is and how it is used It shows you how different XMLtechnologies work together, and how to create an XML docu-ment and deliver it to readers
What Is XML?
XML stands for Extensible Markup Language (often
miscapi-talized as eXtensible Markup Language to justify the acronym).
XML is a set of rules for defining semantic tags that break adocument into parts and identify the different parts of the doc-ument It is a meta-markup language that defines a syntax inwhich other domain-specific markup languages can be written
of whatever the vendor chooses to include
Related technologies
Trang 39XML, however, is a meta-markup language It’s a language that lets you make up thetags you need as you go along These tags must be organized according to certaingeneral principles, but they’re quite flexible in their meaning For example, if you’reworking on genealogy and need to describe family names, personal names, dates,births, adoptions, deaths, burial sites, marriages, divorces, and so on, you can cre-ate tags for each of these You don’t have to force your data to fit into paragraphs,list items, table cells, or other very general categories.
You can document the tags you create in a schema written in any of several guages, including document type definitions (DTDs) and the W3C XML SchemaLanguage You’ll learn more about DTDs and schemas in Parts II and IV of this book.For now, think of a schema as a vocabulary and a syntax for certain kinds of docu-ments For example, the schema for Peter Murray-Rust’s Chemical Markup Language(CML) is a DTD that describes a vocabulary and a syntax for the molecular sciences:chemistry, crystallography, solid-state physics, and the like It includes tags foratoms, molecules, bonds, spectra, and so on Many different people in the field canshare this schema Other schemas are available for other fields, and you can createyour own
lan-XML defines the meta syntax that domain-specific markup languages such asMusicXML, MathML, and CML must follow It specifies the rules for the low-levelsyntax, saying how markup is distinguished from content, how attributes areattached to elements, and so forth, without saying what these tags, elements, andattributes are or what they mean It gives the patterns that elements must followwithout specifying the names of the elements For example, XML says that tagsbegin with a <and end with a > However, XML does not tell you what names must
go between the <and the >
If an application understands this meta syntax, it at least partially understands allthe languages built from this meta syntax A browser does not need to know inadvance each and every tag that might be used by thousands of different markuplanguages Instead, the browser discovers the tags used by any given document as
it reads the document or its schema The detailed instructions about how to displaythe content of these tags are provided in a separate style sheet that is attached
to the document
For example, consider the three-dimensional Schrödinger equation:
XML means you don’t have to wait for browser vendors to catch up with your ideas.You can invent the tags you need, when you need them, and tell the browsers how
to display these tags
ih ∂ψ r, t
∂ t = – h
2
Trang 40XML describes structure and semantics, not formatting
XML markup describes a document’s structure and meaning It does not describethe formatting of the elements on the page You can add formatting to a documentwith a style sheet The document itself only contains tags that say what is in thedocument, not what the document looks like
By contrast, HTML encompasses formatting, structural, and semantic markup <B>
is a formatting tag that makes its content bold <STRONG>is a semantic tag thatmeans its contents are especially important <TD>is a structural tag that indicatesthat the contents are a cell in a table In fact, some tags can have all three kinds ofmeaning An <H1>tag can simultaneously mean 20-point Helvetica bold, a level 1heading, and the title of the page
For example, in HTML, a song might be described using a definition title, definitiondata, an unordered list, and list items But none of these elements actually haveanything to do with music The HTML might look something like this:
<DT>Hot Cop
<DD> by Jacques Morali, Henri Belolo, and Victor Willis
<UL>
<LI> Jacques Morali
<LI> PolyGram Records