Programming - XML By Example.pdf

(3423 KB) Pobierz
www.sharexxx.net - free books & magazines
XML
BY EXAMPLE
201 West 103rd Street
Indianapolis, Indiana 46290
Benoît Marchal
778293661.008.png 778293661.009.png 778293661.010.png 778293661.011.png
Publisher
John Pierce
Acquisitions Editor
Todd Green
Development Editor
Susan Hobbs
Technical Editor
Karl Fast
Managing Editor
Thomas F. Hayes
Project Editor
Karen S. Shields
Copy Editor
Sossity Smith
Indexer
Tina Trettin
Proofreader
Maribeth Echard
Team Coordinator
Julie Otto
Media Developer
Jay Payne
Interior Designer
Karen Ruggles
Cover Designer
Rader Design
Copy Writer
Eric Borgert
Production
Louis Porter Jr.
XML by Example
Copyright © 2000 by Que ®
All rights reserved. No part of this book shall be repro-
duced, stored in a retrieval system, or transmitted by
any means, electronic, mechanical, photocopying,
recording, or otherwise, without written permission
from the publisher. No patent liability is assumed with
respect to the use of the information contained herein.
Although every precaution has been taken in the
preparation of this book, the publisher and author
assume no responsibility for errors or omissions. Nor is
any liability assumed for damages resulting from the
use of the information contained herein.
International Standard Book Number: 0-7897-2242-9
Library of Congress Catalog Card Number: 99-66449
Printed in the United States of America
First Printing: December 1999
01
00
4
3
2
Trademarks
All terms mentioned in this book that are known to be
trademarks or service marks have been appropriately
capitalized. Que 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 or
service mark.
Warning and Disclaimer
Every effort has been made to make this book as com-
plete and as accurate as possible, but no warranty or
fitness is implied. The information provided is on an
“as is” basis. The author and the publisher shall have
neither liability nor responsibility to any person or
entity with respect to any loss or damages arising from
the information contained in this book.
778293661.001.png
 
778293661.002.png
iii
Contents at a Glance
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1 The XML Galaxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
2 The XML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
3 XML Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
4 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107
5 XSL Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
6 XSL Formatting Objects and Cascading Style Sheet . . . . . . . . . .161
7 The Parser and DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
8 Alternative API: SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231
9 Writing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269
10 Modeling for Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307
11 N-Tiered Architecture and XML . . . . . . . . . . . . . . . . . . . . . . . . . .345
12 Putting It All Together: An e-Commerce Example . . . . . . . . . . . .381
Appendix A: Crash Course on Java . . . . . . . . . . . . . . . . . . . . . . . . . .457
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
 
778293661.003.png 778293661.004.png
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
The by Example Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Who Should Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
This Book’s Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . .3
1
The XML Galaxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
A First Look at XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
No Predefined Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Stricter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
A First Look at Document Structure . . . . . . . . . . . . . . . . . . . . . . .10
Markup Language History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Mark-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Procedural Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Generic Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Standard Generalized Markup Language . . . . . . . . . . . . . . .18
Hypertext Markup Language . . . . . . . . . . . . . . . . . . . . . . . .20
eXtensible Markup Language . . . . . . . . . . . . . . . . . . . . . . . .26
Application of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Document Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Data Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Companion Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
XML Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
DOM and SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
XLink and XPointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
XML Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
XML Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
XML Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
XML Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
XSL Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
2
The XML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
A First Look at the XML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . .42
Getting Started with XML Markup . . . . . . . . . . . . . . . . . . .42
Element’s Start and End Tags . . . . . . . . . . . . . . . . . . . . . . .44
Names in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Empty Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Nesting of Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
XML Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
778293661.005.png
v
Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Special Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
Processing Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
CDATA Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Frequently Asked Questions on XML . . . . . . . . . . . . . . . . . . . . . .55
Code Indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
Why the End Tag? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56
XML and Semantic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Four Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Forget End Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Forget That XML Is Case Sensitive . . . . . . . . . . . . . . . . . . .60
Introduce Spaces in the Name of Element . . . . . . . . . . . . . .60
Forget the Quotes for Attribute Value . . . . . . . . . . . . . . . . . .60
XML Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Three Applications of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62
Business Document Exchange . . . . . . . . . . . . . . . . . . . . . . .63
Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
3 XML Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
The DTD Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Element Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Element Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Special Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
The Secret of Plus, Star, and Question Mark . . . . . . . . . . . .73
The Secret of Comma and Vertical Bar . . . . . . . . . . . . . . . . .73
Element Content and Indenting . . . . . . . . . . . . . . . . . . . . . .74
Nonambiguous Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
Document Type Declaration . . . . . . . . . . . . . . . . . . . . . . . . .76
Internal and External Subsets . . . . . . . . . . . . . . . . . . . . . . .77
Public Identifiers Format . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Standalone Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Why Schemas? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80
Well-Formed and Valid Documents . . . . . . . . . . . . . . . . . . . .81
Relationship Between the DTD and the Document . . . . . . . . . . .82
Benefits of the DTD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Validating the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Entities and Notations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
General and Parameter Entities . . . . . . . . . . . . . . . . . . . . . .86
Internal and External Entities . . . . . . . . . . . . . . . . . . . . . . .87
Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Managing Documents with Entities . . . . . . . . . . . . . . . . . . .90
 
778293661.006.png 778293661.007.png
Zgłoś jeśli naruszono regulamin