DTD - Element

In a DTD, elements are declared through element declarations.

Declare an element

In a DTD, XML elements are declared through element declarations. Element declarations use the following syntax:

<!ELEMENT element_name category>

Or

<!ELEMENT element_name (element_content)>

Empty elements

Empty elements are declared by the category keyword EMPTY:

<!ELEMENT element_name EMPTY>

Example:

<!ELEMENT br EMPTY>

XML example:

<br />

Elements with only PCDATA

Elements with only PCDATA are declared by #PCDATA in parentheses:

<!ELEMENT element_name (#PCDATA)>

Example:

<!ELEMENT from (#PCDATA)>

An element with any content

Elements declared by the category keyword ANY can contain any combination of parseable data:

<!ELEMENT element_name ANY>

Example:

<!ELEMENT note ANY>

An element with child elements (sequence)

An element with one or more child elements is declared by the child element names in parentheses:

<!ELEMENT element_name (child_element_name 1)>

Or

<!ELEMENT element_name (child_element_name 1,child_element_name 2,...)>

Example:

<!ELEMENT note (to,from,heading,body)>

When child elements are declared in a sequence separated by commas, these child elements must appear in the same order in the document. In a complete declaration, child elements must also be declared, and child elements can also have child elements. The complete declaration of the 'note' element is:

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Declare an element that occurs only once

<!ELEMENT element_name (child_element_name)>

Example:

<!ELEMENT note (message)>

The above example declares that: the 'message' child element must occur once and must only appear within the 'note' element once.

Declare the minimum occurrence of an element


Example:

<!ELEMENT element_name (child_element_name+)>

<!ELEMENT note (message+)>

The plus sign in the above example declares that the "message" child element must appear at least once within the "note" element.

Declare an element that appears zero or multiple times

Example:

<!ELEMENT element_name (child_element_name*)>

The asterisk in the above example declares that the child element "message" can appear zero or multiple times within the "note" element.

Declare an element that appears zero or once

<!ELEMENT element_name (child_element_name?)>

Example:

<!ELEMENT note (message?)>

The question mark in the above example declares that the child element "message" can appear zero or once within the "note" element.

Declare "non.../either..." type content

Example:

<!ELEMENT note (to,from,header,(message|body))>

The above example declares that the "note" element must contain the "to" element, "from" element, "header" element, and non-"message" element, which is the "body" element.

Declare mixed content

Example:

<!ELEMENT note (#PCDATA|to|from|header|message)*>

The above example declares that the "note" element can contain PCDATA, "to", "from", "header", or "message" that can appear zero or multiple times.