The elements of the XML documents can be defined in DTD using ELEMENT keyword. The elementName given below should conform to the naming syntax of XML elements.

Element declarations

Syntax

Description

<!ELEMENT elementName category>

General syntax for declaring elements, where category represents the type of element.

<!ELEMENT elementName EMPTY>

The category is EMPTY indicating element cannot have content.

 

Example:

<!ELEMENT br EMPTY>

<!ELEMENT elementName (#PCDATA)>

The content of the element is only parsed character data.

 

Example:

<!ELEMENT mail (#PCDATA)>

<!ELEMENT elementName ANY>

The content of the element can be any type of parsable data.

 

Example:

<!ELEMENT mail ANY>

<!ELEMENT elementName (child1, child2, …)>

The elements with one or more children are declared in this syntax with the name and order of the children appearing in same sequence as the document.

 

Example:

<!ELEMENT message (to,from,subject,body)>

<!ELEM ENT to (#PCDATA)>

<!ELEMENT from (#PCDATA)>

<!ELEMENT subject (#PCDATA)>

<!ELEMENT body (#PCDATA)>

Cardinality operators

The cardinal operators are: none, ?, +, *. They determine the number of times element appears within content model.  They can be applied to single element, group or subgroup of elements as below:

  • None: With none of the above operators the element should appear only once. This default operator. (EX: <!ELEMENT message (developer, manager)>)
  • ?: This has the value 0 or 1. This makes the element optional. (EX DTD: <!ELEMENT message (developer, manager?), EX XML: <message><developer>Tony</developer><message>)

  • + - This indicates the element that can appear one or more times. (EX DTD: <!ELEMENT message (developer, manager)+>, EX XML: <message><developer>Tony</developer><developer>Mark</developer><manager>David</manager><message>)

  • * -This indicates the element may not appear or can appear from one to many times. (EX DTD: <!ELEMENT message (developer*, manager)>, EX XML: <message> <manager>David</manager><message>

     

  • ›› go to examples ››