DTDs
Home ] Up ] [ DTDs ] XML Schema ]

 

Operators & Keywords
Sharing DTDs
Declaring Attributes

 

A DTD defines the structure of an XML document, and provides some information about the required content.

A DTD provides for the declaration of elements, attribute lists, entities, and notations. It is a sequence of declarations enclosed in a DOCTYPE declaration, or stored separately and referred to from a DOCTYPE.

Validity of a document, based on a DTD, operates on the principle that everything not permitted is forbidden. Everything in the document must match a declaration in the DTD.  If a document has a DTD and the document satisfies the DTD, the the document is considered valid.  Otherwise, it is invalid.

However, a DTD is not capable of specifying everything about a document.  For example, it does not provide for:

  • Which element is the document's root element
  • The specific number of instances of each kind of element in the document.
  • The content of character data inside an element
  • The semantic meaning of an element.

Here's a simple example of a DTD:

<!ELEMENT person (name, profession)>
<!ELEMENT name (first-name, last-name)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT last-name (#PCDATA)>
<!ELEMENT profession (#PCDATA)>

This can be used in an XML file to specify the contents:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE person [
  <!ELEMENT person (name, profession)>
  <!ELEMENT name (first-name, last-name)>
  <!ELEMENT first-name (#PCDATA)>
  <!ELEMENT last-name (#PCDATA)>
  <!ELEMENT profession (#PCDATA)>
]>
<person>
  <name>
    <first-name>Bryan</first-name>
    <last-name>Higgs</last-name>
  </name>
  <profession>Computer Scientist</profession>
</person>

In contrast, the file:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE person [
  <!ELEMENT person (name, profession)>
  <!ELEMENT name (first-name, last-name)>
  <!ELEMENT first-name (#PCDATA)>
  <!ELEMENT last-name (#PCDATA)>
  <!ELEMENT profession (#PCDATA)>
]>
<person>
  <name>
    <first-name>Bryan</first-name>
    <last-name>Higgs</last-name>
  </name>
</person>

is not valid, because we have no <profession> specified.

Also, if we specified in a file:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE person [
  <!ELEMENT person (name, profession)>
  <!ELEMENT name (first-name, last-name)>
  <!ELEMENT first-name (#PCDATA)>
  <!ELEMENT last-name  (#PCDATA)>
  <!ELEMENT profession (#PCDATA)>
]>
<person>
  <name>
    <first-name>Bryan</first-name>
    <last-name>Higgs</last-name>
  </name>
<profession>Computer Scientist</profession>
<profession>Physicist</profession>
</person>
This would also be invalid, because there is more than one <profession> was specified.

 

The page was last updated February 19, 2008