DTD Introduction
DTD stands for Document Type Definition. A DTD allows you to create rules for the elements within your XML documents. Although XML itself has rules, the rules defined in a DTD are specific to your own needs.
So, for an XML document to be well-formed, it needs to use correct XML syntax, and it needs to conform to its DTD or schema (we'll cover XML schemas later).
The DTD is declared at the top of your XML document. The actual contents of the DTD can be included within your XML document or included in another document and linked to (or both).
Do I Need to Create a DTD?
If you have created your own XML elements, attributes, and/or entities, then you should create a DTD.
If you are creating an XML document using pre-defined elements/attributes/entities (i.e. ones that have been created by someone else), then a DTD should already exist. All you need to do is link to that DTD using the DOCTYPE
declaration. (We'll cover DOCTYPE
soon).
What's in a DTD?
A DTD consists of a list of syntax definitions for each element in your XML document.
When you create a DTD, you are creating the syntax rules for any XML document that uses the DTD. You are specifying which element names can be included in the document, the attributes that each element can have, whether or not these are required or optional, and more.
Example DTD
The following example demonstrates what a DTD could look like:
I'll explain the DTD syntax a little later on, but for now, let's look at how to configure your XML document to use a DTD.