DTD – Elements

An ELEMENT declaration is used to declare the elements in a DTD.

Declaring Elements:

Syntax:

<!ELEMENT element-name category>

OR

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

Empty Elements:

The category keyword EMPTY is used to declare the Empty elements.

Syntax:

<!ELEMENT element-name EMPTY>

Example:

<!ELEMENT br EMPTY>

XML example:

<br />

Elements with Parsed Character Data:

To declare the elements with only parsed character data, the #PCDATA is used inside parentheses.

Syntax:

<!ELEMENT element-name (#PCDATA)>

Example:

<!ELEMENT from (#PCDATA)>

Elements with any Contents:

For an element to contain any combination of parsable data, it should be declared with the category keyword ANY.

Syntax:

<!ELEMENT element-name ANY>

Example:

<!ELEMENT note ANY>

Elements with Children (sequences):

To declare the elements with one or more children, the name of the children elements are used inside parentheses.

Syntax:

<!ELEMENT element-name (child1)>

OR

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

Example:

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

The children must appear in the same sequence in the document as declared in a sequence separated by commas. The children must also be declared in a full declaration. The children can also have children in a full declaration.

Example: Full declaration of the “note” element:

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

Declaring Only One Occurrence of an Element:

Syntax:

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

Example:

<!ELEMENT note (message)>

Explanation:

In the above example, we are declaring that the child element “message” must occur once, and only once inside the “note” element.

Declaring Minimum One Occurrence of an Element:

Syntax:

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

Example:

<!ELEMENT note (message+)>

Explanation:

In the above example, we are using the + sign to declare that the child element “message” must occur one or more times inside the “note” element.

Declaring Zero or More Occurrences of an Element:

Syntax:

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

Example:

<!ELEMENT note (message*)>

Explanation:

In the above example, we are using the * sign to declare that the child element “message” can occur zero or more times inside the “note” element.

Declaring Zero or One Occurrence of an Element :

Syntax:

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

Example:

<!ELEMENT note (message?)>

Explanation:

In the above example, we are using the “?” sign to declare that the child element “message” can occur zero or one time inside the “note” element.

Declaring either/or Content:

Example:

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

Explanation:

In the above example, we are declaring that the “note” element must contain a “to” element, a “from” element, a “header” element, and either a “message” or a “body” element.

Declaring Mixed Content:

Example:

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

Explanation:

In the above example, we are declaring that the “note” element can contain zero or more occurrences of parsed character data, “to”, “from”, “header”, or “message” elements.

Please Share