Datasheet
DTDs
The XML 1.0 specification describes a grammar using a document type declaration (DTD). The language
for writing a DTD is taken from SGML and doesn’t look anything like XML. DTDs can’t deal with
namespaces and don’t allow you to say anything about the data between a start and end tag. Suppose
you have an element that looks like this:
<quantity>5</quantity>
Perhaps you’d like to be able to say that the content of a <quantity> element is a non-negative integer.
Unfortunately, you can’t say this using DTDs.
XML Schema
Shortly after XML was released, the W3C started a Working Group to define a new language for describ-
ing XML grammars. Among the goals for this new schema language were the following:
❑ Describe the grammar/schema in XML.
❑ Support the use of XML Namespaces.
❑ Allow rich datatypes to constrain element and attribute content.
The result of the working group’s effort is known as XML Schema. The XML Schema specification is bro-
ken into two parts:
❑ XML Schema Part 1: Structures describes XML Schema’s facilities for specifying the rules of a
grammar for an XML document. It also describes the rules for using XML Schema in conjunc-
tion with namespaces.
❑ XML Schema Part 2: Datatypes covers XML Schema’s rich set of datatypes that enable you to
specify the types of data contained in elements and attributes. There are a lot of details to be
taken care of, which has made the specification very large.
If you’re unfamiliar with XML Schema, XML Schema Part 0: Primer is a good introduction.
Relax-NG
The third option for specifying the grammar for an XML document is Relax-NG. It was designed to ful-
fill essentially the same three goals that were used for XML Schema. The difference is that the resulting
specification is much simpler. Relax-NG is the result of a merger between James Clark’s TREX and
MURATA Makoto’s Relax. Unfortunately, there hasn’t been much industry support for Relax-NG, due to
the W3C’s endorsement of XML Schema. Andy Clark’s Neko XML tools provide basic support for Relax-
NG that can be used with Xerces. We’ll cover the Neko tools a bit later in the chapter.
Validity Example
Let’s go back to the example XML file. We’ve chosen to specify the grammar for the book.xml document
using XML Schema. The xsi:schemaLocation attribute in lines 4-5 works together with the default
namespace declaration in line 2 to tell the XML parser that the schema document for the namespace
http://sauria.com/schemas/apache-xml-book/book is located at http://www.sauria.com
/schemas/apache-xml-book/book.xsd. The schema is attached to the namespace, not the document.
5
Xerces
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 5