What is a DTD ?

Document Type Declaration (DTD) is a mechanism (set of rules) to describe the structure, syntax and vocabulary of XML documents.  It is a modeling language for XML but it does not follow the same syntax as XML.

Element Declaration

Following lines show the possible syntaxes for element declaration

<!ELEMENT reports (employee*)>

<!ELEMENT employee (ss_number, first_name, middle_name, last_name, email, extension, birthdate, salary)>

<!ELEMENT email (#PCDATA)>

<!ELEMENT extension EMPTY>

#PCDATA    -    Parsed Character Data, meaning that elements can contain text.  This requirement means that no child elements may be present in the element within which #PCDATA is specified

EMPTY        -    Indicates that this is the leaf element, cannot contain any more children

Occurrence

There are notations to specify the number of occurrences that a child element can occur within the parent element.  These notations can appear at the end of each child element

+   -   Element can appear one or several times
*   -   Element can appear zero or several times
?   -   Element can appear zero or one time
 nothing    -   Element can appear only once (also, it must appear once)

Separators

,   -   Elements on the right and left of comma must appear in the same order
|   -   Only one of the elements on the left or right of this symbol must appear

Attribute Declaration

Following lines show the possible syntaxes for attribute declaration

<!ATTLIST customer ID CDATA #REQUIRED>

<!ATTLIST customer Preferred (true | false) "false">

customer         -        Element name

ID, Preferred   -       Attribute names

(true | false)     -        Possible attribute values

false                -        Default attribute value

CDATA         -        Character data

#REQUIRED -        Attribute value must be provided

#IMPLIED     -        If no value is provided, application must use its own default

#FIXED         -        Attribute value must be the one that is provided  in DTD