|
Chapter 3: Creating DTDs continued
Working with Internal DTDsYou specify internal DTDs using the DOCTYPE assignment. The DOCTYPE assignment is one of the most basic elements in an XML document. Similar to the document type element, which is a container for all other elements, the DOCTYPE declaration is a container for all DTD assignments.
Declaring Internal DTDsInternal DTD declarations are formatted as follows:
<!DOCTYPE root_name [ assignments ]> The declaration begins with the DOCTYPE keyword, followed by the name of the root element for the document. Typically, the name of the root element serves as a descriptor for the type of information the document contains. The root name is followed by an open bracket, which signifies the beginning of the declaration assignments. Because there's usually a large group of declarations, assignments are normally entered on separate lines following the document type declaration. The last entry in the document type declaration is always the closing bracket for the DOCTYPE keyword. Following this, if you wanted to structure a set of purchase orders, you might define the document type as follows:
<?XML version="1.0" ?>
Within the DTD for the purchase_order document, you could then define elements, such as:
<!DOCTYPE purchase_order [ The example DTD declares seven elements (purchase_order, customer, account_id, name, first, mi, and last) and sets the order in which those elements may be entered in a document. The line breaks used aren't relevant to the DTD, and neither is the order in which the elements are listed. Although the elements are entered from the highest level to the lowest level, you could also enter them in this order:
<!DOCTYPE purchase_order [ or this order:
<!DOCTYPE purchase_order [ As these examples show, the order of DTD declarations isn't important. What is important are the declaration, the declaration name, and the associated values. In the case of elements, the values in parentheses set the order in which the elements must be used. Here, customer elements must contain exactly one account_id element followed by exactly one name element. The name element must contain exactly one first element followed by exactly one mi ele-ment followed by exactly one last element. The first, mi, and last elements must contain parsed character data (#PCDATA), which is raw text that could contain entity references, such as > or < but don't contain other markup or child elements. The spacing between the declaration name and other elements is also important. The following declaration is improperly formatted:
<!ELEMENTaccount_id (#PCDATA)> as is the following declaration:
<!ELEMENT account_id(#PCDATA)> The correct format is:
<!ELEMENT account_id (#PCDATA)> Listing 3-1 provides the source for a basic document with an internal DTD. As you can see, the internal DTD definition follows the XML declaration and is in turn followed by the document's contents. The opening tag for the root element, purchase_order, is the first tag in the body of the document. It's followed by other elements in the order prescribed in the DTD. The closing tag for the root element is that last item in the document. Listing 3-1. An XML Document with an Internal DTD
<?xml version="1.0" ?>
Adding Internal DTDs to DocumentsTo declare an internal DTD in an XML document, follow these steps:
The result should look similar to the following example:
<?XML version="1.0" ?>
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||