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]>
- This is an internal DTD (the DTD is defined between the square brackets within the XML document).
Example:
<!DOCTYPE rootname SYSTEM URL>
- The keyword
SYSTEM
indicates that it's a private DTD (not for public distribution). - The presence of
URL
and [DTD
] together indicates that this is both an external and internal DTD (part of the DTD is defined in a document located at the URL, the other part is defined within the XML document).
Example:
<!DOCTYPE rootname SYSTEM URL [DTD]>
- The keyword
SYSTEM
indicates that it's a private DTD (not for public distribution). - The presence of
URL
and [DTD
] together indicates that this is both an external and internal DTD (part of the DTD is defined in a document located at the URL, the other part is defined within the XML document).
Example:
<!DOCTYPE rootname PUBLIC identifier URL>
- The keyword
PUBLIC
indicates that it's a public DTD (for public distribution). - The presence of
URL
indicates that this is an external DTD (the DTD is defined in a document located at the URL). - The identifier indicates the formal public identifier and is required when using a public DTD.
Example:
<!DOCTYPE rootname PUBLIC identifier URL [DTD]>
- The keyword
PUBLIC
indicates that it's a public DTD (for public distribution). - The presence of
URL
and [DTD
] together indicates that this is both an external and internal DTD (part of the DTD is defined in a document located at the URL, the other part is defined within the XML document). - The identifier indicates the formal public identifier and is required when using a public 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.