Introduction to XML Document Type Definition by Examples

The purpose of a DTD (Document Type Definition) is to define a set of rules describing the structure of an XML document with a list of legal elements and attributes.

The general structure of a DTD as as follows :


[info]The ROOT_ELEMENT is the root element of the XML document[/info]

For the DTD, the three main building blocks are as follows:

  1. <!ELEMENT ….
  2. <!ATTLIST ….
  3. <!ENTITY ….

[info]Click here to Practice and Learn Document Type Definition to validate XML online[/info]

For the sake of simplicity, DTD syntax is illustrated using examples in the following table:

DTD Explanation and Examples
<!ELEMENT element-name EMPTY>
Example : <!ELEMENT br EMPTY>
Empty elements are declared with the category keyword EMPTY:
<br />
<!ELEMENT element-name (#PCDATA)>
Example : <!ELEMENT title (#PCDATA)>
Elements with only parsed character data are declared with #PCDATA inside parentheses:
<title>Java Programming</title>
<!ELEMENT element-name (child1,child2)>




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


   Feature Extraction
   Mark Nixon

<!ELEMENT element-name (child-name+)>
Example : <!ELEMENT Student (country+)>
The + sign in the example above declares that the child element “country” must occur ONE or More times inside the “Student” element.
<!ELEMENT element-name (child-name?)>
Example : <!ELEMENT Student (Tutor?)>
The ? sign in the example above declares that the child element “Tutor” must occur ZERO or ONE times inside the “Student” element.
<!ELEMENT element-name (child-name*)>
Example : <!ELEMENT Student (module*)>
The * sign in the example above declares that the child element “module” must occur ZERO or More times inside the “Student” element.
<!ELEMENT element-name (option1|option2)>
Example : <!ELEMENT Student (Tutor|Supervisor)>
The example declares that the “Student” element must contain either a “Tutor” or a “Supervisor” element.
<!ELEMENT element-name (option1|option2)>
Example : <!ELEMENT Student (Tutor|Supervisor)>
The example declares that the “Student” element must contain either a “Tutor” or a “Supervisor” element.
<!ATTLIST element-name attr-name attr-type attr-value>
Example : <!ATTLIST book language CDATA “english”>
The element book has an attribute named language of type CDATA with a default value “english”
The possible values for the attr-values are listed as:
Example : <!ATTLIST country code CDATA #REQUIRED> The element country has an attribute named code of type CDATA with a required value
Example : <!ATTLIST city mayor CDATA #IMPLIED> The element city has an attribute named mayor of type CDATA but is not required.
Example : <!ATTLIST car wheels CDATA #FIXED “4”> The element car has an attribute named wheels of type CDATA with a fixed value of “4”
Example : <!ATTLIST Bus color (white|blue|green) “white”> The element Bushas an attribute named color that is within the set of choices (white OR blue OR green). The default value is white

Example : <!ENTITY copyright “Copyright by Imed Bouchrika”>
Entities are variables used to define shortcuts to standard text or special characters.
Example <website>&copyright<website>

DTD document can be integrated within the XML document as shown in the example below:







]>

Hou
Asma
 

[info]We use the statement standalone=”yes” within the XML header[/info]

Or instead, the DTD can created on a separate file, for example mydata.dtd and called as shown below from the XML document




Hou
Asma
 

Questions

1 CDATA is a character data for attributes? is there any other data types ?

2 We can store data either within an element or attribue, which one is better to use ?

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *