XML DTD Elements
Elements in Document Type Definition (DTD) are defined with an ELEMENT
declaration.
Different types of elements are shown below.
How to declare DTD Elements
DTD Elements are declared in this way:
<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
Empty DTD Elements
Empty elements are declared with the category keyword EMPTY
:
<!ELEMENT element-name EMPTY>
DTD Elements with Parsed Character Data
Elements with only parsed character data are declared with #PCDATA
inside parentheses:
<!ELEMENT element-name (#PCDATA)>
DTD Elements with any Contents
Elements declared with the category keyword ANY, can contain any combination of parsable data:
<!ELEMENT element-name ANY>
DTD Elements with Children
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,...)>
When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document.
For example:
<!ELEMENT book (title,author,publisher)>
<!ELEMENT title(#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher(#PCDATA)>
Declaring Only One Occurrence of a DTD Element
In this way we declare a child-name element that occurs once and inside the element-name element
<!ELEMENT element-name (child-name)>
Declaring Minimum One Occurrence of a DTD Element
The +
sign declares that the child-name element must occur once or more times inside the element-name element.
<!ELEMENT element-name (child-name+)>
Declaring Zero or More Occurrences of a DTD Element
The *
sign declares that the child-name element can occur zero or more times inside the element-name element.
<!ELEMENT element-name (child-name*)>
Declaring Zero or One Occurrences of a DTD Element
The ?
sign declares that the child-name element can occur zero or one time inside the element-name element.
<!ELEMENT element-name (child-name?)>
Declaring either/or Content
In this example, the |
sign declares that book element can contain a title element, either an author or a #PCDATA element and a publisher element.
<!ELEMENT book (title,(author|#PCDATA),publisher)>
Declaring Mixed Content
In this example, the *
sign declares that book element can contain zero or more occurrences of #PCDATA, title, author or publisher elements.
<!ELEMENT book (#PCDATA|title|author|publisher)*>