| Table of Content: 
General overviewThe definition
Simple rules
How to reference a DTD from a documentDeclaring elementsDeclaring attributesSome examplesHow to validateOther resources Well what is validation and what is a DTD ? DTD is the acronym for Document Type Definition. This is a description of
the content for a family of XML files. This is part of the XML 1.0
specification, and allows to describe and check that a given document
instance conforms to a set of rules detailing its structure and content. Validation is the process of checking a document against a DTD (more
generally against a set of construction rules). The validation process and building DTDs are the two most difficult parts
of the XML life cycle. Briefly a DTD defines all the possibles element to be
found within your document, what is the formal shape of your document tree
(by defining the allowed content of an element, either text, a regular
expression for the allowed list of children, or mixed content i.e. both text
and children). The DTD also defines the allowed attributes for all elements
and the types of the attributes. The W3C XML Recommendation (Tim Bray's annotated version of
Rev1): (unfortunately) all this is inherited from the SGML world, the syntax is
ancient... Writing DTD can be done in multiple ways, the rules to build them if you
need something fixed or something which can evolve over time can be radically
different. Really complex DTD like DocBook ones are flexible but quite harder
to design. I will just focus on DTDs for a formats with a fixed simple
structure. It is just a set of basic rules, and definitely not exhaustive nor
usable for complex DTD design. Assuming the top element of the document is specand the dtd
is placed in the filemydtdin the subdirectorydtdsof the directory from where the document were loaded: <!DOCTYPE spec SYSTEM "dtds/mydtd">
 Notes: 
the system string is actually an URI-Reference (as defined in RFC 2396) so you can use a
    full URL string indicating the location of your DTD on the Web, this is a
    really good thing to do if you want others to validate your documentit is also possible to associate a PUBLICidentifier (a
    magic string) so that the DTD is looked up in catalogs on the client side
    without having to locate it on the weba dtd contains a set of elements and attributes declarations, but they
    don't define what the root of the document should be. This is explicitly
    told to the parser/validator as the first element of the
    DOCTYPEdeclaration. The following declares an element spec: <!ELEMENT spec (front, body, back?)>
 it also expresses that the spec element contains one front,
onebodyand one optionalbackchildren elements in
this order. The declaration of one element of the structure and its content
are done in a single declaration. Similarly the following declaresdiv1elements: <!ELEMENT div1 (head, (p | list | note)*, div2?)>
 means div1 contains one headthen a series of optionalp,lists andnotes and then an
optionaldiv2. And last but not least an element can contain
text: <!ELEMENT b (#PCDATA)>
 
bcontains text or being of mixed content (text and elements
in no particular order): <!ELEMENT p (#PCDATA|a|ul|b|i|em)*>
 
p can contain text ora,ul,b,i oremelements in no particular
order. again the attributes declaration includes their content definition: <!ATTLIST termdef name CDATA #IMPLIED>
 means that the element termdefcan have anameattribute containing text (CDATA) and which is optional
(#IMPLIED). The attribute value can also be defined within a
set: <!ATTLIST list type (bullets|ordered|glossary)
"ordered">
 means listelement have atypeattribute with 3
allowed values "bullets", "ordered" or "glossary" and which default to
"ordered" if the attribute is not explicitly specified. The content type of an attribute can be text (CDATA),
anchor/reference/references
(ID/IDREF/IDREFS), entity(ies)
(ENTITY/ENTITIES) or name(s)
(NMTOKEN/NMTOKENS). The following defines that achapterelement can have an optionalidattribute
of typeID, usable for reference from attribute of type
IDREF: <!ATTLIST chapter id ID #IMPLIED>
 The last value of an attribute definition can be #REQUIRED
meaning that the attribute has to be given,#IMPLIEDmeaning that it is optional, or the default value (possibly prefixed by#FIXEDif it is the only allowed). Notes: The directory test/valid/dtds/in the libxml distribution
contains some complex DTD examples. Thetest/valid/dia.xmlexample shows an XML file where the simple DTD is directly included within
the document. The simplest is to use the xmllint program coming with libxml. The
--validoption turn on validation of the files given as input,
for example the following validates a copy of the first revision of the XML
1.0 specification: xmllint --valid --noout test/valid/REC-xml-19980210.xml
 the -- noout is used to not output the resulting tree. The --dtdvalid dtdallows to validate the document(s) against
a given DTD. Libxml exports an API to handle DTDs and validation, check the associated
description. DTDs are as old as SGML. So there may be a number of examples on-line, I
will just list one for now, others pointers welcome: I suggest looking at the examples found under test/valid/dtd and any of
the large number of books available on XML. The dia example in test/valid
should be both simple and complete enough to allow you to build your own. 
 Daniel Veillard |