Document Type Definition (DTD) : Well-Formed

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Document Type Definition(DTD)

 DTD is used to define the rules and attributes for using the tags in a
XML document.

 An XML document can be defined as:

Well-formed: If the XML document adheres to all the general XML rules
such as tags must be properly nested, opening and closing tags must be
balanced, and empty tags must end with '/>', then it is called as well-
formed.

 A well-formed XML document is an XML document with correct syntax.


OR

Valid: An XML document said to be valid when it is not only well-formed,


but it also conforms to available DTD that specifies which tags it uses,
what attributes those tags can contain, and which tags can occur inside
other tags, among other properties.

 If a XML document follows the rules given by the DTD then the
document is valid xml document.

 A well formed XML document can be validated against DTD or Schema.

 A DTD defines the XML document structure with a list of legal elements
and attributes.

 A DTD defines the structure of XML document like elements and their
relation, hierarchy, attributes and entities.

 DTDs check the validity of structure and vocabulary of an XML


document against the grammatical rules of the appropriate XML
language.
A DTD describes (Features)
 The elements that can appear in an XML document.
 The order in which the elements can appear.
 Optional and mandatory elements.
 Element attributes and whether they are optional or mandatory.
 Whether attributes can have default values.

The set of rules for a XML document can be specified using DTD Types as

1. Inline or Internal DTD: The structure and rules of XML document are
specified in the XML document.

 A DTD is referred to as an internal DTD if elements are declared


within the XML files.
 To reference it as internal DTD, standalone attribute in XML
declaration must be set to yes. This means the declaration works
independent of external source.

The syntax of internal DTD is:


<!DOCTYPE root-element [element-declarations]>

2. External DTD: The structure and rules of XML document are specified in
a separate file with “.dtd” extension, referenced in XML document.

 In external DTD elements are declared outside the XML file. They
are accessed by specifying the system attributes which may be
either the legal .dtd file or a valid URL.

 To reference it as external DTD, standalone attribute in the XML


declaration must be set as no. This means, declaration includes
information from the external source.
Following is the syntax for external DTD:
<!DOCTYPE root-element SYSTEM "file-name/URL">
Advantages of using DTD
1. Documentation - We can define own format for the XML files.
Looking at this document a user/developer can understand the
structure of the data.
2. Validation - It gives a way to check the validity of XML files by
checking whether the elements appear in the right order,
mandatory elements and attributes are in place, the elements and
attributes have not been inserted in an incorrect way, and so on.

Disadvantages of using DTD

1. It does not support the namespaces. Namespace is a mechanism by


which element and attribute names can be assigned to groups.
However, in a DTD namespaces have to be defined within the DTD,
which violates the purpose of using namespaces.
2. It supports only the text string data type.(does not support datatypes).
3. It is not object oriented. Hence, the concept of inheritance cannot be
applied on the DTDs.
4. Limited possibilities to express the cardinality for elements.
5. DTD will not use XML syntax, hence certain xml processors and xml
frameworks do not understand DTD.
6. DTD will not use namespaces.
DTD Components / DTD Elements
1. Elements (<!ELEMENT>)
2. Attributes (<!ATTLIST>)
3. Entities (<!ENTITY>)

1. Defining Elements and Structure (<!ELEMENT>)

 XML elements can be defined as building blocks of an XML


document. Elements can behave as a container to hold text,
elements, attributes, media objects or mix of all.
 Each XML document contains one or more elements, the
boundaries of which are either delimited by start-tags and end-
tags, or empty elements.

 A DTD element is declared with an ELEMENT declaration. When


an XML file is validated by DTD,
 Parser initially checks for the root element and then the child
elements are validated.

Syntax : <!ELEMENT elementname (content)>

1. <!ELEMENT elementname EMPTY > //Empty Element


