An XML document with correct syntax is called "Well Formed".
An XML document validated against a DTD is both "Well Formed" and "Valid".
A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a DTD:
The DOCTYPE declaration, in the example above, is a reference to an external DTD file. The content of the file is shown in the paragraph below.
The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements:
The DTD above is interpreted like this:
#PCDATA means parse-able text data.
A doctype declaration can also be used to define special characters and character strings, used in the document:
An entity has three parts: an ampersand (&), an entity name, and a semicolon (;).
With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.
With a DTD, you can verify that the data you receive from the outside world is valid.
You can also use a DTD to verify your own data.
If you want to study DTD, please read our DTD Tutorial.
XML does not require a DTD/Schema.
When you are experimenting with XML, or when you are working with small XML files, creating DTDs may be a waste of time.
If you develop applications, wait until the specification is stable before you add a document definition. Otherwise, your software might stop working because of validation errors.