ebook img

XML PDF

256 Pages·2.68 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview XML

V Q S G ISUAL UICK TART UIDE XML S E ECOND DITION K H G EVIN OWARD OLDBERG Peachpit Press Visual QuickStart Guide XML, Second Edition Kevin Howard Goldberg Peachpit Press 1249 Eighth Street Berkeley, CA 94710 510/524-2178 510/524-2221 (fax) Find us on the Web at: www.peachpit.com To report errors, please send a note to [email protected] Peachpit Press is a division of Pearson Education Copyright © 2009 by Elizabeth Castro and Kevin Howard Goldberg Production Editor: David Van Ness Tech Editors: Chris Hare and Michael Weiss Compositor: Kevin Howard Goldberg Indexer: Valerie Perry Cover Design: Peachpit Press Notice of Rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact [email protected]. Notice of Liability The information in this book is distributed on an “As Is” basis without warranty. While every pre- caution has been taken in the preparation of the book, neither the author nor Peachpit shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it. Trademarks Visual QuickStart Guide is a trademark of Peachpit, a division of Pearson Education. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book. ISBN-13: 978-0-321-55967-8 ISBN-10: 0-321-55967-3 9 8 7 6 5 4 3 2 1 Printed and bound in the United States of America T C ABLE OF ONTENTS Part 1: XML Chapter 1: Writing XML . . . . . . . . . . . . . . . . . . . . . . . .3 An XML Sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Rules for Writing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Elements, Attributes, and Values . . . . . . . . . . . . . . . . . . . . .6 How To Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Creating the Root Element . . . . . . . . . . . . . . . . . . . . . . . . .8 Writing Child Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Nesting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Adding Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Using Empty Elements . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Writing Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Predefi ned Entities – Five Special Symbols . . . . . . . . . . . . .14 Displaying Elements as Text . . . . . . . . . . . . . . . . . . . . . . . .15 Part 2: XSL Chapter 2: XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Transforming XML with XSLT . . . . . . . . . . . . . . . . . . . . .20 Beginning an XSLT Style Sheet . . . . . . . . . . . . . . . . . . . . .22 Creating the Root Template . . . . . . . . . . . . . . . . . . . . . . . .23 Outputting HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Outputting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Looping Over Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 Processing Nodes Conditionally . . . . . . . . . . . . . . . . . . . . .30 Adding Conditional Choices . . . . . . . . . . . . . . . . . . . . . . .31 Sorting Nodes Before Processing . . . . . . . . . . . . . . . . . . . .32 Generating Output Attributes . . . . . . . . . . . . . . . . . . . . . .33 Creating and Applying Templates . . . . . . . . . . . . . . . . . . .34 Chapter 3: XPath Patterns and Expressions . . . . . . . . . .37 Locating Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Determining the Current Node . . . . . . . . . . . . . . . . . . . . .40 Referring to the Current Node . . . . . . . . . . . . . . . . . . . . . .41 Selecting a Node’s Children . . . . . . . . . . . . . . . . . . . . . . . .42 Selecting a Node’s Parent or Siblings . . . . . . . . . . . . . . . . .43 Selecting a Node’s Attributes . . . . . . . . . . . . . . . . . . . . . . .44 Conditionally Selecting Nodes . . . . . . . . . . . . . . . . . . . . . .45 Creating Absolute Location Paths . . . . . . . . . . . . . . . . . . .46 Selecting All the Descendants . . . . . . . . . . . . . . . . . . . . . .47 Chapter 4: XPath Functions . . . . . . . . . . . . . . . . . . . .49 Comparing Two Values . . . . . . . . . . . . . . . . . . . . . . . . . . .50 Testing the Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Multiplying, Dividing, Adding, Subtracting . . . . . . . . . . .52 Counting Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Rounding Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Extracting Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 Changing the Case of a String . . . . . . . . . . . . . . . . . . . . . .57 Totaling Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 More XPath Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 Chapter 5: XSL-FO . . . . . . . . . . . . . . . . . . . . . . . . . .61 Th e Two Parts of an XSL-FO Document . . . . . . . . . . . . . .62 Creating an XSL-FO Document . . . . . . . . . . . . . . . . . . . .63 Creating and Styling Blocks of Page Content . . . . . . . . . . .64 Adding Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Defi ning a Page Template . . . . . . . . . . . . . . . . . . . . . . . . .66 Creating a Page Template Header . . . . . . . . . . . . . . . . . . .67 Using XSLT to Create XSL-FO . . . . . . . . . . . . . . . . . . . . .68 Inserting Page Breaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 Outputting Page Content in Columns . . . . . . . . . . . . . . . .70 Adding a New Page Template . . . . . . . . . . . . . . . . . . . . . .71 Part 3: DTD Chapter 6: Creating a DTD . . . . . . . . . . . . . . . . . . . .75 Working with DTDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 Defi ning an Element Th at Contains Text . . . . . . . . . . . . . .77 Defi ning an Empty Element . . . . . . . . . . . . . . . . . . . . . . .78 Defi ning an Element Th at Contains a Child . . . . . . . . . . .79 Defi ning an Element Th at Contains Children . . . . . . . . . .80 Defi ning How Many Occurrences . . . . . . . . . . . . . . . . . . .81 Defi ning Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Defi ning an Element Th at Contains Anything . . . . . . . . . .83 About Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84 Defi ning Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 Defi ning Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Defi ning Attributes with Choices. . . . . . . . . . . . . . . . . . . .87 Defi ning Attributes with Unique Values . . . . . . . . . . . . . .88 Referencing Attributes with Unique Values . . . . . . . . . . . .89 Restricting Attributes to Valid XML Names . . . . . . . . . . . .90 Chapter 7: Entities and Notations in DTDs . . . . . . . . . .91 Creating a General Entity . . . . . . . . . . . . . . . . . . . . . . . . .92 Using General Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Creating an External General Entity . . . . . . . . . . . . . . . . .94 Using External General Entities . . . . . . . . . . . . . . . . . . . . .95 Creating Entities for Unparsed Content . . . . . . . . . . . . . . .96 Embedding Unparsed Content . . . . . . . . . . . . . . . . . . . . .98 Creating and Using Parameter Entities . . . . . . . . . . . . . .100 Creating an External Parameter Entity . . . . . . . . . . . . . . .101 Chapter 8: Validation and Using DTDs . . . . . . . . . . .103 Creating an External DTD . . . . . . . . . . . . . . . . . . . . . . .104 Declaring an External DTD . . . . . . . . . . . . . . . . . . . . . .105 Declaring and Creating an Internal DTD . . . . . . . . . . . .106 Validating XML Documents Against a DTD . . . . . . . . . .107 Naming a Public External DTD . . . . . . . . . . . . . . . . . . .108 Declaring a Public External DTD . . . . . . . . . . . . . . . . . .109 Pros and Cons of DTDs . . . . . . . . . . . . . . . . . . . . . . . . .110 Part 4: XML Schema Chapter 9: XML Schema Basics . . . . . . . . . . . . . . . .113 Working with XML Schema . . . . . . . . . . . . . . . . . . . . . .114 Beginning a Simple XML Schema . . . . . . . . . . . . . . . . . .116 Associating an XML Schema with an XML Document . .117 Annotating Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . .118 Chapter 10: Defi ning Simple Types . . . . . . . . . . . . . . .119 Defi ning a Simple Type Element . . . . . . . . . . . . . . . . . . .120 Using Date and Time Types . . . . . . . . . . . . . . . . . . . . . . .122 Using Number Types . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 Predefi ning an Element’s Content . . . . . . . . . . . . . . . . . .125 Deriving Custom Simple Types . . . . . . . . . . . . . . . . . . . .126 Deriving Named Custom Types . . . . . . . . . . . . . . . . . . . .127 Specifying a Range of Acceptable Values . . . . . . . . . . . . .128 Specifying a Set of Acceptable Values . . . . . . . . . . . . . . . .130 Limiting the Length of an Element . . . . . . . . . . . . . . . . .131 Specifying a Pattern for an Element . . . . . . . . . . . . . . . . .132 Limiting a Number’s Digits . . . . . . . . . . . . . . . . . . . . . . .134 Deriving a List Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 Deriving a Union Type . . . . . . . . . . . . . . . . . . . . . . . . . .136 Chapter 11: Defi ning Complex Types . . . . . . . . . . . . .137 Complex Type Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Deriving Anonymous Complex Types . . . . . . . . . . . . . . .140 Deriving Named Complex Types . . . . . . . . . . . . . . . . . . .141 Defi ning Complex Types Th at Contain Child Elements .142 Requiring Child Elements to Appear in Sequence . . . . . .143 Allowing Child Elements to Appear in Any Order . . . . . .144 Creating a Set of Choices . . . . . . . . . . . . . . . . . . . . . . . . .145 Defi ning Elements to Contain Only Text . . . . . . . . . . . .146 Defi ning Empty Elements . . . . . . . . . . . . . . . . . . . . . . . .147 Defi ning Elements with Mixed Content . . . . . . . . . . . . .148 Deriving Complex Types from Existing Complex Types .149 Referencing Globally Defi ned Elements . . . . . . . . . . . . . .150 Controlling How Many . . . . . . . . . . . . . . . . . . . . . . . . . .151 Defi ning Named Model Groups . . . . . . . . . . . . . . . . . . .152 Referencing a Named Model Group . . . . . . . . . . . . . . . .153 Defi ning Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Requiring an Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . .155 Predefi ning an Attribute’s Content . . . . . . . . . . . . . . . . . .156 Defi ning Attribute Groups . . . . . . . . . . . . . . . . . . . . . . . .157 Referencing Attribute Groups . . . . . . . . . . . . . . . . . . . . .158 Local and Global Defi nitions . . . . . . . . . . . . . . . . . . . . . .159 Part 5: Namespaces Chapter 12: XML Namespaces . . . . . . . . . . . . . . . . . .163 Designing a Namespace Name . . . . . . . . . . . . . . . . . . . . .164 Declaring a Default Namespace . . . . . . . . . . . . . . . . . . . .165 Declaring a Namespace Name Prefi x . . . . . . . . . . . . . . . .166 Labeling Elements with a Namespace Prefi x . . . . . . . . . . .167 How Namespaces Aff ect Attributes . . . . . . . . . . . . . . . . .168 Chapter 13: Using XML Namespaces . . . . . . . . . . . . .169 Populating an XML Namespace . . . . . . . . . . . . . . . . . . . .170 XML Schemas, XML Documents, and Namespaces . . . .171 Referencing XML Schema Components in Namespaces .172 Namespaces and Validating XML . . . . . . . . . . . . . . . . . .173 Adding All Locally Defi ned Elements . . . . . . . . . . . . . . .174 Adding Particular Locally Defi ned Elements . . . . . . . . . .175 XML Schemas in Multiple Files . . . . . . . . . . . . . . . . . . . .176 XML Schemas with Multiple Namespaces . . . . . . . . . . . .177 Th e Schema of Schemas as the Default . . . . . . . . . . . . . .178 Namespaces and DTDs . . . . . . . . . . . . . . . . . . . . . . . . . .179 XSLT and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . .180 Part 6: Recent W3C Recommendations Chapter 14: XSLT 2.0 . . . . . . . . . . . . . . . . . . . . . . . .183 Extending XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 Creating a Simplifi ed Style Sheet . . . . . . . . . . . . . . . . . . .185 Generating XHTML Output Documents . . . . . . . . . . . .186 Generating Multiple Output Documents. . . . . . . . . . . . .187 Creating User Defi ned Functions . . . . . . . . . . . . . . . . . . .188 Calling User Defi ned Functions . . . . . . . . . . . . . . . . . . . .189 Grouping Output Using Common Values . . . . . . . . . . . .190 Validating XSLT Output . . . . . . . . . . . . . . . . . . . . . . . . .191 Chapter 15: XPath 2.0 . . . . . . . . . . . . . . . . . . . . . . .193 XPath 1.0 and XPath 2.0 . . . . . . . . . . . . . . . . . . . . . . . . .194 Averaging Values in a Sequence . . . . . . . . . . . . . . . . . . . .196 Finding the Minimum or Maximum Value . . . . . . . . . . .197 Formatting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 Testing Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199 Quantifying a Condition . . . . . . . . . . . . . . . . . . . . . . . . .200 Removing Duplicate Items . . . . . . . . . . . . . . . . . . . . . . .201 Looping Over Sequences . . . . . . . . . . . . . . . . . . . . . . . . .202 Using Today’s Date and Time . . . . . . . . . . . . . . . . . . . . .203 Writing Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Processing Non-XML Input . . . . . . . . . . . . . . . . . . . . . .205 Chapter 16: XQuery 1.0 . . . . . . . . . . . . . . . . . . . . . .207 XQuery 1.0 vs. XSLT 2.0 . . . . . . . . . . . . . . . . . . . . . . . . .208 Composing an XQuery Document . . . . . . . . . . . . . . . . .209 Identifying an XML Source Document . . . . . . . . . . . . . .210 Using Path Expressions . . . . . . . . . . . . . . . . . . . . . . . . . .211 Writing FLWOR Expressions . . . . . . . . . . . . . . . . . . . . . .212 Testing with Conditional Expressions . . . . . . . . . . . . . . .214 Joining Two Related Data Sources . . . . . . . . . . . . . . . . . .215 Creating and Calling User Defi ned Functions . . . . . . . . .216 XQuery and Databases . . . . . . . . . . . . . . . . . . . . . . . . . .217 Part 7: XML in Practice Chapter 17: Ajax, RSS, SOAP, and More . . . . . . . . . .221 Ajax Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Ajax Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224 RSS Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 RSS Schema. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Extending RSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 SOAP and Web Services . . . . . . . . . . . . . . . . . . . . . . . . .230 SOAP Message Schema . . . . . . . . . . . . . . . . . . . . . . . . . .231 WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 KML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 A Simple KML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 ODF and OOXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236 eBooks, ePub, and More . . . . . . . . . . . . . . . . . . . . . . . . .238 Tools for XML in Practice . . . . . . . . . . . . . . . . . . . . . . . .240 Appendices Appendix A: XML Tools . . . . . . . . . . . . . . . . . . . . . . .245 XML Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 Additional XML Editors . . . . . . . . . . . . . . . . . . . . . . . . .248 XML Tools and Resources . . . . . . . . . . . . . . . . . . . . . . . .249 Appendix B: Character Sets and Entities . . . . . . . . . . . .251 Specifying the Character Encoding . . . . . . . . . . . . . . . . .252 Using Numeric Character References . . . . . . . . . . . . . . . .253 Using Entity References . . . . . . . . . . . . . . . . . . . . . . . . . .254 Unicode Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Index . . . . . . . . . . . . . . . . . . . . . . . . . . .257 P 1: ART XML Writing XML 3 1 1 W XML RITING The XML specification defines how to write a document in XML format. XML is not a language itself. Rather, an XML document is written in a custom markup language, according to the XML specification. For example, there could be custom markup languages describing genealogical, chemical, or business data, and you could write XML documents in each one. Every custom markup language created using the XML specification must adhere to XML’s underlying grammar. Therefore, that is where I will start this book. In this chapter, you will learn the rules for writing XML documents, regardless of the specific custom markup lan- W r guage in which you are writing. itin g X Officially, custom markup languages created M L with XML are called XML applications. In other words, these custom markup languages are applications of XML, such as XSLT, RSS, SOAP, etc. But for me, an application is a full- blown software program, like Photoshop. I find the term so imprecise, I usually try to avoid it. Tools for Writing XML XML, like HTML, can be written using any text editor or word processor. There are also many XML editors that have been created since the first edition of this book. These editors have various capabilities, such as validating your XML as you type (see Appendix A). I’ll assume you know how to create new docu- ments, open old ones for editing, and save them when you’re done. Just be sure to save all your XML documents with the .xml extension. 3

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.