- 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
There's no rule that says that you absolutely have to use DTDs, or even W3C XML Schemas to define a grammar for your XML documents. One alternative schema proposal is RELAX NG, pronounced "relaxing". A combination of the original RELAX specification and the TREX specification, RELAX NG provides a simpler way of defining XML grammars than using W3C XML Schemas.
Consider our original document:
<order orderid="THX1138" customerNumber="3263827"> <lineitem itemid="C33"> <quantity>36</quantity> <unitprice currency="dollars">.35</unitprice> </lineitem> <lineitem itemid="M48"> <quantity>1</quantity> <unitprice>2200</unitprice> </lineitem> </order>
It's a single root order element, with one or more lineitem children. The lineitems each have a single quantity and unitprice element. We can express the element structure in a simple, almost natural structure in RELAX NG:
<element name="order" xmlns="http://relaxng.org/ns/structure/1.0"> <oneOrMore> <element name="lineitem"> <element name="quantity"> <text/> </element> <element name="unitprice"> <text/> </element> </element> </oneOrMore> </element>
First, notice that because we can have more than one lineitem element, we've specified it as oneOrMore. If lineitem were optional, we'd specify at as zeroOrMore. Without a modifier, we've specified that quantity and unitprice must appear once and only once.
Next, notice that the quantity and unitprice elements can have text content. RELAX NG enables you to specify all sorts of choices, but for a simple element it's typically text or empty, as in
<element name="status"> <empty/> <element>
RELAX NG also allows you to easily specify a particular range of values, which we'll see in a moment with the currency attribute.
Adding attributes is also simple:
<element name="order" xmlns="http://relaxng.org/ns/structure/1.0"> <attribute name="orderid"><text /></attribute> <attribute name="customerNumber"><text /></attribute> <oneOrMore> <element name="lineitem"> <attribute name="itemid"><text /></attribute> <element name="quantity"> <text/> </element> <element name="unitprice"> <attribute name="currency"><text /></attribute> <text/> </element> </element> </oneOrMore> </element>
As we did with elements, we can specify that attributes can have arbitrary text values, as we're doing here, but we also have the option to specify particular values. For example:
... <element name="unitprice"> <attribute name="currency"> <choice> <value>dollars</value> <value>pounds</value> </choice> </attribute> <text/> </element> ...
In this case, we can specify a value of either dollars or pounds. This nice thing about this syntax, however, is that we can also use it for elements.
We can also easily specify that an attribute or an element is optional. For example, we might make the currency attribute optional:
... <element name="unitprice"> <optional> <attribute name="currency"> <choice> <value>dollars</value> <value>pounds</value> </choice> </attribute> </optional> <text/> </element> ...
This ability to specify optional elements also makes it easy to specify mixed content, in which elements and plain text appear together as the content of an element. For example, we can define a mixed content description element that may include the product name and price:
... <element name="description"> <mixed> <optional> <element name="productname"> <text /> </element> </optional> <optional> <element name="unitprice"> <optional> <attribute name="currency"> <choice> <value>dollars</value> <value>pounds</value> </choice> </attribute> </optional> <text/> </element> </optional> </mixed> </element> ...
You may have noticed that we now have a duplicate definition for the unitprice element. Like XML Schemas, RELAX NG enables us to define various element and attribute types and then refer to them within the document. For example, we can describe the structure of the document as:
<grammar xmlns="http://relaxng.org/ns/structure/1.0"> <start> <element name="order"> <attribute name="orderid"><text /></attribute> <attribute name="customerNumber"><text /></attribute> <ref name="orderContent" /> </element> </start> <define name="orderContent"> <oneOrMore> <element name="lineitem"> <attribute name="itemid"><text /></attribute> <ref name="lineitemContent" /> </element> </oneOrMore> </define> <define name="lineitemContent"> <element name="quantity"> <text/> </element> <element name="unitprice"> <optional> <attribute name="currency"><text /></attribute> </optional> <text/> </element> <optional> <element name="description"> <ref name="descriptionContent"/> </element> </optional> </define> <define name="descriptionContent"> <mixed> <optional> <element name="productname"> <text /> </element> </optional> <optional> <element name="unitprice"> <optional> <attribute name="currency"> <choice> <value>dollars</value> <value>pounds</value> </choice> </attribute> </optional> <text/> </element> </optional> </mixed> </define> </grammar>
We define a grammar and definitions, with the start element showing where the actual structure definition starts.
RELAX NG is intended to be more simple to use than other methods of validation such as XML Schemas, so it doesn't include some of the more data-related functionality such as referential entigrity, in which the value of one element or attribute must be something that's been defined elsewhere in the document. It also leaves out support for the definition of entities and notations. Still, it's got a lot of power, particularly when combined with related specifications such as RELAX NG DTD Compatibility. For more power, you can even reference XML Schema datatypes.