DTD - Elements

Declaring Elements:

To declare an element in a DTD, you use the <!ELEMENT> declaration. Here's an example:

<!ELEMENT book (title, author, year)>

In this example, the book element is declared with the title, author, and year elements as its children. The order of the children elements is important.

Empty Elements:

Empty elements are declared using the EMPTY keyword. They don't have any content or children elements. Here's an example:

<!ELEMENT br EMPTY>

In this example, the br element is declared as an empty element. It represents a line break.

Elements with Parsed Character Data:

Elements that contain only text or parsed character data are declared with the #PCDATA keyword. Here's an example:

<!ELEMENT title (#PCDATA)>

In this example, the title element can contain only text or parsed character data.

Elements with any Contents:

Elements that can contain any content, including child elements and text, are declared using the ANY keyword. Here's an example:

<!ELEMENT content ANY>

In this example, the content element can have any content, including child elements and text.

Elements with Children (sequences):

Elements can have child elements declared in a specific sequence. Here's an example:

<!ELEMENT address (street, city, country)>

In this example, the address element must contain the child elements street, city, and country in that order.

Declaring Only One Occurrence of an Element:

To specify that an element can occur only once, you use the ? symbol. Here's an example:

<!ELEMENT author (#PCDATA)?>

In this example, the author element can occur at most once, and it can only contain text or parsed character data.

Declaring Minimum One Occurrence of an Element:

To specify that an element must occur at least once, you use the + symbol. Here's an example:

<!ELEMENT paragraph (#PCDATA)+>

In this example, the paragraph element must occur at least once and can contain only text or parsed character data.

Declaring Zero or More Occurrences of an Element:

To specify that an element can occur zero or more times, you use the * symbol. Here's an example:

<!ELEMENT comment (#PCDATA)*>

In this example, the comment element can occur zero or more times and can contain only text or parsed character data.

Declaring Zero or One Occurrences of an Element:

To specify that an element can occur zero or one time, you use the ? symbol. Here's an example:

<!ELEMENT optional (content?)>

In this example, the optional element can occur at most once and can contain the content element or be empty.

Declaring either/or Content:

To specify that an element can contain either one child element or another, you use the | symbol. Here's an example:

<!ELEMENT section (text | image)>

In this example, the section element can contain either the text element or the image element.

Declaring Mixed Content:

Mixed content refers to elements that can contain a mixture of text and child elements. You use the (#PCDATA) keyword along with the * symbol to declare mixed content. Here's an example:

<!ELEMENT description (#PCDATA | emphasis | link)*>

In this example, the description element can contain text, the emphasis element, or the link element in any order and any number of times.