DTD - element


In a DTD, elements are declared with an element declaration.


Declare an element

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

<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>


empty element

Empty elements are declared with 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 with #PCDATA inside parentheses:

<!ELEMENT element-name (#PCDATA)>

Example:

<!ELEMENT from (#PCDATA)>


Elements with any content

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

<!ELEMENT element-name ANY>

Example:

<!ELEMENT note ANY>


Elements with child elements (sequence)

Elements with one or more child elements are declared with the names of the child elements inside parentheses:

<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>

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 full declaration, the child elements must also be declared, and the children may also have children. The full declaration of the "note" element is:

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


Elements declared to occur exactly once

<!ELEMENT element-name (child-name)>

Example:

<!ELEMENT note (message)>

The example above declares that the message child element must appear once, and must appear only once inside the "note" element.


Elements declared to occur at least once

<!ELEMENT element-name (child-name+)>

Example:

<!ELEMENT note (message+)>

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


Elements declared to occur zero or more times

<!ELEMENT element-name (child-name*)>

Example:

<!ELEMENT note (message*)>

The asterisk (*) in the example above declares that the child element message may appear zero or more times inside the "note" element.


Elements declared to occur zero or once

<!ELEMENT element-name (child-name?)>

Example:

<!ELEMENT note (message?)>

The question mark (?) in the example above declares that the child element message may appear zero or one time inside the "note" element.


Declaring "either/or" type content.

Example:

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

The example above declares that the "note" element must contain the "to" element, the "from" element, the "header" element, and the "body" element rather than the "message" element.


Declared mixed content

Example:

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

The example above declares that the "note" element may contain zero or more occurrences of PCDATA, "to", "from", "header", or "message".


other extensions