DTD – Elements

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

Declaring Elements:

Syntax:


OR


Empty Elements:

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

Syntax:


Example:


XML example:


Elements with Parsed Character Data:

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

Syntax:


Example:


Elements with any Contents:

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

Syntax:


Example:


Elements with Children (sequences):

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

Syntax:


OR


Example:


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:






Declaring Only One Occurrence of an Element:

Syntax:


Example:


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:


Example:


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:


Example:


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:


Example:


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:


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:


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.