DTD Basics   «Prev  Next»

Lesson 10A checklist for creating DTDs
ObjectiveCreate a DTD from an existing set of tags.

Checklist for creating Document Type Definitions

The following checklist outlines the steps you can take to create a DTD from an existing set of tags:
  1. Determine the root element for the document.
  2. List all the other elements used in the document.
  3. Create the document tree structure for these elements.
  4. Note whether elements need to appear in a specific order.
  5. Note whether certain elements have content that must be present, can be present, or may be present multiple times.
  6. Note which elements will contain text, which will only contain other elements, and which may contain both.

Well-formed XML

As you have seen so far, you can write well-formed XML by following a few simple rules. In effect, these rules describe how to determine one of XML's key characteristics: its structure. Without the rules, the material is ambiguous and well-formedness only goes so far. Several other technologies are available for validating XML documents, each with their own advantages and disadvantages.
Document Type Definitions (DTDs) offer a way of specifying further rules that help in the interpretation of documents and their structure. By using namespaces it is possible to create unambiguously distinct sets of elements and attributes according the purpose you have in mind. Whether or not they appear in a namespace, such special-purpose sets of terms are sometimes known as vocabularies. You can describe the structure and vocabulary of a XML language in a software-friendly fashion, which can be used to check whether XML documents meet the requirements. This checking is known as validation.


The Document Type Declaration in Detail

The document type declaration, or DOCTYPE, informs the parser that your document should conform to a DTD. It also indicates where the parser can find the rest of the definition. In the first example, the DOCTYPE was simple:
<<!DOCTYPE name [ ]>

The document type declaration always begins in the same way, with <!DOCTYPE, followed by some whitespace, just as there is after element names. Note that whitespace is not allowed to appear in between DOCTYPE and the opening <!. After the whitespace, the name of the XML document's root element must appear exactly as it will in the document, including any namespace prefix. Because the document's root element is <name>, the word name follows the opening <!DOCTYPE in the previous declaration.
Caution: Remember that XML is case sensitive. Therefore, anytime you see a name in XML, it is case sensitive. When the recommendation says the name must appear exactly as it will in the document, this includes character case. You will see this throughout the DTD; any reference to XML names implies case sensitivity.
Following the name of the root element, you have several options for specifying the rest of the document type declaration. In the <name> example, the element declarations appeared between the [ and ] of the DTD. When declarations appear between the [ and ], as in the example, they are called internal subset declarations. It is also possible to have some or all of your declarations in a separate document. DTD declarations that appear in external documents are external subset declarations. You can refer to an external DTD by either using system identifiers or public identifiers.
Note: Aside from experimentation and development, it's rarely necessary to use internal DTDs, and external DTDs have many advantages. For example, because the DTD appears in a single separate document, it is easier to make changes that apply to all documents that use the DTD. Later in this course you look at XML documents and DTDs that consist of many fi les using entities. You must remember, however, that looking up the DTD file takes extra processing time. In addition, if the DTD fi e is located on the Internet, you have to wait for it to download. Usually it is better to keep a local copy of the DTD for validation purposes or ensure the tools you are using have a caching mechanism. If you are maintaining a local copy, you should check for changes to the DTD at the original location.

DTD Mechanics Creation
The next lesson concludes this module.

DTD Checklist - Exercise

Click the Exercise link below to create a DTD.
DTD Checklist - Exercise