XSD vs DTD: Choosing and Applying an XML Schema

November 08, 2026 · XML.now Editorial · XML Troubleshooting

Compare namespaces, data types, entities, validation limits and migration decisions.

When Validation Fails: Identifying the Schema Conflict

An XML document that parses without error can still fail validation with messages like "Element not expected" or "No declaration for attribute." The immediate diagnostic is to check the document's prolog and root element:

  • A <!DOCTYPE declaration signals a DTD expectation.
  • An xsi:schemaLocation or xmlns:xs attribute signals an XSD expectation.

The underlying failure mode differs by schema language. DTD breakage typically involves undeclared entities, missing #PCDATA declarations, or attribute type mismatches (ID versus IDREF). XSD breakage typically involves data type mismatches, namespace errors, or occurrence constraint violations (minOccurs/maxOccurs).

A DTD defines document structure through a grammar of element and attribute rules, as specified in the XML 1.0 Recommendation. An XSD defines structure and data types using XML elements, with namespace support and richer constraints layered on top of the core XML specification.

Namespace Handling: Where DTD Validation Stops

DTD Limitation: No Namespace Awareness

A DTD treats the element name dc:title as a literal string containing a colon. When a document mixes elements from multiple vocabularies—for example, Dublin Core metadata inside DocBook content—the DTD cannot validate namespace-qualified child elements independently. The only workaround is to declare every possible prefixed element name in the DTD, which eliminates the separation that namespaces provide.

Observable check: If your instance document declares xmlns:dc="http://purl.org/dc/elements/1.1/" and validation fails on dc:title, examine the DTD for an <!ELEMENT dc:title ...> declaration. If absent, the DTD rejects the element regardless of namespace correctness.

XSD Design: Target Namespace per Schema

An XSD schema targets a specific namespace through the targetNamespace attribute:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://example.com/books">

All element and type definitions within that schema belong to the declared namespace. The instance document must bind the correct namespace and may use xsi:schemaLocation to point to the .xsd file.

Observable check: When an XSD declares targetNamespace but the instance document omits the corresponding namespace declaration, the validator rejects elements from that namespace. When the XSD omits targetNamespace, it validates only unqualified elements, and any namespace-prefixed element fails.

If you are constrained to a DTD and must handle multiple vocabularies, the practical options are to flatten namespaces by removing prefixes or to accept that validation will be partial.

Data Types: The Primary Failure Boundary

DTD: Attribute Types Only, No Element Typing

