Introduction to DTDs

Defining different types of elements

DTD’s allow for the definition of different types of Elements. It can be specified that an element should contain text content, element content, mixed content or no content. Earlier we have already seen how text content is defined using the string ‘#PCDATA’ and how elements can be added to our <addressBook>. By replacing the string ‘#PCDATA’ with the name of the element <entry>. This specifies that the element <addressBook> can only contain ‘entry’ elements’ (element content).

Next, we structure our entries even further by providing a more descriptive model for address entries:

<!DOCTYPE addressBook [
<!ELEMENT addressBook (entry*)>
<!ELEMENT entry (name, address, phoneNr)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT phoneNr (#PCDATA)> ]>

The above DTD extends the previous DTD. The content model of <entry> was changed from #PCDATA to allow <name>, <address> and <phoneNr>. The new DTD requires that each entry element has exactly one <name> element, one <address> element and one <phoneNr> element.

1
2
3
4
5
6
7
8
9
10
11
12
<addressBook>
   <entry>
      <name>Clark Kent</name>
      <address>344 Clinton Street Metropolis</address>
      <phoneNr>55 50145</phoneNr>
   </entry>
   <entry>
      <name>Bruce Wayne</name>
      <address>1007 Mountain Drive Gotham</address>
      <phoneNr>53 59333</phoneNr>
   </entry>
</addressBook>

Mixed element content is a combination of text and element content. For instance, within the element <phoneNr> it might be necessary to tag the country calling code with an element <countryCode>, but leave the rest of the telephone number as text. Such mixed element content can be defined in the following way:

<!ELEMENT phoneNr (#PCDATA | countryCode)*>
<!ELEMENT countryCode (#PCDATA)>


The above DTD will allow us to identify the country code in the following way:

1
2
3
4
<phoneNr>
    <countryCode>53</countryCode>
    59333
</phoneNr>


Another form of element content are empty elements, elements that contain no content. In HTML the <br /> element indicating a line break and <hr /> element indicating a horizontal row. These are examples of empty elements. The characteristic of empty elements is that they do not contain data, text or child elements and the element is closed immediately. With DTD empty elements can be defined with the keyword ‘EMPTY’. For instance, the following statement defines an empty element named <relationship>:

<!ELEMENT relationship EMPTY>