Beginning XML, 4th Edition....1
About the Authors....8
Credits....9
Acknowledgments....11
Contents....13
Introduction....29
Part I - Introduction....39
Chapter 1: What Is XML?....41
Of Data....-1
Binary Files....42
Text Files....43
A Brief History of Markup....44
So What Is XML?....45
What Does XML Buy Us?....48
HTML and XML: Apples and Red Delicious Apples....51
Hierarchies of Information....52
What’s a Document Type?....55
No....-1
Origin of the XML Standards....56
What Is the World Wide Web Consortium?....56
Components of XML....57
Where XML Can Be Used....-1
Reducing Server Load....58
Website Content....58
Distributed Computing....59
e-Commerce....59
Summary....60
Exercise Questions....60
Question 1....60
Question 2....60
Chapter 2: Well-Formed XML....61
Parsing XML....62
Tags and Text and Elements....-1
Rules for Elements....69
Attributes....77
When to Use Attributes....81
Comments....83
Empty Elements....87
XML Declarations....88
Version....89
Encoding....89
Standalone....91
Processing Instructions....94
Illegal PCDATA Characters....97
Escaping Characters....98
CDATA Sections....99
Errors in XML....102
Summary....102
Exercise Questions....103
Question 1....103
Question 2....103
Chapter 3: XML Namespaces....105
Why We Need Namespaces....105
Using Prefixes....107
Why Doesn’t XML Just Use These Prefixes?....108
How XML Namespaces Work....110
Default Namespaces....113
Do Different Notations Make Any Difference?....119
Namespaces and Attributes....121
Understanding URIs....124
URLs....124
URNs....125
Why Use URLs for Namespaces....-1
What Do Namespace URIs Really Mean?....126
RDDL....127
When to Use Namespaces....127
Summary....128
Exercise Questions....129
Question 1....129
Question 2....129
Question 3....129
Part II - Validation....131
Chapter 4: Document Type Definitions....133
Running the Samples....134
Preparing the Ground....134
The Document Type Declaration....138
Sharing Vocabularies....142
Anatomy of a DTD....143
Element Declarations....143
Attribute Declarations....158
Entities....169
Developing DTDs....179
DTD Limitations....180
DTD Syntax....180
XML Namespaces....181
Data Typing....181
Limited Content Model Descriptions....181
Summary....181
Exercise Questions....182
Question 1....182
Question 2....182
Question 3....182
Chapter 5: XML Schemas....183
Benefits of XML Schemas....184
XML Schemas Use XML Syntax....184
XML Schema Namespace Support....184
XML Schema Data Types....185
XML Schema Content Models....185
Do We Still Need DTDs?....185
XML Schemas....186
The XML Schema Document....186
Running the Samples....186
Declarations....190
Declarations....193
Declarations....203
Declarations....205
Content Models....206
Declarations....215
Declarations....221
Creating Elements with Simple Content and Attributes....223
Datatypes....224
Declarations....231
Creating a Schema from Multiple Documents....238
Declarations....238
Declarations....242
Documenting XML Schemas....244
Comments....244
Attributes from Other Namespaces....245
Annotations....246
Summary....247
Exercise Questions....248
Question 1....248
Question 2....248
Question 3....248
Chapter 6: RELAX NG....249
XML and Compact Syntaxes....250
Running the Samples....250
RELAX NG Patterns....251
Element....-1
Combining and Reusing Patterns and Grammars....265
Named Patterns....265
Combining Named Pattern Definitions....268
Schema Modularization Using the include Directive....269
Redefining Included Named Patterns....270
Removing Patterns with the notAllowed Pattern....271
Extensions and Restrictions....272
Nested Grammars....273
Additional RELAX NG Features....274
Namespaces....274
Name-Classes....275
Datatypes....279
List Patterns....281
Comments and Divisions....282
Useful Resources....283
Summary....283
Exercise Questions....283
Question 1....284
Question 2....284
Part III - Processing....285
Chapter 7: XPath....287
Ways of Looking at an XML Document....288
Modeling XML Documents....288
Visualizing XPath....289
Understanding Context....290
What Is a Node?....292
XPath 1.0 Types....295
Abbreviated and Unabbreviated Syntax....297
XPath 1.0 Axes....298
Child Axis....298
attribute Axis....300
ancestor Axis....302
ancestor-or-self Axis....303
descendant Axis....303
descendant-or-self Axis....304
following Axis....304
following-sibling Axis....306
namespace Axis....306
parent Axis....309
preceding Axis....309
preceding-sibling Axis....310
self Axis....311
XPath 1.0 Functions....312
Boolean Functions....312
Node-Set Functions....313
Numeric Functions....313
String Functions....314
Predicates....316
Structure of XPath Expressions....316
XPath 2.0....319
Revised XPath Data Model....319
W3C XML Schema Data Types....319
Additional XPath 2.0 Functions....320
XPath 2.0 Features....320
Summary....323
Exercise Questions....323
Question 1....324
Question 2....324
Chapter 8: XSLT....325
What Is XSLT?....325
Restructuring XML....326
Presenting XML Content....326
How an XSLT Processor Works....326
Running the Examples....327
Introducing the Saxon XSLT Processor....327
Installing the Saxon XSLT Processor....328
Procedural versus Declarative Programming....330
Procedural Programming....330
Declarative Programming....330
Foundational XSLT Elements....331
The Element....333
The Element....334
The Element....334
Getting Information from the Source Tree....335
The Element....335
The Element....337
The Element....341
Influencing the Output with the Element....344
Conditional Processing....344
The Element....344
The Element....346
The Element....349
The Element....350
XSLT Modes....352
XSLT Variables and Parameters....358
Named Templates and the Element....360
XSLT Functions....361
XSLT 2.0....361
Grouping in Version 2.0....362
Non-XML Input and String Handling....365
Multiple Outputs....368
User-Defined Functions....370
xsl:value-of changes....372
Summary....373
Exercise Questions....373
Question 1....373
Question 2....373
Part IV - Databases....375
Chapter 9: XQuery, the XML Quey Language....377
Why XQuery?....378
Historical Factors....378
Technical Factors....378
Current Status....379
XQuery Tools....381
Saxon....381
X-Hive.com Online....383
X-Hive Database....384
Tamino Database....384
Microsoft SQL Server 2005....384
Oracle....384
Some XQuery Examples....384
Input Functions....384
Retrieving Nodes....386
Element Constructors....389
The XQuery Prolog....393
Computed Constructors....396
Syntax....397
The XQuery Data Model....398
Shared Data Model with XPath 2.0 and XSLT 2.0....398
Node Kinds....399
Sequences of Node-Sets....399
Document Order....399
Comparing Items and Nodes....399
Types in XQuery....399
Axes in XQuery....399
XQuery Expressions....400
FLWOR Expressions....400
XQuery Functions....406
The concat() Function....407
The count() Function....407
Using Parameters with XQuery....408
User-Defined Functions....409
Looking Ahead....410
Update Functionality....410
Full-Text Search....410
Summary....410
Exercise Questions....411
Question 1....411
Question 2....411
Chapter 10: XML and Databases....413
The Need for Efficient XML Data Stores....413
The Increasing Amount of XML....414
Comparing XML-Based Data and Relational Data....415
Approaches to Storing XML....416
Storing XML on File Systems....416
Using XML With Conventional Databases....417
Native XML Databases....419
Using Native XML Databases....420
Obtaining and Installing eXist....420
Interacting with eXist....422
XML in Commercial RDBMSs....433
XML Functionality in SQL Server 2000....433
Web Service Support....464
XML in Open Source RDBMS....464
Installing MySQL....464
Adding Information in MySQL....465
Querying MySQL....468
Updating XML in MySQL....473
Usability of XML in MySQL....474
Client-Side XML Support....475
Choosing a Database to Store XML....476
Looking Ahead....476
Summary....476
Exercise Questions....476
Question 1....477
Question 2....477
Question 3....477
Part V - Programming....479
Chapter 11: The XML Document Object Model (DOM)....481
Purpose of the XML DOM....481
Interfaces and Objects....483
The Document Object Model at the W3C....484
XML DOM Implementations....485
Two Ways to View DOM Nodes....486
Overview of the XML DOM....486
Tools to Run the Examples....488
Browser Differences....488
The Node Object....496
Properties of the Node Object....496
Methods of the Node Object....499
Loading an XML Document....500
The Effect of Text Nodes....506
The NamedNodeMap Object....509
The NodeList Object....513
The DOMException Object....514
The Document Interface....516
How the XML DOM Is Used in InfoPath 2007....519
Summary....520
Exercise Questions....520
Question 1....520
Question 2....520
Chapter 12: Simple API for XML (SAX)....521
What Is SAX and Why Was It Invented?....521
A Brief History of SAX....522
Where to Get SAX....523
Setting Up SAX....524
Receiving SAX Events....524
ContentHandler Interface....525
ErrorHandler Interface....542
DTDHandler Interface....547
EntityResolver Interface....548
Features and Properties....548
Extension Interfaces....552
Good SAX and Bad SAX....553
Consumers....-1
Other Languages....554
Summary....555
Exercise Questions....556
Question 1....556
Question 2....556
Part VI - Communication....557
Chapter 13: RSS Atom, and Content Syndication....559
Syndication and Meta Data....559
Syndication Systems....560
The Origin of RSS Species....563
RSS-DEV and RSS 1.0....567
UserLand and RSS 2.0....569
Atom....571
Working with News Feeds....574
Newsreaders....574
Data Quality....574
A Simple Aggregator....575
Modeling Feeds....575
Program Flow....578
Implementation....578
Transforming RSS with XSLT....595
Useful Resources....605
Summary....606
Exercise Questions....606
Question 1....607
Question 2....607
Chapter 14: Web Services....609
What Is an RPC?....609
RPC Protocols....611
DCOM....611
IIOP....612
Java RMI....613
The New RPC Protocol: Web Services....613
XML-RPC....614
The Network Transport....617
Taking a REST....634
The Web Services Stack....638
SOAP....638
WSDL....639
UDDI....640
Surrounding Specifications....640
Summary....642
Exercise Questions....643
Question 1....643
Question 2....643
Chapter 15: SOAP and WSDL....645
Laying the Groundwork....646
Running Examples in Windows 2003....-1
The New RPC Protocol: SOAP....646
Just RESTing....650
Basic SOAP Messages....651
More Complex SOAP Interactions....658
Defining Web Services: WSDL....670
....671
....671
....672
....673
....673
....675
....676
Other Bindings....679
Summary....682
Exercise Questions....682
Question 1....682
Question 2....682
Chapter 16: Ajax....683
Early Attempts at Asynchronous Updates....683
Microsoft versus Mozilla....685
Cross-Browser Solutions....685
Basic Posting Techniques....687
Transport and Processing on the Server....690
JSON....690
Payment Card Validator....691
The AutoSuggest Box....696
Server-Side Proxies....719
The Currency Converter Proxy....720
Summary....724
Exercise Questions....725
Question 1....725
Question 2....725
Question 3....725
Part VII - Display....727
Chapter 17: Cascading Style Sheets (CSS)....729
Why Stylesheets?....730
Introducing CSS....731
CSS Properties....732
Inheritance....733
Attaching the Stylesheet to an XML Document....737
Selectors....738
Using CSS for Layout of XML Documents....739
Understanding the Box Model....740
Positioning in CSS....744
Laying Out Tabular Data....757
Links in XML Documents....759
XLink Support in Firefox....759
Forcing Links Using the XHTML Namespace....763
Images in XML Documents....763
Using CSS to Add Content to Documents....764
Attribute Content....767
Attribute Selectors....767
Using Attribute Values in Documents....767
Summary....770
Exercise Questions....770
Question 1....770
Question 2....771
Question 3....771
Question 4....771
Chapter 18: XHTML....773
Separating Style from Content....774
Learning XHTML 1.x....776
Document Type Definitions for XHTML....776
Basic Changes in Writing XHTML....778
Styling XHTML Documents....789
Stricter Documents Make Faster and Lighter Processors....791
XHTML Tools....791
Validating XHTML Documents....792
Validation Pitfalls....794
Mime Types Pitfalls....795
Modularized XHTML....797
Module Implementations....799
XHTML 1.1....799
XHTML Basic....800
What’s Next for XHTML....801
Summary....803
Exercise Questions....804
Question 1....804
Question 2....804
Chapter 19: Scalable Vector Graphics (SVG)....805
What Is SVG?....805
Scalable....-1
Putting SVG to Work....807
An SVG Toolkit....807
Getting Started....809
Views and Units....812
The Painter’s Model....812
Grouping....814
Transformations....814
Paths....815
Images....818
Text....819
Comments....-1
Scripting....822
SVG on Your Website....823
Tangram: A Simple Application....824
XHTML Wrapper....825
SVG Shapes....826
Tangram Script....830
Useful Resources....837
Summary....838
Exercise Questions....838
Question 1....838
Question 2....839
Chapter 20: XForms....841
How XForms Improves on HTML Forms....842
XForms Tools....842
An Illustrative XForms Example....848
XForms Form Controls....855
The xforms:input Element....855
The xforms:secret Element....856
The xforms:textarea Element....856
The xforms:output Element....856
The xforms:upload Element....857
The xforms:range Element....857
The xforms:trigger Element....858
The xforms:submit Element....858
The xforms:select Element....859
The xforms:select1 Element....860
Constraining XForms Instances....866
The xforms:bind Element....866
W3C XML Schema in XForms....871
Schema or Bind Elements: Which One to Choose?....872
XForms Events....872
The XForms Action Module....873
Developing and Debugging XForms....874
Alternatives to XForms....874
Microsoft InfoPath....874
Adobe LiveCycle....875
HTML Forms....876
Summary....876
Exercise Questions....876
Question 1....876
Question 2....876
Part VIII - Case Study....877
Chapter 21: Case Study: Payment Calculator....879
Mortgage Calculations....879
What You’ll Need....880
Online Loan Calculator....880
Integrating the Calculation Web Service....887
Enhancing the Display with SVG....903
Adding the Frame to the Main Page....906
Summary....910
Chapter 22: Case Study: Payment Calculator — Ruby on Rails....911
Mortgage Calculations....911
What You Need for the Example....912
Creating the Project....913
Building the Online Loan Calculator....914
Summary....944
Appendix A: Exercise Solutions....946
Chapter 1....946
Question 1....946
Question 2....947
Chapter 2....948
Question 1....948
Question 2....951
Chapter 3....955
Question 1....955
Question 2....955
Question 3....956
Chapter 4....957
Question 1....957
Question 2....957
Question 3....958
Chapter 5....958
Question 1....959
Question 2....959
Question 3....960
Chapter 6....961
Question 1....961
Question 2....963
Chapter 7....963
Question 1....963
Question 2....964
Chapter 8....964
Question 1....964
Question 2....964
Chapter 9....964
Question 1....965
Question 2....965
Chapter 10....965
Question 1....965
Question 2....965
Question 3....966
Chapter 11....966
Question 1....966
Question 2....967
Chapter 12....967
Question 1....967
Question 2....970
Chapter 13....972
Question 1....972
Question 2....974
Chapter 14....977
Question 1....977
Question 2....978
Chapter 15....979
Question 1....979
Question 2....979
Chapter 16....981
Question 1....981
Question 2....982
Question 3....982
Chapter 17....982
Question 1....982
Question 2....983
Question 3....984
Question 4....984
Chapter 18....984
Question 1....984
Question 2....987
Chapter 19....988
Question 1....988
Question 2....991
Chapter 20....994
Question 1....994
Question 2....994
Appendix B: XPath Reference....996
XPath 2.0 Is Emerging Slowly....997
The New Sequencing....997
Datatyping Control....997
Axes....997
Node Tests....1000
Functions....1002
XPath 2.0 Functions....1009
Appendix C: XSLT Reference....1012
Elements....1013
Functions....1036
Available XPath Functions....1039
Types....1039
New in XSLT 2.0....1040
More Powerful Expressions....1040
Muenchian’s Everywhere....1041
Planning on Using XSLT 2.0....1041
New Functions....1042
A Quick Note on XSLT 2.0 and XQuery 1.0....1042
Appendix D: The XML Document Obect Model....1044
Notation....1045
Basic Datatypes....1048
Strings....1049
User Data....1049
Objects....1049
Fundamental Interfaces: Core Module....1049
DOMException....1049
DOMError....1051
DOMErrorHandler....1051
Node....1052
Document....1056
DOMImplementation....1058
DOMImplementationSource....1058
DOMImplementationList....1059
DocumentFragment....1060
NodeList....1060
DOMStringList....1060
NameList....1061
Element....1061
NamedNodeMap....1063
Attr....1064
CharacterData....1064
Text....1065
Comment....1065
TypeInfo....1066
UserDataHandler....1066
DOMLocator....1067
DOMConfiguration....1067
Extended Interfaces: XML Module....1068
CDATASection....1068
ProcessingInstruction....1068
DocumentType....1068
Notation....1069
Entity....1069
EntityReference....1070
Load and Save Interfaces....1070
Data Types....1070
LSException....1070
DOMImplementationLS....1071
LSParser....1071
LSSerializer....1073
LSInput....1073
LSOutput....1074
LSResourceResolver....1074
LSParserFilter....1074
LSSerializerFilter....1075
LSProgressEvent....1075
LSLoadEvent....1076
Validation Interfaces....1076
ExceptionVAL....1076
NodeEditVAL....1077
DocumentEditVAL....1078
ElementEditVAL....1078
CharacterDataEditVAL....1080
Appendix E: XML Schema Element and Attribute Reference....1082
Unknown....-1
all....1082
annotation....1083
any....1084
anyAttribute....1085
appinfo....1086
attribute....1086
attributeGroup....1088
choice....1088
complexContent....1089
complexType....1090
documentation....1091
element....1092
extension....1094
field....1095
group....1095
import....1096
include....1097
key....1098
keyref....1100
list....1100
notation....1101
redefine....1101
restriction....1102
schema....1104
selector....1106
sequence....1106
simpleContent....1107
simpleType....1107
union....1108
unique....1109
XML Schema Instance Attributes....1109
Appendix F: XML Schema Datatypes Reference....1112
XML Schema Built-in Datatypes....1113
Constraining Facets....1121
length....1122
minLength....1122
maxLength....1123
pattern....1124
enumeration....1124
whiteSpace....1125
maxInclusive....1125
minInclusive....1126
maxExclusive....1127
minExclusive....1128
totalDigits....1128
fractionDigits....1129
Appendix G: SAX 2.0.2 Reference....1134
Classes and Interfaces....1135
Interface org.xml.sax.Attributes....1136
Interface org.xml.sax.ext.Attributes2....1140
Interface org.xml.sax.ContentHandler....1142
Interface org.xml.sax.ext.DeclHandler....1150
Interface org.xml.sax.DTDHandler....1152
Interface org.xml.sax.EntityResolver....1154
Interface org.xml.sax.ext.EntityResolver2....1156
Interface org.xml.sax.ErrorHandler....1159
Class org.xml.sax.InputSource....1161
Interface org.xml.sax.ext.LexicalHandler....1165
Interface org.xml.sax.Locator....1168
Interface org.xml.sax.ext.Locator2....1170
Exception org.xml.sax.SAXException....1171
Exception org.xml.sax.SAXNotRecognizedException....1173
Exception org.xml.sax.SAXNotSupportedException....1173
Exception org.xml.sax.SAXParseException....1174
Interface org.xml.sax.XMLFilter....1176
Interface org.xml.sax.XMLReader....1177
Index....1186
When the first edition of this book was written, XML was a relatively new language but already gaining ground fast and becoming more and more widely used in a vast range of applications. By the time of the second edition, XML had already proven itself to be more than a passing fad, and was in fact being used throughout the industry for an incredibly wide range of uses. With the third edition, it was clear that XML was a mature technology, but more important, it became evident that the XML landscape was dividing into several areas of expertise. Now in this edition, we needed to categorize the increasing number of specifications surrounding XML, which either use XML or provide functionality in addition to the XML core specification.
So what is XML? It's a markup language, used to describe the structure of data in meaningful ways. Anywhere that data is input/output, stored, or transmitted from one place to another, is a potential fit for XML's capabilities. Perhaps the most well-known applications are web-related (especially with the latest developments in handheld web access—for which some of the technology is XML-based). However, there are many other non-web-based applications for which XML is useful—for example, as a replacement for (or to complement) traditional databases, or for the transfer of financial information between businesses. News organizations, along with individuals, have also been using XML to distribute syndicated news stories and blog entries.
This book aims to teach you all you need to know about XML—what it is, how it works, what technologies surround it, and how it can best be used in a variety of situations, from simple data transfer to using XML in your web pages. It answers the fundamental questions:
This book is for people who know that it would be a pretty good idea to learn XML but aren't 100 percent sure why. You've heard the hype but haven't seen enough substance to figure out what XML is and what it can do. You may be using development tools that try to hide the XML behind user interfaces and scripts, but you want to know what is really happening behind the scenes. You may already be somehow involved in web development and probably even know the basics of HTML, although neither of these qualifications is absolutely necessary for this book.
What you don't need is knowledge of markup languages in general. This book assumes that you're new to the concept of markup languages, and we have structured it in a way that should make sense to the beginner and yet quickly bring you to XML expert status.
The word "Beginning" in the title refers to the style of the book, rather than the reader's experience level. There are two types of beginner for whom this book is ideal:
The subjects covered in this book are arranged to take you from novice to expert in as logical a manner as we could. This Fourth Edition is structured in sections based on various areas of XML expertise. Unless you are already using XML, you should start by reading the introduction to XML in Part I. From there, you can quickly jump into specific areas of expertise, or, if you prefer, you can read through the book in order. Keep in mind that there is quite a lot of overlap in XML, and that some of the sections make use of techniques described elsewhere in the book.
This book builds on the strengths of the earlier editions, and provides new material to reflect the changes in the XML landscape—notably XQuery, RSS and Atom, and AJAX. Updates have been made to reflect the most recent versions of specifications and best practices throughout the book. In addition to the many changes, each chapter has a set of exercise questions to test your understanding of the material. Possible solutions to these questions appear in Appendix A.