Comparison
XSD vs DTD
DTDs came with XML and are still the only way to declare an entity. For anything else, XSD has replaced them — and this is the one comparison this site cannot run.
A DTD can say which elements may contain which, and nothing about what a value is. Everything is text, there is no namespace awareness, and the syntax is not XML — which means a DTD cannot be processed with the tools you use for everything else.
It survives for one reason: entity declarations. There is no other mechanism in XML for defining &myentity;, so a document that needs one carries a DOCTYPE whether or not anybody validates against it. That is also why DOCTYPE is a security surface — the same machinery reaches external resources.
The honest position is: use XSD or RELAX NG to validate, avoid DTDs for anything else, and if you are only emitting a DOCTYPE for entities, consider expanding them before publishing instead.
What actually differs
| Aspect | XSD | DTD |
|---|---|---|
| Syntax | XML | Its own, non-XML |
| Datatypes | Full library with facets | None. Everything is text |
| Namespaces | Central | Not aware of them at all |
| Entities | Cannot declare them | The only way to declare one |
| Cardinality | minOccurs and maxOccurs, any numbers | ?, * and + only |
| Where it lives | A separate document, referenced or supplied | Inline in the DOCTYPE, or external |
| Security | Fetching a schema is the validator's choice | Doorway to external entity and expansion attacks |
Why there is no demo
Nothing here to execute
We cannot run DTD validation. The WebAssembly build of libxml2 this site uses has no working DTD validation switch, so a demonstration here would be a claim about a validator nobody ran — and the entire point of these pages is that both sides are executed. The entity half of a DTD does work, and is demonstrated on the glossary pages for the internal subset and general entities.Which to pick
XSD, when
- Anything that validates values rather than only shape — which is almost every reason to validate.
- The vocabulary uses namespaces, which a DTD cannot see.
- You want the grammar to be processable with ordinary XML tooling.
DTD, when
- You need to declare general entities. Nothing else in XML can.
- You are working with a legacy vocabulary — DocBook 4, older publishing chains — whose DTD is the normative definition.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.