- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
In XML development, there are actually several "XML Schema" proposals available on the market today. We'll cover some of the others in future sections, but here we're going to discuss the XML Schema Recommendation put out by the W3C.
XML Schemas solve three problems presented by Document Type Definitions:
DTDs use an unfamiliar, nonXML-based syntax.
There's no way to specify two different elements with the same name using a DTD, even if they appear in different places within the document hierarchy.
DTDs have limited datatyping capabilities; it's difficult, if not impossible, to specify that the contents of an element must be a number or a date.
The first two problems are simple; an XML Schema document consists of a valid XML document that describes not only individual elements, as a DTD does, but also their place within the document structure.
The third is arguably where XML Schemas show their power. Not only do XML Schemas provide an opportunity to specify that the content of an element or attribute must conform to one of 47 basic datatypes such as integer or timestamp, but they enable you to create entire element hierarchies as types, and then assign those types to a particular element.
Take our order structure from the earlier "Validation" section:
<order orderid="THX1138" customerNumber="3263827"> <lineitem itemid="C33"> <quantity>36</quantity> <unitprice currency="dollars">.35</unitprice> </lineitem> <lineitem itemid="M48"> <quantity>1</quantity> <unitprice currency="dollars">2200</unitprice> </lineitem> </order>
As with a DTD, we can build up an XML schema piece by piece. To start with, let's look at the lineitem element:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="order"> <xs:complexType> <xs:sequence> <xs:element ref="lineitem" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> <xs:attribute name="customerNumber" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
Here we've created a schema document and defined a single element named order. An element that contains only text, and has no attributes, is said to be a simpleType. Because the order element contains other elements, and because it has attributes, it's a complexType, so we need to specify that.
Within the complexType, we first specify that order contains a sequence of other elements. In this case there's just one other element in that sequence, but we'll see more in a moment. The element is a lineitem, and it's optional (as indicated by a minOccurs value of 0). It may also appear any number of times (as indicated by the maxOccurs value of unbounded).
Next we specify the attributes, orderid and customerNumber. Both are designated as type string, one of the predefined types within the XML Schema specification. We've also indicated that both of them are required.
In this case, we've simply referenced an element, lineitem, which we can then define:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="order"> <xs:complexType> <xs:sequence> <xs:element ref="lineitem" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> <xs:attribute name="customerNumber" type="xs:string" use="required"/> </xs:complexType> </xs:element> <xs:element name="lineitem"> <xs:complexType> <xs:sequence> <xs:element name="quantity" type="xs:string"/> <xs:element name="unitprice" type="priceType"/> </xs:sequence> <xs:attribute name="itemid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
The lineitem element is also a complexType, but in this case we're defining most of it inline (within the definition of the lineitem element itself). Notice that we've specified the elements directly by name, rather than by reference. Also notice that this time we've referred to a type (priceType) that's not within the XML Schema namespace (prefixed by xs:). This is a custom type that we can then define explicitly:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="order"> <xs:complexType> <xs:sequence> <xs:element ref="lineitem" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> <xs:attribute name="customerNumber" type="xs:string" use="required"/> </xs:complexType> </xs:element> <xs:element name="lineitem"> <xs:complexType> <xs:sequence> <xs:element name="quantity" type="xs:string"/> <xs:element name="unitprice" type="priceType"/> </xs:sequence> <xs:attribute name="itemid" type="xs:string" use="required"/> </xs:complexType> </xs:element> <xs:complexType type="priceType"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="currency"> <xs:simpleType> <xs:restriction base = "xs:string"> <xs:enumeration value="dollars"/> <xs:enumeration value="pounds"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:schema>
The actual definition is a bit beyond the scope of this discussion, but notice that we've defined the complexType independently, so we can refer to it from any element we wish. Simply put, priceType describes an element that may have only text content (simpleContent) but has an attribute (currency) that must consist of either the string value dollars or the string value pounds.
In this way, you can create a schema document that's much more flexible than a DTD. To reference the schema from an instance document, for an XML document to be validated against the schema, it's customary to add the schema location via attributes on the root element:
<?xml version="1.0"?> <order orderid="THX1138" customerNumber="3263827" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="myschema.xsd"> <lineitem itemid="C33"> <quantity>36</quantity> <unitprice currency="dollars">.35</unitprice> </lineitem> <lineitem itemid="M48"> <quantity>1</quantity> <unitprice currency="dollars">2200</unitprice> </lineitem> </order>
This is known as a hint. In most cases, you can also specify the schema directly from within the application.