Eg: <!ELEMENT address EMPTY>
The XML element in XML file should be <address/>
2. <!ELEMENT elementnam e (child1, child2...)> //Child Elements
Eg: <!ELEMENT PRODUCT(PID,PNAME,DESC,PRICE,QTY)>
3. <!ELEMENT elementname (#PCDATA)> //Element containing data
Eg: <!ELEMENT PNAME (#PCDATA)>
4. <!ELEMENT elementnam e (#PCDATA|child1|child2) > //Mixed type element
<!ELEMENT address (#PCDATA|home | office)*>

The Qualifiers for DTD definition are


Qualifier Meaning
? Optional , Zero or One occurrence
<!ELEMENT address name?>
* Zero or more Occurrences
<!ELEMENT address name ∗ >
+ one or more Occurrences
<!ELEMENT address name + >
, sequence of child elements separated by comma
<!ELEMENT address (name, company, phone)>
| It allows making choices in the child element.
<!ELEMENT address (home | office)>
<!ELEMENT PRODUCTS (PRODUCT+)> //root element
<!ELEMENT PRODUCT(PID,PNAME,DESC,PRICE,QTY)> //nested elements
<!ELEMENT PID(#PCDATA)> //element containing data
<!ELEMENT PNAME(#PCDATA)>
<!ELEMENT DESC(#PCDATA)>
<!ELEMENT PRICE(#PCDATA)>
<!ELEMENT QTY(#PCDATA)>

2. Defining DTD Attributes (<!ATTLIST>)

o Attribute gives more information about an element or more


precisely it defines a property of an element.
o An XML attribute is always in the form of a name-value pair.
o An element can have any number of unique attributes.
o We declare a list of allowable attributes for each element ,
o All attributes are defined using ATTLIST declaration.

Basic syntax of DTD attributes declaration is:

<!ATTLIST element-name attribute-name attribute-type attribute-value>


We want specify that an attribute is required.
Eg: <!ATTLIST name id CDATA #REQUIRED>

<!ATTLIST element-name attribute-name attribute-type "default-value">


Eg: <!ATTLIST name id CDATA "0">

<!ATTLIST element-name attribute-name attribute-type #FIXED "value" >


#FIXED keyword followed by the fixed value is used when you want to
specify that the attribute value is constant and cannot be changed. A
common use of fixed attributes is specifying version numbers.
Eg:
<!ATTLIST company name #FIXED "INFOSYS">

<!ATTLIST element-name attribute-name attribute-type #IMPLIED>


If the attribute you are declaring has no default value, has no fixed
value, and is not required, then we must declare that the attribute as
implied. Keyword #IMPLIED is used to specify an attribute as implied.
Eg: <!ATTLIST name id CDATA #IMPLIED>
PCDATA -> The PCDATA text is parsed by The XML parser.
(Parsed Character Data)
CDATA -> The text inside CDATA is ignored by the XML Parser. Unparsed
character data. (A text string).
# -> indicates the string followed by it is a special keyword not xml
element name.
enumeration -> Set of Enumerated values.

3. Defining DTD Entities (!ENTITY )


 Entities are used to substitute a value for an XML element.
 Entities are a mechanism to define replacement values
 XML supports User defined entities or Predefined entities (&lt;).
 Entities can be can be declared internally or externally

Internal Entity
If an entity is declared within a DTD it is called as internal entity.

Following is the syntax for internal entity declaration:


<!ENTITY entity_name "entity_value">

Eg: <!ENTITY phone_no "(011) 123-4567">

<address>
&phone_no;
</address>

External Entity
If an entity is declared outside a DTD it is called as external entity. You
can refer to an external Entity by either using system identifiers or public
identifiers.

Following is the syntax for External Entity declaration:


<!ENTITY name SYSTEM "URI/URL">

<!DOCTYPE books SYSTEM “books.dtd” [


<!ENTITY copyright SYSTEM “copy.xml”> //external
<!ENTITY call-us “123456789”> ] > //internal0
Inline or Internal DTD Examples
<!—Employees Info Using Inline or Internal DTD 
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE emps [
<!ELEMENT emps (emp+) >
<!ELEMENT emp (id,name,sal,dept,company)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT sal (#PCDATA)>
<!ELEMENT dept (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ATTLIST dept grade CDATA #REQUIRED
block CDATA "A">
<!ATTLIST id prefix CDATA #IMPLIED>
<!ENTITY address "Infosys Technoligies, Gachibowli, Hyderabad">
]>

<emps>
<emp>
<id prefix="INF">101</id>
<name>Ramesh</name>
<sal>85900</sal>
<dept grade="B"> Developer</dept>
<company>&lt;&address;&gt;</company>
</emp>
<emp>
<id>121</id>
<name>Suresh</name>
<sal>65900</sal>
<dept grade="C"> Tester</dept>
<company>&quot;&address;&quot;</company>
</emp>
<emp>
<id>131</id>
<name>Ganesh</name>
<sal>185900</sal>
<dept grade="A">Designer</dept>
<company>&apos;&address;&apos;</company>
</emp>
</emps>
<!—Products Info Using Inline or Internal DTD 
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE products [
<!ELEMENT products (product+)>
<!ELEMENT product (pid,pname,desc,price,qty)>
<!ELEMENT pid (#PCDATA)>
<!ELEMENT pname (#PCDATA)>
<!ELEMENT desc (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT qty (#PCDATA)>
]>

<products>
<product>
<pid> LG102 </pid>
<pname> LG ULTRA HD 50 </pname>
<desc> High Definition </desc>
<price> 119000 </price>
<qty> 12 </qty>
</product>

<product>
<pid> SS110 </pid>
<pname> SAMSUNG CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 139000 </price>
<qty> 8 </qty>
</product>

<product>
<pid> MI222 </pid>
<pname> MI CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 69000 </price>
<qty> 14 </qty>
</product>

</products>
External DTD Examples
<!—Employees Info Using External DTD 
emp.dtd
<!ELEMENT emps (emp+) >
<!ELEMENT emp (id,name,sal,dept,company)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT sal (#PCDATA)>
<!ELEMENT dept (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ATTLIST dept grade CDATA #REQUIRED
block CDATA "A">
<!ATTLIST id prefix CDATA #IMPLIED>
<!ENTITY address "Infosys Technoligies, Gachibowli, Hyderabad">
empexter.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE emps SYSTEM "emp.dtd" >
<emps>
<emp>
<id prefix="INF">101</id>
<name>Ramesh</name>
<sal>85900</sal>
<dept grade="B"> Developer</dept>
<company>&lt;&address;&gt;</company>
</emp>
<emp>
<id>121</id>
<name>Suresh</name>
<sal>65900</sal>
<dept grade="C"> Tester</dept>
<company>&quot;&address;&quot;</company>
</emp>
<emp>
<id>131</id>
<name>Ganesh</name>
<sal>185900</sal>
<dept grade="A">Designer</dept>
<company>&apos;&address;&apos;</company>
</emp>
</emps>
<!—Products Info Using External DTD 
product.dtd
<!ELEMENT products (product+)>
<!ELEMENT product (pid,pname,desc,price,qty)>
<!ELEMENT pid (#PCDATA)>
<!ELEMENT pname (#PCDATA)>
<!ELEMENT desc (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT qty (#PCDATA)>
prodexter.xml

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>


<!DOCTYPE products SYSTEM "product.dtd" >
<products>
<product>
<pid> LG102 </pid>
<pname> LG ULTRA HD 50 </pname>
<desc> High Definition </desc>
<price> 119000 </price>
<qty> 12 </qty>
</product>

<product>
<pid> SS110 </pid>
<pname> SAMSUNG CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 139000 </price>
<qty> 8 </qty>
</product>

<product>
<pid> MI222 </pid>
<pname> MI CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 69000 </price>
<qty> 14 </qty>
</product>

</products>
<!—MotorBikes Info Using internal DTD 
motorbike -> make,model,year,color,engine,chasis-num,accessories.
engine -> eng-no,cylinders,fuel-type.
accessories attributes -> disk-brake => “yes or no” , auto-start =>”yes or no”.
use appropriate entities.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>


<!DOCTYPE catalog [
<!ELEMENT catalog (motorbike*)>
<!ELEMENT motorbike (make,model,year,color,engine,chasisnum,accessories)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT color (#PCDATA)>
<!ELEMENT engine (eng-no,cylinders,fueltype)>
<!ELEMENT eng-no (#PCDATA)>
<!ELEMENT cylinders (#PCDATA)>
<!ELEMENT fueltype (#PCDATA)>
<!ELEMENT chasisnum (#PCDATA)>
<!ELEMENT accessories (#PCDATA)>
<!ATTLIST accessories diskbrake (yes|no) #REQUIRED>
<!ATTLIST accessories autostart (yes|no) #REQUIRED>
<!ENTITY mirror "TwoSides">
]>

<catalog>
<motorbike>
<make> HeroHonda</make>
<model>PassionPro</model>
<year> 2019</year>
<color>Black</color>
<engine>
<eng-no> 2345EF</eng-no>
<cylinders>2 </cylinders>
<fueltype> Petrol</fueltype>
</engine>
<chasisnum>1122</chasisnum>
<accessories diskbrake="yes" autostart="no"> ALL Reuired , &mirror;</accessories>
</motorbike>

<motorbike>
<make> Honda Active</make>
<model>5G</model>
<year> 2018</year>
<color>RED</color>
<engine>
<eng-no> 12345EF</eng-no>
<cylinders> 4 </cylinders>
<fueltype> Petrol</fueltype>
</engine>
<chasisnum>110022</chasisnum>
<accessories diskbrake="yes" autostart="yes"> ALL Reuired , &mirror;</accessories>
</motorbike>
</catalog>

You might also like