Open In App

DTD Elements

Last Updated : 17 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In XML, elements are the fundamental building blocks. The container can hold text, attributes, or even media objects. The parse first checks the validity of the root element and further that of the child element whenever an XML file is validated

Syntax:

<!ELEMENT element_name (element_content)>   or <!ELEMENT element-name category>
  • ELEMENT declaration is to tell the parser that we are about to declare an element.
  • Element name: It shows the name of the root element, also called a generic identifier
  • Content: It describes the type of content it can hold which can be specific rules, any data, or even another element.

If any rule is specified, it can be either ANY or EMPTY and if any data or other element is specified, it is surrounded by parentheses.

1. Empty Elements: Empty elements are declared with the keyword EMPTY in the category. The empty element doesn’t have a closing tag and doesn’t have any content.

Syntax:

<!ELEMENT element-name EMPTY>

Example 1:

<!ELEMENT header EMPTY>

The above declaration in DTD file defines the given element in XML file:

<header/>

Example 2:

<!ELEMENT br EMPTY>

In XML file :

<br />

2. Elements with any Contents: Elements declared with the category “ANY” may contain any kind of parsable data. It removes any syntax checking. ANY model is used when the data of element doesn’t matter or when the contents of the element is not decided. Also the elements can be used in any order and any number of times.

Syntax:

<!ELEMENT element-name ANY>

Example:

<!ELEMENT note ANY>

In XML file:

<note>
 Sample text
</note>

3. Elements with children: When an element contains any other element, it must be specified as a child element in the parenthesis.

<!ELEMENT element_name (child_element_name)>

Example:

<!ELEMENT city(street)>

In XML:

<city>
  <street></street>
</city>

4. Elements with Multiple Children: When multiple elements are added together, the sequence is specified by a comma separated list. XML file must contain the tags in the same order as mentioned in the list.

Syntax:

<!ELEMENT element_name (child_element1, child_element2, ...)>

Example:

<!ELEMENT address(city, street)>

Above line in DTD allows the address element to contain one instance of the city element and one instance of the street element in XML document:

In XML:

 <address>
    <city></city>
    <street></street>
  </address>

5. Single occurrence of element: The declaration specifies that child element should occur only once and within the parent element

Syntax:

 <!ELEMENT element-name (child_element)>

Example:

<!ELEMENT note (message)>

Child element “message” should be declared only once and also inside the parent element “note”

6. Minimum One Occurrence of an Element: The declaration specifies that child element should occur minimum one time within the parent element. It is followed by “+” sign.

Syntax:

<!ELEMENT element-name (child_element+ )>

Example:

<!ELEMENT note (message+ )>

Child element “message” may occur one or more times inside the parent element “note”. “+ ” sign indicates one or more occurrence.

7. Zero or More Occurrences of an Element: The declaration specifies that child element occurs zero or more times within the parent element. It should be followed by “*” sign.

Syntax:

 <!ELEMENT element-name (child_element* )>

Example:

<!ELEMENT note (message* )>

Child element “message” occur zero or more times inside the parent element “note”. “*” sign indicates zero or more occurrence.

8. Zero or One Occurrences of an Element: The declaration specifies that child element occurs zero or one time within the parent element. It should be followed by “?” sign.

Syntax:

<!ELEMENT element-name (child_element? )>

Example:

<!ELEMENT note (message? )>

Child element “message” occurs zero or one time inside the parent element “note“. “?” sign indicates zero or more occurrence.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads