Every technology has its share of controversial topics, and XML is not immune to debate—especially when it comes to document type definitions (DTDs) and schemas. Both of these XML technologies perform similar functions, but the argument over which you should use rages on. Let’s look at some of the pros and cons of each.

The role of DTDs and schemas
Essentially, DTDs and schemas fulfill the same purpose: helping XML developers describe the structure and data within their XML documents. Without DTDs and schemas, it would be difficult to determine whether an XML document is valid in terms of it meeting certain structural and data type criteria.

The DTD or schema provides the XML parser with a description of what a particular type of XML document should look like. For example, it will say whether a <City> element can be contained within an <Address> element. It will also say how many <City> elements the parser should expect to find in the <Address> element, and it can describe the type of data within the element.

The DTD argument
DTDs, which have been around longer than XML, describe the type of document you’re dealing with. When XML arrived as a specification, certain concepts were considered to help protect against mistakes within the document. One concept, validation, is the process of comparing an XML document to its DTD.

The main advantage of the DTD is that it provides validating parsers with a map of how to validate the XML document. The map describes how elements relate to each other within the document. It also specifies how elements nest within each other and the number of times they can occur, and it loosely defines data types.

Because XML first used DTDs to validate documents, they’re the de facto standard protocol for describing XML documents. Nearly every XML package supports validation with DTDs.

As useful as DTDs are, they aren’t really designed for handling the XML domain. The major concern most developers have with DTDs is the lack of good type-checking. Also, DTDs are created in a strange and seemingly archaic format. They have only a limited capability in describing the document structure in terms of how many elements can nest within other elements.

More XML articles

“Specify DataSet requirements in XML, not code”
Find out how you can use the Singleton factory pattern to build DataSet objects. Using an XML configuration document, you can dynamically create DataSet objects.
“An introduction to XML grammar”
Restricting the content in an XML document is important to ensure consistency, but it can be cumbersome. Follow this example to learn how to create a document type definition (DTD) for XML documents and then join the recent discussion in our forum.

The schema argument
The schema is a device created to address the shortcomings of DTDs and to fit more closely with the goals of XML. Almost since XML first appeared, there have been extensions to DTDs and various other solutions. Many solutions have appeared in the form of schemas. Recently, the W3C has entertained a schema specification to help wrangle all of the existing schema solutions into a single standard.

The major advantage of schemas is their ability to more strongly type the data in XML documents. Schemas are described using XML instead of the archaic form used by DTDs. Schemas also provide a richer approach to describing complex XML types.

Document structure can be described more accurately with schemas as well, using features such as minOccurs and maxOccurs to specify the number of times an element can occur within a particular context.

The only real drawback to schemas is that you may encounter a lag in applications that support them.

In a nutshell
Because of the prevalence of DTDs, they are the de facto document validator. But schemas allow you to use better control over data types.

Do you prefer using DTDs, or are you sold on schemas?

Weigh in on the debate over using DTDs vs. schemas. Share your comments in our discussion forum or send us an e-mail.