DTD - Elements

In a DTD, elements are declared using element declarations.

Declare an element

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

!ELEMENT elementName category

Or

!ELEMENT elementName (elementContent)

Empty elements

Empty elements are declared with the category keyword EMPTY:

!ELEMENT elementName EMPTY

Example:

!ELEMENT br EMPTY

XML example:

<br />

Elements that only contain PCDATA

Elements that only contain PCDATA are declared using #PCDATA within parentheses:

!ELEMENT elementName (#PCDATA)

Example:

!ELEMENT from (#PCDATA)

Elements with any content

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

!ELEMENT elementName 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 元素名称 (子元素名称 1)>

Or

<!ELEMENT 元素名称 (子元素名称 1,子元素名称 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)>

Declares the element that appears only once

<!ELEMENT 元素名称 (子元素名称)>

Example:

<!ELEMENT note (message)>

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

Declares the element that appears at least once

<!ELEMENT 元素名称 (子元素名称+)>

Example:

<!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.

Declares the element that appears zero or more times

<!ELEMENT 元素名称 (子元素名称*)>

Example:

<!ELEMENT note (message*)>

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

Declares the element that appears zero or one time

<!ELEMENT 元素名称 (子元素名称?)>

Example:

<!ELEMENT note (message?)>

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

Declares the content of the type "non.../either..."

Example:

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

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

Declares mixed content type

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 more times.