DTD Elements In XML

The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference. XML provides an application independent way of sharing data. With a DTD, independent groups of users can agree to use a common DTD for interchanging data. Your application can use a standard DTD to verify that data that you receive from the outside world is valid. You can also use a DTD to verify your own data.
 
 
Declaring an Element
 
In the DTD, XML elements are declared with an element declaration.
 
An element declaration has the following syntax:
 
<!ELEMENT element-name (element-content)>
 
Element with data
 
<!ELEMENT element-name (#CDATA)>
or
<!ELEMENT element-name (#PCDATA)>
or
<!ELEMENT element-name (ANY)>
 
#CDATA
means the element contains character data that is not supposed to be parsed by a parser. 
 
#PCDATA
means that the element contains data that is going to be parsed by a parser. 
 
ANY  
The keyword ANY declares an element with any content.
 
If a #PCDATA section contains elements, these elements must also be declared.
 
 
example:
 
<!ELEMENT subject (#PCDATA)>
 
 
Empty element
 
Empty elements are declared with the keyword EMPTY inside the parentheses:
 
<!ELEMENT element-name (EMPTY)>
 
example:
<!ELEMENT break (EMPTY)>
 
 
Elements with sub-element (sequences)
 
Elements with one or more children are defined with the name of the children elements inside the parentheses:
 
<!ELEMENT element-name (child-element-name)>
or
<!ELEMENT element-name (child-element-name,child-element-name,…..)>
 
example:
<!ELEMENT email (sender,recipient,subject,message)>
 
In a full declaration, the children must also be declared, and the children can also have children. When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document, this means they should follow the same sequence as they are declered.
 
The full declaration of the note document will be:
 
<!ELEMENT email (sender,recipient,subject,message)>
<!ELEMENT sender (sender_name,sender_email_id)>
<!ELEMENT recipient (recipient_name,recipient_email_id)>
<!ELEMENT subject (#PCDATA)>
<!ELEMENT message (message_header,message_body_text,message_footer)>
 
 
Declaring only one occurrence of the same element
 
<!ELEMENT element-name (child-name)>
 
example:
<!ELEMENT email (message_text)>
 
The example declaration above declares that the child element message_text can only occur one time inside the email element.
 
 
Declaring minimum one occurrence of the same element
 
<!ELEMENT element-name (child-name+)>
 
example:
<!ELEMENT email (message_text+)>
 
The + sign in the example above declares that the child element message_text must occur one or more times inside the email element.
 
 
Zero or more occurrences of the same element
 
<!ELEMENT element-name (child-name*)>
 
example:
<!ELEMENT email (message_text*)>
 
The * sign in the example above declares that the child element message_text must occur one or more times inside the email element.
 
 
Zero or one occurrences of the same element
 
<!ELEMENT element-name (child-name?)>
 
example:
<!ELEMENT email (message_text?)>
 
The ? sign in the example above declares that the child element message_text can occur zero or one times inside the email element.
 
 
mixed content
 
example:
<!ELEMENT email (sender,recipient+,subject?,message*)>
 
The example above declares that the element note must contain at least one recipient child element, exactly one sender child element, zero or one subject, zero or more message, and some other parsed character data as well.