DTDs define ten attribute types: CDATA, ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, ENTITIES, NOTATION, and enumerated values. Element content is always parsed character data (#PCDATA) or child elements. No mechanism exists to constrain element text to a numeric range, a date format, or a boolean value.

Failure example: The element <price>fifty dollars</price> passes DTD validation because all element content is #PCDATA. The same document fails under an XSD that declares <xs:element name="price" type="xs:decimal"/>.

Observable check: If your XML contains elements that must hold numbers, dates, or booleans, and validation must catch format errors before application code runs, DTD alone is insufficient.

XSD: Built-in and Derived Types

XSD provides over 40 built-in types including xs:string, xs:integer, xs:decimal, xs:date, xs:time, xs:boolean, and xs:anyURI. You can derive restricted types:

<xs:simpleType name="priceType">
  <xs:restriction base="xs:decimal">
    <xs:minInclusive value="0.00"/>
    <xs:maxInclusive value="9999.99"/>
  </xs:restriction>
</xs:simpleType>

Observable check: When migrating a DTD element from #PCDATA to an XSD typed element, review actual data in existing documents. A field declared as xs:date will reject values like "January 2024" that passed DTD validation without complaint.

Entities: A DTD Mechanism with No XSD Equivalent

The XML specification defines entity declaration as part of the DTD. An internal entity such as:

<!ENTITY publisher "Acme Press, 2024">

allows &publisher; to appear throughout the document. XSD 1.0 and 1.1 provide no entity declaration mechanism. An XSD validator rejects &publisher; as an undefined reference unless the entity is declared in an internal DTD subset or resolved by a preprocessor before validation.

Hard limit: You cannot replace a DTD with an XSD if your workflow depends on entity references for reusable text, copyright notices, or boilerplate, without also changing the document preparation pipeline.

Practical options when entities are required: 1. Keep a minimal internal DTD subset inside the XML file that declares the entities, and validate structure with an external XSD. 2. Replace entity references with XInclude or XSLT expansion before XSD validation.

Validation Boundaries: What Neither Schema Can Enforce

DTD Cannot Enforce

  • Element content data types beyond #PCDATA
  • Occurrence constraints on mixed content (no way to require "at least one paragraph, then zero or one image" within mixed content)
  • Uniqueness across multiple elements (only attribute-based ID uniqueness, document-wide)
  • Co-occurrence constraints (attribute A required only when attribute B equals a specific value)

XSD Cannot Enforce

  • Entity declarations
  • Cross-document references without XSD 1.1 xs:assert using XPath, which adds complexity and limits processor compatibility
  • Arithmetic rules such as "total must equal sum of line items"
  • Conditional element presence based on sibling element values without xs:assert

Observable check: If validation requirements include arithmetic, cross-element calculations, or conditional attribute presence, neither DTD nor XSD alone suffices. The practical combination is XSD for structure and type constraints, plus Schematron for business rules, or post-validation checks in application code.

Deciding Between DTD and XSD

Choose XSD When

  • Element content requires data type validation (dates, numbers, booleans)
  • The document uses multiple namespaces and requires per-namespace validation
  • Type definitions must be reused across schemas via xs:include or xs:import
  • The toolchain supports XSD and a legacy DTD is being maintained only for historical reasons

Keep DTD When

  • The XML is small, single-namespace, and has no element-level type requirements
  • The toolchain requires DTD (older SGML systems, embedded SAX parsers without XSD support)
  • Entity references are essential and cannot be removed from the document workflow
  • Validation must occur inside a parser that only supports DTD

Migrating from DTD to XSD: Steps with Observable Checks

  1. Extract declarations: Copy all element and attribute declarations from the DTD into a new XSD file. Check: The XSD file contains an xs:element for each <!ELEMENT and an xs:attribute for each <!ATTLIST entry.

  2. Map content models: Convert DTD sequences to XSD compositors. (title, author+) becomes:

    <xs:sequence>
      <xs:element ref="title"/>
      <xs:element ref="author" maxOccurs="unbounded"/>
    </xs:sequence>
    

    Check: The minOccurs and maxOccurs attributes match the DTD occurrence indicators (?, *, +).

  3. Replace #PCDATA with typed elements: Review actual data in existing XML files, then assign types. Check: <xs:element name="price" type="xs:decimal"/> rejects non-numeric content that the DTD previously accepted.

  4. Remove entity declarations: Replace &entity; references with literal text in the XML files, or retain a minimal internal DTD subset. Check: The XSD validator no longer reports undefined entity errors.

  5. Validate with both schemas: Use a tool like xmllint with --dtdvalid for the original DTD and --schema for the new XSD. Check: The same document produces identical structural validation results (entity and type differences aside).

Tools such as trang can automate the initial DTD-to-XSD structural conversion, but they cannot recover semantic type information. Every #PCDATA element will map to xs:string. Manual review and type assignment remain necessary.

What Migration Cannot Recover

  • Entity replacement text: Unless entities are resolved before conversion, the text they represent is lost.
  • ID/IDREF cross-document semantics: XSD supports xs:ID and xs:IDREF but cannot validate references that span multiple files.
  • Parameter entities: DTDs use parameter entities for macro-like reuse within the DTD itself. XSD has no equivalent; use xs:include, xs:import, or XInclude for schema composition.
  • Unordered content models: A DTD content model like (title | author)+ allows any order. XSD requires an explicit xs:choice or xs:all. In XSD 1.0, xs:all restricts elements to minOccurs of 0 or 1 and maxOccurs of 1. Element ordering may be enforced differently after migration.