Chapter 3. Document Type Definitions (DTDs)
While XML is extremely flexible, not all the programs that read
particular XML documents are so flexible. Many programs can work with
only some XML applications but not others. For example, Adobe
Illustrator can read and write Scalable Vector Graphics (SVG) files, but
you wouldn’t expect it to understand a Platform for Privacy Preferences
(P3P) document. And within a particular XML application, it’s often
important to ensure that a given document adheres to the rules of that
XML application. For instance, in XHTML, li
elements should only be children of
ul
or ol
elements. Browsers may not know what to do
with them, or may act inconsistently, if li
elements appear in the middle of a blockquote
or p
element.
XML 1.0 provides a solution to this dilemma: a document type
definition (DTD). DTDs are written in a formal syntax that explains
precisely which elements may appear where in the document and what the
elements’ contents and attributes are. A DTD can make statements such as
“A ul
element only contains li
elements” or “Every employee
element must have a social_security_number
attribute.” Different
XML applications can use different DTDs to specify what they do and do
not allow.
A validating parser compares a document to its DTD and lists any places where the document differs from the constraints specified in the DTD. The program can then decide what it wants to do about any violations. Some programs may reject the document. Others may try ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.