A Document Type Definition (DTD) is a document that describes the structure of an XML document, what elements and attributes it contains and what values they may have.
DTD's form part of the W3C's XML Standard, but are typically considered to be a separate schema technology and are not typically used in conjunction with other schema formats like XSD, RelaxNG etc.
A DTD document has its own syntax and grammar, the rules are simple, but with because definitions for elements and attributes are split, making sense of the document can be time consuming to process by hand. Furthermore if entity references are used, then a recursive pre-processing step may also need to be applied which can make it very difficult to interpret them by hand (see Tricky DTD Sample).
A DTD document can be embedded within an XML file or can exist on its own. Common definitions can also be broken out into files and included in order to increase re-use or maintainability.
BookStore.dtd Sample |
Copy Code
|
---|---|
<!ELEMENT bookstore (book*)> <!ELEMENT book (title,author,genre?)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (first-name, last-name)> <!ELEMENT genre (#PCDATA)> <!ELEMENT first-name (#PCDATA)> <!ELEMENT last-name (#PCDATA)> <!ATTLIST book price CDATA #REQUIRED> <!ATTLIST book publicationdate CDATA> <!ATTLIST book IBSN CDATA> |
The following expressions appear within a DTD document.
DTD Expression | Description |
---|---|
CDATA | Data Type : Character Data |
ID | Data Type : Unique Identifier |
IDREF | Data Type : Reference to a Unique Identifier |
IDREFS | Data Type : Reference to a 0-n Unique Identifiers |
ENTITY | Data Type : Reference to an ENTITY Declaration |
ENTITIES | Data Type : Reference to an 0-n ENTITY Declarations |
NMTOKEN | Data Type : Named Token |
NMTOKENS | Data Type : 0-n Named Tokens |
? | Element cardinality : Optional (0-1) |
* | Element cardinality : Zero to Many (0-n) |
+ | Element cardinality : One to Many (1-n) |
, | Sequence : separates items within a sequence |
| | Choice : separates items within a choice |
--> | Ends a comment |
<!-- | Starts a comment |
ANY | Declares an attribute on an Element |
ATTLIST | Declares an attribute on an Element |
EMPTY | Declares an attribute on an Element |
DOCTYPE | Starts a DTD declaration |
ELEMENT | Defines an XML Element |
ENTITY | Defines an ENTITY, which can be used as a replacement token |
NOTATION | |
#PCDATA | Allows mixed content to be defined |
#REQUIRED | Attribute property : Attribute is required |
#FIXED | Attribute property : Attribute must have a specific value |
#IMPLIED | Attribute property : Attribute is optional (0-1) |
SYSTEM | ENTITY property : The entity value is to be loaded from a uri |
PUBLIC | ENTITY property : The entity value is refered to by a public identifier, or can be loaded from a uri |