Home Xml DTD-Elements
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

DTD-Elements

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

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.

     

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 25, 2007

Ask Questions?    Discuss: DTD-Elements  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com