Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

Document syntax

Document type declaration

The <!DOCTYPE root …> in the prolog that names the document's root element and points at, or contains, its markup declarations.

A document type declaration does two separable jobs, and conflating them causes most of the confusion around it. It can point at an external DTD by SYSTEM or PUBLIC identifier, and it can carry an internal subset in square brackets. The internal subset is where entity declarations live, which is why a DOCTYPE turns up in documents that have nothing to do with validation.

Nothing about a DOCTYPE makes a parser validate. Validation against a DTD is a separate mode that has to be asked for, and most parsers — ours included — do not run it. A document can declare a DTD it flagrantly violates and still be read without complaint.

Defined in
XML 1.0 §2.8
Category
Document syntax
Also written
DOCTYPE, <!DOCTYPE>

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.

  • A document that violates its own DTD is still well-formed: the element model is not checked.

    document.xml
    <!DOCTYPE note [<!ELEMENT note (to)><!ELEMENT to (#PCDATA)>]>
    <note><from>who</from></note>

    What the engine returns

    Well-formed.
  • The internal subset is read for entity declarations, and those are expanded.

    document.xml
    <!DOCTYPE x [<!ENTITY who "Ada">]>
    <x>Hello &who;</x>
    expression
    string(/x)

    What the engine returns

    Hello Ada

What catches people out

  • A DOCTYPE is not a schema reference. xsi:schemaLocation is the XSD equivalent, and it is a hint rather than an instruction too.
  • The root element named in the DOCTYPE must match the document's actual root, and that much even a non-validating parser will check.
  • Many hardened parsers reject DOCTYPE outright, because it is the doorway to external entity and entity expansion attacks. Emitting one can make your document unreadable to a careful consumer.

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.