- The document type (DOCTYPE) declaration consists of an internal, or references an external Document Type Definition (DTD).
- It can also have a combination of both internal and external DTDs. The DTD defines the constraints on the structure of an XML document.
- It declares all of the document's element typesglossary, children element types, and the order and number of each element type.
- It also declares any attributes, entities, notations, processing instructions, comments, and PE references in the document.
Internal DTD:
Following is a example of Internal DTD:
!DOCTYPE root_element [
Document Type Definition (DTD):
elements/attributes/entities/notations/
processing instructions/comments/PE references
]>
Rules:
- The document type declaration must be placed between the XML declaration and the first element (root element) in the documentwell-formedness constraint.
- The keyword DOCTYPE must be followed by the name of the root element in the XML documentvalidity constraint.
- The keyword DOCTYPE must be in upper case
Example:
<?xml version="1.0" standalone="yes" ?>
<!--open the DOCTYPE declaration -
the open square bracket indicates an internal DTD-->
<!DOCTYPE foo [
<!--define the internal DTD-->
<!ELEMENT foo (#PCDATA)>
<!--close the DOCTYPE declaration-->
]>
<foo>Hello World.</foo>
The External DTD:
- External DTDs are useful for creating a common DTD that can be shared between multiple documents.
- Any changes that are made to the external DTD automatically updates all the documents that reference it.
- There are two types of external DTDs: private, and public.
Rules:
- If any elements, attributes, or entities are used in the XML document that are referenced or defined in an external DTD, standalone="no" must be included in the XML declarationvalidity constraint.
"Private" External DTDs:
Private external DTDs are identified by the keyword SYSTEM, and are intended for use by a single author or group of authors.
where:
DTD_location: relative or absolute URLglossary
XML Code:
<!--inform the XML processor
that an external DTD is referenced-->
<?xml version="1.0" standalone="no" ?>