DTD (Document Type Definition) contains a description what is allowed in XML files. DTD can therefore be used by a parser to verify that XML files are valid.

The DTD can either be included in the xml file or be a stand alone file. It defines the tags being allowed, their sequence and its content. Unfortunately they require to learn yet an other syntax. For the previous car example it could look as:

Example 12.2. DTD

<!ELEMENT car (brand, type, color?)>
<!ELEMENT brand (#PCDATA)>
<!ELEMENT color (#PCDATA)>

The ? after color is a special character. Those character mean:

  1. may occur once or not at all ?

  2. must occur at least once or many times +

  3. may occur not at all or many times *

There is also a choice character

<!ELEMENT boat(sails¦motors)>

PCDATA means parsed character data.

CDATA means not parsed character data.

Also attributes need to be mentioned (for every attribute one line)

<!ATTLIST <element name> id ID #REQUIRED<attrbute name> CDATA #IMPLIED >

Implied means optional. CDATA means a string and ID that can be used to identification to the element.

To include the DTD in the XML file the second line must be as:

Example 12.3. DTD in XML

<!DOCTYPE car[
<!ELEMENT car (brand, type, color?)>
<!ELEMENT brand (#PCDATA)>
<!ELEMENT color (#PCDATA)>

To have it externally

<!DOCTYPE car SYSTEM "URL address">

This is also identical to the first line in the DTD standalone file.

Finally you could put something as:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

in the first line to to have the attribute standalone="yes" to disable DTD verification.

Linurs startpage