XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / Element declarations

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

Every element used in a valid document must be declared in its DTD with an element declaration. Element declarations have this basic form:

 <!ELEMENT name inhaltsspezifizierung> 

Any legal XML name can name an element. The content specification defines which children it may or must have and in what order. It can become complex: three children of one type, two followed by one of another type, or elements chosen from seven types mixed with text.

#PCDATA

The simplest specification permits parsed character data only, without child elements. Use the keyword #PCDATA in parentheses. This declaration, for example, lets the element telefonnummer contain text but no elements:

 <!ELEMENT telefonnummer (#PCDATA)> 

Such an element may also contain character references and CDATA sections, treated as plain text during parsing, plus comments and processing instructions, irrelevant to validation. It may also contain entity references, but only if they resolve to plain text without child elements.

Child elements

Another simple specification requires exactly one child of a particular type. Put its name in parentheses. This declaration requires the element fax to contain exactly one telefonnummer element:

 <!ELEMENT fax (telefonnummer)> 

A fax element may contain only telefonnummer, exactly once.

Sequences

Specifications listing one child are uncommon. Most elements contain text or potentially several children. The simplest way to specify several children separates names with commas, a sequence, requiring the listed elements in that order. This declaration says a name element must contain exactly one vorname child followed by exactly one nachname child:

 <!ELEMENT name (vorname, nachname)> 

Under this declaration, this name element is valid:

<name>
   <vorname>Madonna</vorname>
   <nachname>Ciconne</nachname>
</name>

This one is invalid because it reverses the order:

<name>
   <nachname>Ciconne</nachname>
   <vorname>Madonna</vorname>
</name>

This is invalid because the element nachname is missing:

<name>
   <vorname>Madonna</vorname>
</name>

This is invalid because there is an extra zweiter_vorname element:

<name>
   <vorname>Madonna</vorname>
   <zweiter_vorname>Louise</zweiter_vorname>
   <nachname>Ciconne</nachname>
</name>

Number of children

Instances of an element need not have identical children. Append one of three suffixes to a name to specify expected occurrences at that position:

  • ? Zero or one element is allowed.

  • * Zero or more elements are allowed.

  • + One or more elements are required.

This declaration requires a name element to contain exactly one vorname element and optionally a zweiter_vorname element and optionally a nachname element:

 <!ELEMENT name (vorname, zweiter_vorname?, nachname?)> 

These name elements are all valid:

<name>
   <vorname>Madonna</vorname>
   <nachname>Ciconne</nachname>
</name>
<name>
   <vorname>Madonna</vorname>
   <zweiter_vorname>Louise</zweiter_vorname>
   <nachname>Ciconne</nachname>
</name>
<name>
   <vorname>Madonna</vorname>
</name>

These are invalid:

<name>
   <vorname>George</vorname>
   <!-- es ist nur ein zweiter Vorname erlaubt -->
   <zweiter_vorname>Herbert</zweiter_vorname>
   <zweiter_vorname>Walker</zweiter_vorname>
   <nachname>Bush</nachname>
</name>
<name>
   <!-- der Vorname muss vor dem Nachnamen stehen -->
   <nachname>Ciconne</nachname>
   <vorname>Madonna</vorname>
</name>

Allow multiple middle names by putting an asterisk after zweiter_vorname:

 <!ELEMENT name (vorname, zweiter_vorname*, nachname?)> 

To require zweiter_vorname while allowing multiple middle names, use a plus instead:

 <!ELEMENT name (vorname, zweiter_vorname+, nachname?)> 

Choice

Sometimes different instances contain different child types. This is a choice, a list of names separated by vertical bars. This declaration lets the element methodResponse contain either a params child or a fault child:

 <!ELEMENT methodResponse (params | fault)> 

It cannot contain both at once. Each methodResponse element must have one or the other.

Choices allow unlimited alternatives. This declaration permits each ziffer element to contain exactly one child from null, eins, zwei, drei, vier, fünf, sechs, sieben, acht or neun:

<!ELEMENT ziffer (null | eins | zwei | drei | vier | fünf | sechs | sieben | acht | neun)>

Parentheses

Choices, sequences and suffixes are limited alone, but freely combined they describe almost any content model. Parenthesize a choice or sequence and append ? , * or +, or nest the group in other choices and sequences.

Suppose a kreis element must contain a zentrum element and either a radius or a durchmesser element, but not both. This declaration does it:

 <!ELEMENT kreis (zentrum, (radius | durchmesser))> 

A geometric point uses Cartesian or polar coordinates, so each center contains either an x and a y or an r and a θ. Declare two parenthesized sequences combined as a choice:

 <!ELEMENT zentrum ((x, y) | (r, θ))> 

If you do not care whether x precedes y or the reverse, or whether r precedes θ, extend the choice to cover all four possibilities:

 <!ELEMENT zentrum ((x, y) | (y, x) | (r, θ) | (θ, r) )> 

Permutations increase exponentially with sequence length, so this is impractical beyond two or three children. DTDs cannot conveniently express a requirement for n instances of A and m instances of B irrespective of order.

Parenthesized groups can also take suffixes. A polygon may specify coordinates for each vertex in order. This is a right triangle:

<polygon>
   <r>0</r> <θ>0</θ>
   <x>0</x> <y>10</y>
   <x>10</x> <y>0</y>
</polygon>

A polygon consists of three or more x-y or r-θ pairs. An x is always followed by a y, and an r by a θ. This declaration ensures that:

<!ELEMENT polygon (((x, y) | (r, θ)), ((x, y) | (r, θ)), ((x, y) | (r, θ))+)>

The plus applies to ((x, y) | (r, θ)).

Returning to names, suppose you want a first name alone, a last name alone, or first and last names with any number of middle names. This declaration does it:

<!ELEMENT name (nachname | (vorname, ( (zweiter_vorname+, nachname) | (nachname?) ))>

Mixed content

Narrative elements often contain children and nonempty unmarked text. Recall the definition element from A narrative XML document on the page “Elements, tags and character data”:

<definition>Als <begriff>Turing-Maschine</begriff> bezeichnet man einen abstrakten endlichen Automaten mit unendlichem Speicher, der als äquivalent zu jedem anderen endlichen Automaten mit beliebig großem Speicher angesehen werden kann. Das heißt, was für eine Turing-Maschine gilt, gilt für alle Turing-Maschinen, unabhängig davon, wie sie implementiert wurden.</definition>

The element definition contains nonempty text and a begriff child. This is called mixed content (mixed content). Declare mixed content like this:

 <!ELEMENT definition (#PCDATA | begriff)*> 

This lets an element definition contain parsed character data and begriff children, without fixing order or occurrence counts. It permits a definition element to have 1 begriff child, 0 begriff children or 23 begriff children.

Any number of other child names can be listed, but #PCDATA must always come first. This declaration permits an absatz element to contain any number of name, beruf, fußnote, betont and datum in any order, mixed with parsed character data:

<!ELEMENT absatz (#PCDATA | name | beruf | fußnote | betont | datum)*>

This is the only way to declare mixed content. You cannot require exactly one begriff child of the element definition alongside parsed text, or require all text after the begriff child. You cannot parenthesize mixed content as part of a larger group. You can only allow any number of listed elements in any order, plus undifferentiated parsed character data.

Empty elements

Elements with no content are called empty elements and are sometimes written with a closing />:

<bild quelle="bus.jpg" breite="152" höhe="345" alt="Alan Turing steht vor einem Bus" />

Declare them with the keyword EMPTY as content specification:

 <!ELEMENT image EMPTY> 

This requires the element image to be empty, not necessarily written as a single empty-element tag. Under that declaration, this is also a valid image element:

<image source="bus.jpg" width="152" height="345" alt="Alan Turing steht vor einem Bus"></image>

Empty elements may contain absolutely nothing, not even whitespace! This is an invalid image element:

<image source="bus.jpg" width="152" height="345" alt="Alan Turing steht vor einem Bus">
</image>

ANY

Very loose DTDs sometimes declare an element without constraining its content. Use the keyword ANY. This declaration permits a seite element to contain any content, including mixed content, children and other seite elements:

 <!ELEMENT seite ANY> 

Children actually appearing in the element seite still need their own declarations. ANY does not allow undeclared elements.

ANY can help when initially designing a DTD and structure without a clear final picture, but using ANY in finished DTDs is poor style. It occasionally appears where external DTD subsets and entities change uncontrollably, but this is rare. You really need ANY only when writing a DTD for an application such as XSLT or RDF incorporating content from arbitrary unknown XML applications.

  

 

 

 

back to top