DTD <!DOCTYPE>

If you've had the opportunity to view some XML documents, you may have noticed a line starting with <!DOCTYPE appearing near the top of the document.

If you've viewed the source code of a (valid) XHTML file, you may have seen a line like this:

The purpose of this line is to declare the Document Type Definition (DTD). Actually, we even used the !DOCTYPE declaration in a previous lesson to define an entity.

As mentioned in the previous lesson, a DTD specifies the rules regarding the elements within your XML document.

DOCTYPE Syntax

To use a DTD within your XML document, you need to declare it. The DTD can either be internal (written into the same document that it's being used in), or external (located in another document).

You declare a DTD at the top of your XML document (in the prolog) using the !DOCTYPE declaration. The basic syntax is:

...where, rootname is the root element, and [DTD] is the actual definition.

Actually, there are slight variations depending on whether your DTD is internal or external (or both), public or private. They are outlined below.

DTD Variations

<!DOCTYPE rootname [DTD]>

Example:

<!DOCTYPE rootname SYSTEM URL>

Example:

<!DOCTYPE rootname SYSTEM URL [DTD]>

Example:

<!DOCTYPE rootname PUBLIC identifier URL>

Example:

<!DOCTYPE rootname PUBLIC identifier URL [DTD]>

Example:

The next few lessons demonstrate some of the different methods of declaring your DTD, depending on whether you're using an internal, external, or combined DTD.