DTD Elements : XML



In the last chapter we have studied the DTD Basics and different building blocks of the DTD and XML document. In this chapter we will be learning DTD elements.

DTD Elements :

DTD elements are declared with an ELEMENT declaration.

<!ELEMENT element-name keyword>
OR
<!ELEMENT element-name (element-content)>

Different ways of using DTD Element :

We can use DTD element like below syntax –

DTD ElementUsage
Empty ElementsUsing EMPTY keyword
Elements with Parsed Character DataUsing #PCDATA
Elements with any ContentsUsing ANY Keyword
Elements with childresUsing list wrapped inside ()
Element with Only One OccurrenceUsing element name only
Element with Minimum One OccurrenceUsing + Operator
Element with Zero or More OccurrencesUsing * Operator
Element with Zero or One OccurrencesUsing ? Operator
Element with either orUsing | Operator

Empty Elements :

EMPTY Keyword is used to specify Empty elements. Please look at following syntax –

<!ELEMENT element-name EMPTY>

Example :

DTD ExampleXML Example
<!ELEMENT br EMPTY>
<!ELEMENT hr EMPTY>
<br />
<hr />

Elements with Parsed Character Data

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

<!ELEMENT element-name (#PCDATA)>

Example:

<!ELEMENT author (#PCDATA)>

Elements with any Contents

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

<!ELEMENT element-name ANY>

Example:

<!ELEMENT Book ANY>

Elements with Children (sequences)

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

<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>

Example:

<!ELEMENT Book (name,author,type,pages)>

Children nodes are declared in a sequence separated by commas. If they are declared like this then the children must appear in the same sequence in the document. The full declaration of the “Book” element is :

<!ELEMENT Book (name,author,type,pages)>
<!ELEMENT name     (#PCDATA)>
<!ELEMENT author   (#PCDATA)>
<!ELEMENT type     (#PCDATA)>
<!ELEMENT pages    (#PCDATA)>

Declaring Only One Occurrence of an Element

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

Example:

<!ELEMENT Book (author)>

The example above declares that the child element “author” must occur once, and only once inside the “Book” element.

Declaring Minimum One Occurrence of an Element

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

Example:

<!ELEMENT Book (author+)>

The + sign in the example above declares that the child element “author” must occur
one or more times inside the “Book” element.


Declaring Zero or More Occurrences of an Element

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

Example:

<!ELEMENT Book (author*)>

The * sign in the example above declares that the child element “author” can occur zero or more times inside the “Book” element.


Declaring Zero or One Occurrences of an Element 

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

Example

<!ELEMENT Book (author?)>

The ? sign in the example above declares that the child element “author” can occur zero or one time inside the “Book” element.


Declaring either/or Content

<!ELEMENT Book (name,author,type,(price|pages))>

Declaration says that –

  1. These elements are must – “name”,”author”,”type”
  2. One element from “pages” and “price” must be present.

Declaring Mixed Content

<!ELEMENT Book (#PCDATA|name|author|type|pages)*>

The example above declares that the “Book” element can contain zero or more occurrences of parsed character data, “name”, “author”, “type”, or “pages” elements.