A Simple DTD Example
Recall Example 2-2
from the last chapter, which described a person. The
person had a name and three professions. The name had a first name
and a last name. The particular person described in that example was
Alan Turing. However, that’s not relevant for DTDs. A DTD
only describes the general type, not the specific instance. A DTD
for person documents would say that a person
element contains one name
child element followed by zero or
more profession
child elements.
It would further say that each name
element contains exactly one first_name
child element followed by
exactly one last_name
child
element. Finally it would state that the first_name
, last_name
, and profession
elements all contain text.
Example 3-1 is a DTD that
describes such a person
element.
<!ELEMENT person (name, profession*)> <!ELEMENT name (first_name, last_name)> <!ELEMENT first_name (#PCDATA)> <!ELEMENT last_name (#PCDATA)> <!ELEMENT profession (#PCDATA)>
This DTD would probably be stored in a separate file from the documents it describes. This allows it to be easily referenced from multiple XML documents. However, it can be included inside the XML document if that’s convenient, using the document type declaration we discuss later in this section. If it is stored in a separate file, then that file would most likely be named person.dtd, or something similar. The .dtd extension is fairly standard although not specifically required by the XML specification. If this ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.