XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / Two DTD examples

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

Some of the best DTD design techniques become apparent only in longer documents. Here we develop DTDs for the two person-description formats in the code examples An XML document describing a person with attributes and A narrative XML document using attributes on the page “Attributes”.

DTDs for record-like documents

Record-like DTDs are straightforward: many sequences, fewer choices and almost no mixed content. The next example is internal so this small document and its DTD appear together for clarity, but the DTD could easily be placed in a separate file.

<?xml version="1.0"?>
<!DOCTYPE person [
   <!ELEMENT person (name+, beruf*)>
   <!ELEMENT name EMPTY>
   <!ATTLIST name vorname CDATA #REQUIRED
                  nachname CDATA #REQUIRED>
   <!-- Die Attribute vorname und nachname müssen vorhanden sein, sie können aber leer sein. Zum Beispiel <name vorname="Cher" nachname=""> -->
   <!ELEMENT beruf EMPTY>
   <!ATTLIST beruf wert CDATA #REQUIRED>
]>
<person>
   <name vorname="Alan" nachname="Turing"/>
   <beruf wert="Informatiker"/>
   <beruf wert="Mathematiker"/>
   <beruf wert="Kryptograph"/>
</person>

Code example: A DTD describing people

The DTD lies entirely in the internal subset. First the declaration person-ELEMENT says every person must have one or more children name followed by zero or more children beruf. This allows name changes and aliases, assuming everyone has at least one name but need not have a profession.

All elements name must precede all elements beruf. This is less flexible than ideal: names need not logically come first. But arbitrary order makes requiring at least one element name difficult. One of the weaknesses of DTDs is imposing order when only element counts should matter.

Both elements name and beruf are empty and easy to declare. Attributes are more complicated: all three have unrestricted forms and are therefore of type CDATA. All are required. Comments suggest handling edge cases such as celebrities without surnames. Comments are important for explaining otherwise puzzling DTDs.

DTDs for narrative documents

Narrative DTDs are freer and use more mixed content than database-like DTDs. They are often written bottom-up, from smallest elements to largest, and use parameter entities to group similar content models and attribute lists.

The next example is a DTD for biographies like the code example A narrative XML document using attributes. Not every declared element appears there, as is common with narrative documents. Not every web page has an unordered list, yet the DTD for XHTML must declare the element ul for documents that use it. Some example attributes also have fixed defaults.

<!ATTLIST biographie xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">

<!ELEMENT person (vorname, nachname)>
<!-- Geburts- und Todesdaten werden in der Form jjjj/mm/tt angegeben.-->
<!ATTLIST person geboren CDATA #IMPLIED
                 gestorben CDATA #IMPLIED>

<!ELEMENT datum (monat, tag, jahr)>
<!ELEMENT monat (#PCDATA)>
<!ELEMENT tag   (#PCDATA)>
<!ELEMENT jahr  (#PCDATA)>

<!-- xlink:href muss eine URL enthalten.-->
<!ATTLIST betont xlink:type (einfach) #IMPLIED
                 xlink:href CDATA #IMPLIED>

<!ELEMENT beruf (#PCDATA)>
<!ELEMENT fußnote (#PCDATA)>

<!-- Die Quelle wird entsprechend den Konventionen für Zitate des Chicago Manual of Style angegeben.-->
<!ATTLIST fußnote quelle CDATA #REQUIRED>

<!ELEMENT vorname (#PCDATA)>
<!ELEMENT nachname (#PCDATA)>

<!ELEMENT bild EMPTY>
<!ATTLIST bild quelle CDATA #REQUIRED
               breite NMTOKEN #REQUIRED
               höhe   NMTOKEN #REQUIRED
               ALT    CDATA #IMPLIED>

<!ENTITY % top_level "( #PCDATA | bild | absatz | definition | person | beruf | betont | nachname | vorname | fußnote | datum )*">

<!ELEMENT absatz     %top_level; >
<!ELEMENT definition %top_level; >
<!ELEMENT betont     %top_level; >
<!ELEMENT biographie %top_level; >

Code example: A DTD for biographies

The root element biographie has a classic mixed-content declaration. Several elements may contain others unpredictably, so we group all possible top-level elements, the root’s direct children, in one top_level-entity reference. They can then easily become one another’s children. Adding future elements is also simpler, important because this small example probably cannot describe every biography.

  

 

 

 

back to top