Training
Certifications
Books
Special Offers
Community




 
XML Pocket Consultant
Author William R. Stanek
Pages 416
Disk N/A
Level All Levels
Published 01/16/2002
ISBN 9780735611832
Price $29.99
To see this book's discounted price, select a reseller below.
 

More Information

About the Book
Table of Contents
Sample Chapter
Index
Related Series
About the Author

Support: Book & CD

Rate this book
Barnes Noble Amazon Quantum Books

 


Chapter 3: Creating DTDs continued


Working with Internal DTDs

You 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 DTDs

Internal 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" ?>
<!DOCTYPE purchase_order [
 
]>

Within the DTD for the purchase_order document, you could then define elements, such as:

<!DOCTYPE purchase_order [
 <!ELEMENT purchase_order (customer)>
 <!ELEMENT customer (account_id, name)>
 <!ELEMENT account_id (#PCDATA)>
 <!ELEMENT name (first, mi, last)>
 <!ELEMENT first (#PCDATA)>
 <!ELEMENT mi (#PCDATA)>
 <!ELEMENT last (#PCDATA)>
]>

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 [
 <!ELEMENT last (#PCDATA)>
 <!ELEMENT mi (#PCDATA)>
 <!ELEMENT first (#PCDATA)>
 <!ELEMENT name (first, mi, last)>
 <!ELEMENT account_id (#PCDATA)>
 <!ELEMENT customer (account_id, name)>
 <!ELEMENT purchase_order (customer)>
]>

or this order:

<!DOCTYPE purchase_order [
 <!ELEMENT account_id (#PCDATA)>
 <!ELEMENT customer (account_id, name)>
 <!ELEMENT first (#PCDATA)>
 <!ELEMENT last (#PCDATA)>
 <!ELEMENT mi (#PCDATA)>
 <!ELEMENT name (first, mi, last)>
 <!ELEMENT purchase_order (customer)>
]>

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 &gt; or &lt; 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" ?>
<!DOCTYPE purchase_order [
 <!ELEMENT purchase_order (customer)>
 <!ELEMENT customer (account_id, name)>
 <!ELEMENT account_id (#PCDATA)>
 <!ELEMENT name (first, mi, last)>
 <!ELEMENT first (#PCDATA)>
 <!ELEMENT mi (#PCDATA)>
 <!ELEMENT last (#PCDATA)>
]>

<purchase_order>
 <customer>
  <account_id>10-487</account_id>
  <name>
   <first> William </first>
   <mi> R </mi>
   <last> Stanek </last>
  </name>
  </customer>
</purchase_order>

Adding Internal DTDs to Documents

To declare an internal DTD in an XML document, follow these steps:

  1. Open your XML document for editing. At the top of the document following the XML declaration, type <!DOCTYPE root_name [, where root_name is the name of the document's root element.
  2. Enter a few blank lines, in which you'll later enter your declarations as discussed in Chapter 4, "XML Elements in DTDs," Chapter 5, "XML Attributes in DTDs," and Chapter 6, "XML Entities and Notations in DTDs."
  3. Type ]> to complete the DTD.

The result should look similar to the following example:

<?XML version="1.0" ?>
<!DOCTYPE purchase_order [
 
]>


Previous   |  Table of Contents   |   Next



Last Updated: January 17, 2002
Top of Page