DTD-Elements

In a DTD, elements are declared with an ELEMENT declaration.

DTD-Elements

DTD-Elements

     

In a DTD, elements are declared with an ELEMENT declaration.

Declaring Elements : syntax

In a DTD, XML elements are declared  with the following syntax:

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

Empty Elements

Empty elements are declared with the  keyword EMPTY inside the parentheses.

<!ELEMENT element-name EMPTY>
DTD Example: <!ELEMENT br EMPTY>
In XML document: 
  <br />

Elements with Parsed Character Data

Elements with only parsed character data are declared with  #PCDATA inside the parentheses:

<!ELEMENT element-name (#PCDATA)>

DTD Example :

<!ELEMENT To (#PCDATA)>
<!ELEMENT From (#PCDATA)>

Elements with Data

Elements declared with the keyword ANY, can contain any combination of parsable data:

<!ELEMENT element-name ANY>
DTD Example:
<!ELEMENT E-mail (To,From,Subject,Body)>
<!ELEMENT To (#PCDATA)>
<!ELEMENT From (#PCDATA)>

Elements with Children (sequences)

Elements with one or more children are declared with the name of the children elements inside the parentheses as :

<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>
DTD Example:
<!ELEMENT E-mail (To,From,Subject,Body)>

When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared.Children can  have children. The full declaration of the "E-mail" element is:

<!ELEMENT E-mail (To,From,Subject,Body)>
<!ELEMENT To (#PCDATA)>
<!ELEMENT From (#PCDATA)>
<!ELEMENT Subject (#PCDATA)>
<!ELEMENT Body (#PCDATA)>

Declaring Only One Occurrence of an Element 

<!ELEMENT element-name (child-name)>
DTD Example:
<!ELEMENT  color  (Fill-Red)>

The example above declares that the child element "Fill-Red" must occur once, and only once inside the "color" element.

Declaring Minimum One Occurrence of an Element

<!ELEMENT element-name (child-name+)>
DTD Example:
<!ELEMENT color  (Fill-Red+)>

The '+' sign in the example above declares that the child element "Fill-Red" must occur one or more times inside the "color" element.

Declaring Zero or More Occurrences of an Element 

<!ELEMENT element-name (child-name*)>
DTD Example:
<!ELEMENT color (Fill-Red*)>

The '*' sign in the example above declares that the child element  "Fill-Red" can occur zero or more times inside the "color" element.

Declaring Zero or One Occurrence of an Element 

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

DTD Example:

<!ELEMENT color (Fill-Red?)>

The '?' sign in the example above declares that the child element "Fill-Red"  can occur zero or one time inside the "color" element.

Declaring either/or Content

DTD Example:

<!ELEMENT E-mail (To,From,Subject,(Message|Body))>

The example above declares that the "E-mail" element must contain a "To" element, a "From" element, a "Subject" element, and either a "Message" or a "Body" element.

Declaring Mixed Content

DTD Example:

<!ELEMENT E-mail(#PCDATA|To|From|Subject|Body)*>

The example above declares that the "E-mail" element can contain zero or more occurrences of a parsed character data, "To", "From", "Subject", or "Body" elements.