XML - XML Schema vs DTD: Validation Strategy Comparison
XML documents are designed to store and exchange structured data, but simply following XML syntax does not guarantee that the data has the required structure. For example, an XML document can be well-formed while still containing the wrong elements, missing required information, or using an invalid data type. DTD (Document Type Definition) and XML Schema (XSD) are two technologies used to define and validate the structure of XML documents. Both can specify which elements and attributes are permitted, but XML Schema provides considerably more advanced validation capabilities.
1. What Is DTD?
DTD stands for Document Type Definition. It defines the legal structure of an XML document by specifying elements, attributes, entities, and relationships between elements.
A simple DTD can define an XML document containing a student:
<!DOCTYPE student [
<!ELEMENT student (name, age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
The corresponding XML document could be:
<student>
<name>Rahul</name>
<age>21</age>
</student>
The DTD specifies that the student element must contain name followed by age. It also specifies that both elements contain parsed character data.
DTD can be placed inside an XML document as an internal DTD or stored separately as an external DTD.
2. What Is XML Schema?
XML Schema, commonly called XSD (XML Schema Definition), is an XML-based language used to describe and validate XML documents. It provides more detailed control over XML structure and data.
For example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here, the schema specifies that name must contain a string and age must contain an integer. This provides stronger validation than a basic DTD.
3. Major Difference in Data Types
One of the most important differences between DTD and XML Schema is data-type support.
DTD has relatively limited data-type capabilities. For example:
<!ELEMENT age (#PCDATA)>
This does not explicitly require age to contain a number. It treats the content as character data.
XML Schema provides many built-in data types, including:
-
xs:string -
xs:integer -
xs:decimal -
xs:boolean -
xs:date -
xs:dateTime -
xs:time -
xs:anyURI -
xs:float -
xs:double
For example:
<xs:element name="age" type="xs:integer"/>
This allows a validator to identify an invalid value such as:
<age>twenty</age>
when an integer is required.
4. Namespace Support
DTD has limited and less convenient support for XML namespaces. XML Schema, on the other hand, was designed to work naturally with namespaces.
For example, an XML document may use:
<book:book xmlns:book="http://example.com/books">
<book:title>XML Fundamentals</book:title>
</book:book>
An XML Schema can define the namespace explicitly and validate elements belonging to that namespace.
This makes XML Schema more appropriate for large applications where multiple XML vocabularies need to coexist.
5. Element and Attribute Validation
Both DTD and XML Schema can define elements and attributes.
In DTD:
<!ELEMENT student (name, age)>
<!ATTLIST student id ID #REQUIRED>
The id attribute is required.
In XML Schema:
<xs:attribute name="id" type="xs:string" use="required"/>
XML Schema provides much more control over attribute values. It can specify their data types, allowed values, length restrictions, patterns, and other constraints.
6. Restrictions and Constraints
XML Schema allows developers to place detailed restrictions on values.
For example, an age can be restricted to a particular range:
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="18"/>
<xs:maxInclusive value="60"/>
</xs:restriction>
</xs:simpleType>
This means that the value must be an integer between 18 and 60.
XML Schema can also restrict:
-
Minimum and maximum values
-
String length
-
Number of digits
-
Decimal precision
-
Enumeration values
-
Regular-expression patterns
-
Number of occurrences
DTD does not provide this level of value restriction.
7. Reusability
XML Schema supports reusable types and declarations. A complex type can be defined once and reused in several locations.
For example:
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
This type can subsequently be used by multiple elements.
This makes XML Schema particularly useful for large XML applications and enterprise systems.
8. Extensibility
XML Schema is generally more extensible than DTD. Schemas can be designed using reusable components, type derivation, restrictions, extensions, and namespaces.
For example, one complex type can be extended to create another type with additional information. This makes it easier to develop complex XML structures while maintaining a common base structure.
DTD has fewer mechanisms for sophisticated type reuse and extension.
9. Syntax Differences
DTD uses a specialized syntax that is different from XML:
<!ELEMENT book (title, author)>
<!ELEMENT title (#PCDATA)>
XML Schema itself uses XML syntax:
<xs:element name="book">
...
</xs:element>
Because XSD uses XML syntax, developers who already understand XML can generally work with its structure more naturally. However, XSD can also be considerably more verbose than DTD.
10. Validation Capability Comparison
The fundamental difference can be summarized as follows:
| Feature | DTD | XML Schema |
|---|---|---|
| Basic XML structure validation | Yes | Yes |
| Element declarations | Yes | Yes |
| Attribute declarations | Yes | Yes |
| Strong data types | Limited | Extensive |
| Namespace support | Limited | Strong |
| Numeric validation | Limited | Yes |
| Date and time types | No dedicated types | Yes |
| Value restrictions | Limited | Extensive |
| Pattern restrictions | No | Yes |
| Enumeration | Limited | Yes |
| Type inheritance | No | Yes |
| Reusable complex types | Limited | Yes |
| XML-based syntax | No | Yes |
| Extensibility | Limited | High |
| Complex validation rules | Limited | Much stronger |
11. When Should DTD Be Used?
DTD can still be useful when the XML structure is relatively simple and only basic validation is required. It is also important when working with older XML applications, legacy document formats, or systems that already depend on DTD definitions.
For a simple document, a DTD can be easier to create and understand.
For example, if the primary requirement is simply to ensure that a book contains a title and an author, a DTD may be sufficient.
12. When Should XML Schema Be Used?
XML Schema is generally preferable when an application requires detailed validation. It is particularly useful when XML documents contain numeric values, dates, identifiers, namespaces, complex structures, or strict business rules.
For example, an online order might contain:
<order>
<orderId>10025</orderId>
<quantity>5</quantity>
<price>2499.50</price>
<orderDate>2026-08-16</orderDate>
</order>
An XML Schema can validate that:
-
orderIdfollows an appropriate data type. -
quantityis an integer. -
priceis a decimal value. -
orderDatefollows a valid date format. -
Required elements are present.
-
Values fall within defined limits.
This makes XSD more suitable for applications where data accuracy is important.
13. Practical Example
Consider an employee XML document:
<employee>
<name>Anil</name>
<age>30</age>
<salary>45000</salary>
</employee>
A DTD could define the structure:
<!ELEMENT employee (name, age, salary)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
However, DTD does not strongly specify that age must be an integer or that salary must be a decimal.
An XML Schema could provide much stronger validation:
<xs:element name="age" type="xs:integer"/>
<xs:element name="salary" type="xs:decimal"/>
If someone changes the document to:
<age>thirty</age>
the XML Schema validator can identify the value as invalid because it does not satisfy the xs:integer type.
14. Advantages of DTD
The major advantages of DTD include its simplicity, compact syntax, long-standing support, and suitability for basic XML validation. It is useful for relatively simple document structures and legacy XML systems.
Its main disadvantage is that its data-type system and namespace capabilities are limited compared with XML Schema.
15. Advantages of XML Schema
XML Schema provides strong data typing, namespace support, reusable components, value restrictions, complex type definitions, extensibility, and detailed validation capabilities. It is therefore well suited for large and complex XML-based systems.
Its main disadvantage is complexity. XSD definitions can become lengthy and difficult to understand when schemas contain many types, restrictions, extensions, and relationships.
Conclusion
DTD and XML Schema both serve the fundamental purpose of defining and validating XML documents, but they target different levels of complexity. DTD is simpler and suitable for basic structural validation, while XML Schema provides a much richer and more precise validation framework.
For modern XML applications that require strict data types, namespace support, reusable structures, value restrictions, and complex validation rules, XML Schema is generally the stronger choice. DTD remains relevant for simple XML documents and legacy systems where its simpler syntax and established support are sufficient.