Schema vs. DTD IN XML

Schemas make it possible to provide the full “legal” structure of an XML document and to specify type characteristics for data contained within the document. XML based schemas are very quickly emerging as one of the most important facets of the XML revolution. A schema is the set of rules that defines how a document is put together. Schema allows the user to check the validity of the XML document. XML Schema can be used to validate sequence, occurrence and other such constraint for an element.
Difference between Schema and DTD
A DTD is:
DTD’s old and most widely used grammar for XML documents, but they were originally developed for SGML. The XML Document Type Declaration(DTD) contains or points to markup declarations that provide a grammar for a class of documents. This grammar is known as a document type definition or DTD.
The DTD can point to an external subset containing markup declarations, or can contain the markup declarations directly in an internal subset, or can even do both.  
A Schema is:
XML Schemas express shared vocabularies and allow machines to carry out rules made by people. They provide a means for defining the structure, content and semantics of XML documents.
In summary, schemas are a richer and more powerful of describing information than what is possible with DTDs.  
DTD vs. Schema
Both Document Type Definitions (DTD’s) and XML Schemas (XSD’s, also known as WXS) are industry-standard ways to define XML-based data models, and you’ll find many tools and utilities for working with both DTD and XML Schema.
There are many technical benefits of migrating older DTDs to XML Schema, including:
1. Support for primitive (built-in) data types (eg: xs:integer, xs:string, xs:date, and so on), which facilitates using XML in conjunction with other typed-data, including relational data.
2. The ability to define custom data types, using object-oriented data modeling principles: encapsulation, inheritance, and substitution.  
3. Compatibility other XML technologies, for example, Web services, XQuery, XSLT and other technologies can optionally be schema-aware. 
Advantage of using XML Schema over DTD
XML Schemas Support Data Types. One of the greatest strength of XML Schemas is the support for data types.
XML Schemas are extensible to future additions
XML Schemas are flexible and easy to learn in comparison to DTD
XML Schemas are extensible to future additions
XML Schemas are richer and more powerful than DTDs
XML Schemas are written in XML
XML Schemas support data types
XML Schemas support namespaces
XML Schemas are more powerful than DTD
XML Schemas are a tremendous advancement over DTDs:
• Enhanced datatypes
• 44+ versus 10
• Can create your own datatypes (this gives you flexibility to have your own datatype)
Example: “This is a new type based on the string type and elements of this type must follow this pattern: ddd-dddd, where ‘d’ represents a digit”.
•Written in the same syntax as instance documents
• less syntax to remember
• Object-oriented’ish
• Can extend or restrict a type (derive new type definitions on the basis of old ones)
• Can express sets, i.e., can define the child elements to occur in any order
• Can specify element content as being unique (keys on content) and uniqueness within a region
• Can define multiple elements with the same name but different content
• Can define elements with nil content
• Can define substitutable elements – e.g., the “emp” element is substitutable for the “employee” element.
XML Schemas are extensible, because they are written in XML.
XML Schemas secures Data Communication
• Another great strength about XML Schemas is that they are written in XML.
Simple example
With DTD
Given below is an XML file that contains internal DTD declration:
<?xml version=”1.0″ ?>

<!DOCTYPE name
<!ELEMENT name (title?, fname, middle_name?, lname, suffix?) >
<!ELEMENT title (#PCDATA) >
<!ELEMENT fname (#PCDATA) >
<!ELEMENT middle_name (#PCDATA) >
<!ELEMENT lname (#PCDATA) >
<!ELEMENT suffix (#PCDATA) >
<!ENTITY uuml “ü” >

With Schema
Note, it seems a bit hairy when it comes to defining entities with Schema …. so I left out the &uuml; of Kümar here.
The Schema file:
<?xml version=”1.0″ encoding=”UTF-8″?>

<xsd:schema xmlns:xsd=””>
<xsd:element name=”uuml” type=”xsd:token” fixed=”ü”/>

<xsd:complexType name=”name”>
      <xsd:element name=”title” type=”xsd:string” minOccurs=”0″ maxOccurs=”1″ />
      <xsd:element name=”fname” type=”xsd:string” minOccurs=”1″ maxOccurs=”1″ />
     <xsd:element name=”middle_name” type=”xsd:string” minOccurs=”0″ maxOccurs=”1″ />
      <xsd:element name=”lname” type=”xsd:string” minOccurs=”1″ maxOccurs=”1″ />
      <xsd:element name=”suffix” type=”xsd:string” minOccurs=”0″ maxOccurs=”1″ />

The XML file:
<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>


Scroll to Top