Sunday 24 November 2013

XML & DTD



Document Type Definition


The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document. A DTD defines the document structure with a list of legal elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference.
Internal DTD Declaration:
If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax:
<! DOCTYPE root-element [element-declarations]>
XML document with an internal DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weather[
<!ELEMENT weather (day1)>
<!ELEMENT day1 (location,date,temp)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT temp (#PCDATA)>
<!ENTITY GM "Good Morning">
<!ENTITY GN "Good Night">
]>

<weather>
<day1 >
<location>&GM; Sanfransico </location>
<date>20/04/2013</date>
<temp>20'c</temp>
</day1>
</weather>

The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of this document is note
!ELEMENT note defines that the note element contains four elements: "to, from, heading, body" 
!ELEMENT to defines the to element to be of type "#PCDATA"
!ELEMENT from defines the from element to be of type "#PCDATA"
!ELEMENT heading defines the heading element to be of type "#PCDATA"
!ELEMENT body defines the body element to be of type "#PCDATA"

External DTD Declaration
If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the syntax:
<! DOCTYPE root-element SYSTEM "filename">
whether.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weather SYSTEM "weather.dtd">
<weather>
<day1>
<location>&GM;Vita</location>
<date>20/04/2013</date>
<temp>20'c</temp>
</day1>
</weather>

weather.dtd
<!ELEMENT weather (day1)>
<!ELEMENT day1 (location,date,temp)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT temp (#PCDATA)>
<!ENTITY GM "Good Morning">
<!ENTITY GN "Good Night">

Why to Use a DTD?
With a DTD, each of your XML files can carry a description of its own format. With a DTD, independent groups of people can agree to use a standard DTD for interchanging data. Your application can use a standard DTD to verify that the data you receive from the outside world is valid. You can also use a DTD to verify your own data.
The Building Blocks of XML Documents
All XML documents are made up by the following building blocks:
Elements, Attributes, Entities, PCDATA, and CDATA .

Elements
Elements are the main building blocks of both XML and HTML documents.

Attributes
Attributes provide extra information about elements. Attributes are always placed inside the opening tag of an element. Attributes always come in name/value pairs.
Entities .Some characters have a special meaning in XML, like the less than sign (<) that defines the start of an XML tag.

PCDATA
PCDATA means parsed character data.

CDATA
CDATA means character data.
 

ShareThis

Related Posts Plugin for WordPress, Blogger...