Creating a DTD When creating your own DTD you'll need to define all the elements and attributes that you'll be using in your XML documents. In this case we'll be creating a DTD for our message XML document. Some syntax to remember when creating DTDs are the following: A comma equals the boolean operator AND A horizontal bar equals the boolean operator OR parenthenses means this item occurs only once a plus following an item says that the item must occur at least once a question mark following an item name means that it occurs either once or not and a asterick following an item name means that it can occur zero or more times. Elements are declared in the following manner. You have the element declaration, element name and then the element part(s). Then you close the element declaration. Then for declaring attributes you have the attribute declaration, the element name that the attribute is attached to, the attribute name, the type of attribute it is and then if there is any default values for the attribute and then close the attribute declaration. Let's take a look at the DTD I created for the message.xml file. Here's the message DTD we've created. Since we have main element being... the root element being message have to define that which we said has two parts either email or letter. Letter is composed of letterhead and text. Email's composed of header, subject, or text. Subject can or can not be there and you can have more than one text area. The attributes listed we have letter and email both having the attribute reply which has the possible values of yes or no and the default value is no. Then we have the element header which has the elements in it sender, recipient (which can be zero or more) and date (which can or can not be there.) We also have the element subject which just contains PC character data which is what you would use if you were going to have plain text within an element. Then letterhead has sender, recipient, and date. Sender has PC character data, recipient has PC character data, date has PC character data, and then text either has PC character data or salutations zero or more times. Then salutation would be PC character data. After creating this DTD we would save it as message.dtd. Then we need to link our XML documents to this DTD so that they can be validated against it. To link to an external dtd you include declaration doctype, the root element in the DTD, in this case message, the location SYSTEM and then in ( ) the name of the file. This could also be a hyper reference like http:// and then the url of the location of the DTD if it was hosted somewhere online. If this was the case then you would want to change SYSTEM to PUBLIC. Let's take a look what it would look like if you declared a DTD internally. The structure of declaring internally would be very similar. You would have the DOCTYPE declaration the name of the root element, you would have an open [ all the element and attribute declarations and then a closing ] and then a closing tag >. So if we wrapped that email.xml file in this internal document declaration this is what it would look like. It would have the DOCTYPE declaration, the message and then the element names and attribute lists and then the closing bracket and then the closing tag. Now that you've learned how to link a DTD to a document both internally and externally let's take a look at validating an XML document.