In the last section, we reviewed the process of creating a "well-formed"
XML document.
As you saw,
there are many rules you must follow in order to assure that your XML document is
well-formed. But even when you write well-formed XML documents, you're not quite out of
the woods! Making your document well-formed is only half the battle. You must
also make sure that the document is valid
A valid document by definition, is a well-formed XML document. But validity goes one step
further. A valid XML document is also a well-formed SGML document, and as such, can
be read and interpreted as one.
To pass the SGML validity test, an XML document must conform to the specifications defined
by a Document Type Definition (DTD). You can think of the DTD as
defining the overall structure and syntax of the document.
The DTD is in fact the meat of the "meta-markup" concept.
The DTD defines the grammar and vocabulary of a markup language.
In short, the DTD specifies everything a parser needs to know
in order for that parser to interpret a well-formed XML document.
This "specification" can be as simple as listing all the valid elements
(such as elements, tags, attributes, entities) that
an XML document may contain, or can be as complex as specifying relationships
between those elements (such as element X must contain either Element Y or
Element Z but never both).
For example, do you remember our CONTACT XML document from previous sections?
A CONTACT DTD might specify that every CONTACT has an
<ADDRESS> element that must define <STREET>, <CITY>,
<STATE>, and <ZIP> elements, in that particular order.
Further, the DTD could specify that <ADDRESS> elements
may contain multiple <STREET> elements (though they must at least
contain one).
To specify grammatical rules, DTDs take advantage of a set of
regular expressions that match for specified patterns within the XML document
in order to determine whether or not the document is valid.
Matching is done conservatively so that
anything not specifically allowed by the DTD is forbidden.
Okay, enough about what DTD's are....let's look at how you'll build
them.