Schemas and validity
DTD
The original XML schema language: element and attribute declarations written in their own non-XML syntax, plus the entity declarations nothing else provides.
DTDs are weak as grammars — no datatypes, no namespace awareness, no way to say a value must be a date — and they are the only mechanism for declaring entities. That combination is why they persist in formats that have long since moved to XSD for validation.
For our part, we do not offer DTD validation: the WebAssembly build of libxml2 this site runs has no working DTD validation switch. Entity declarations in the internal subset are still read, because that is the parser's normal behaviour rather than a validation mode.
- Defined in
- XML 1.0 §2.8
- Category
- Schemas and validity
- Also written
- document type definition
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
The entity machinery of a DTD works without validation being involved at all.
document.xml <!DOCTYPE x [<!ENTITY ok "yes">]> <x>&ok;</x>expression string(/x)What the engine returns
yesAnd the element model is not enforced: this document contradicts its own DTD.
document.xml <!DOCTYPE x [<!ELEMENT x (a)>]> <x><b/></x>What the engine returns
Well-formed.
What catches people out
- DTDs are not namespace-aware. A DTD constrains the tag text you write, prefix included, rather than the expanded name.
- Everything in a DTD is text: there is no way to say an attribute must be a number.
- Because a DTD can pull in external resources, accepting arbitrary DOCTYPEs is a security decision rather than a parsing one.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- ToolsRELAX NG validatorThe schema language we can validate against, when a DTD is what you have.
- Parse errorsEntity is not definedThe failure mode you still meet from DTDs, long after nobody validates against them.
Related terms
- Document type declarationThe <!DOCTYPE root …> in the prolog that names the document's root element and points at, or contains, its markup declarations.
- Internal subsetThe markup declarations written inline between square brackets in a DOCTYPE, rather than fetched from an external DTD file.
- ValidA well-formed document that also conforms to a declared grammar — a DTD, an XSD schema or a RELAX NG grammar.
- XML Schema (XSD)The W3C schema language: a grammar written in XML itself, with datatypes, namespaces and a type system.
- RELAX NGA schema language built around patterns rather than types, expressing repetition, alternation and interleaving far more directly than XSD.
- Content modelThe rule saying what an element may contain: which children, in what order, how many times, and whether text is allowed.
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.