Element Declaration in DTD

Element declaration in DTD Specifies the name of the tag that use to build XML document. Every (General) XML element declare by following way,

<!ELEMENT element_name (inside_element)>

element_name specifies the general identifier and inside_element specifies what are content inside the element.


Elements with any Contents

Elements declared with the ANY keyword, Any keyword contain any combination of parse-able data.

<!ELEMENT element_name ANY>         <!-- Syntax-->
<!ELEMENT div ANY>                  <!-- Example-->

EMPTY keyword specifies the empty tag. Inside no any element content.


Empty Element

<!ELEMENT element_name (EMPTY)>       <!-- Syntax-->
<!ELEMENT br EMPTY>                   <!-- Example-->

EMPTY keyword specifies the empty tag. Inside no any element content.


Only Text Content Element

If your element content only text data you can declare.

<!ELEMENT element_name (#PCDATA)>

#PCDATA (parsed character data) keyword specifies parsed only character content.


Only One Child Element

If your element content only text data you can declare.

<!ELEMENT element_name (child_element)>   <!-- Syntax-->
<!ELEMENT div (p)>                        <!-- Example-->

This declaration specifies <div>...</div> only have one child <p>...</p> element.


Multiple Child Element

Child elements specifies one or more separated by comma (,) sign.

<!ELEMENT div (p,a,span,h3)>        <!-- Example-->

Multiple Child Element (with same name)

Child elements specifies one or more time along with same name. add + sign end of the child element name.

<!ELEMENT div (p+)>                <!-- Example-->

Occur any number of child element (* sign) :

Child elements specifies any number of deep child of child element. add * (asterisk) sign end of the child element name.

<!ELEMENT div (p*)>             <!-- Example-->

Optional child element(? sign) :

Child element optional if not use in XML document, you can say element_name contain empty.

<!ELEMENT element_name (child_element?)>  <!-- Syntax-->
<!ELEMENT div (p?)>             <!-- Example-->

Mixed Operator :

You can use mixed operation with together.

<!ELEMENT div (p+,a*,span?,h3)>       <!-- Example-->

OR (|) Operator :

You can also specifies the choice of multiple child element using pipe sign (|) operator.

<!ELEMENT element_name (child_element | child_element | child_element)> <!-- Syntax-->

Suppose following specifies the child elements separated by pipe sign. You can use any of the specified child element.

<!ELEMENT div (p | a | span | h3)>      <!-- Example-->

Group Elements :

You can also specifies the group of element with specific operator.

<!ELEMENT article (header,(p,a,span,)*, footer)>
<!ELEMENT article (header,(p,a,span)+, footer)>
<!ELEMENT article (header,(p,a,span)?, footer)>
<!ELEMENT article ( header, ( p | a | span )*, footer ) >