An Example DTD

If you were to translate the English description of the example library XML document into a DTD, it might look something like the following:

<?xml version="1.0"?> <!ELEMENT library (book+)> <!ATTLIST library owner CDATA #REQUIRED

<!ELEMENT book (title, author+)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)>

To add a reference to this DTD in the library file discussed before, you would insert a line at the top of the file after the XML declaration that read <!DOCTYPE config SYSTEM "library.dtd">, where library.dtd was the path to the DTD on your system.

Let's break this down, one step at a time. The first line, <?xml version="1.0"?>, tells you that this is going to be an XML document. Technically, this line is optional; DTDs don't behave like other XML documents, but we'll get to that later. The next line, <!ELEMENT library (book+)>, tells you that there is an element known as library, which can have one or more child elements of the book type. The syntax for element frequencies and grouping in DTDs is terse, but similar to that of regular expressions. The following table lists element frequency and element grouping operators in DTDs.




Specifies zero or one of the preceding elements. For instance, editor? would mean that a book could have an optional editor element.


Specifies one or more of the preceding element. As in the previous example, author+ means that a book has one or more authors.


Specifies a sequence of elements that must occur in that order. (title, author+) means that the book must have a title, followed by one or more authors, in that order.


Groups elements together. An operator applied after parentheses applies to all elements in the group. For instance, (author, editor) + would mean that a document could have one or more authors and one or more editors.


Or operator. This operator permits a choice between alternatives. As an example, (author | editor) would permit a book to have an author or an editor, but not both.


Specifies that zero or more of the preceding element or group can appear. (book, CD)* would permit the library to have any number of books and CDs in it, or none at all.

The next bit is a little more complex:


The first line specifies that the library element has a list of attributes. Notice that the attribute list is separate from the library element declaration itself and linked to it by the element name. If the element name changes, the attribute list must be updated to point to the new element name. Next is a list of attributes for the element. In this case, library has only one attribute, but the list can contain an unbounded number of attributes. The attribute declaration has three mandatory elements: an attribute name, an attribute type, and an attribute description. An attribute type can either be a data type, as specified by the DTD specification, or a list of allowed values. The attribute description is used to specify the behavior of the attribute. A default value can be described here, and whether the attribute is optional or required.

Was this article helpful?

0 0

Post a comment