Document Type Declaration (DTD) is an SGML (Standard Generalized Markup Language) and XML (Extensible Markup Language) construct that identifies a document type. It is used to define the structure, elements, and attributes of a document type.
Document Type Definition (DTD) is a set of rules used to define the structure and elements of an XML document. It is used to validate the syntax and structure of an XML document to ensure that it is well-formed and valid.
Document type declaration
AN document type declarationor DOCTYPEis an instruction that associates a certain XML or SGML document (for example, a Web page) common document type definition (DTD) (for example, the formal definition of a specific version of HTML 2.0 – 4.0). At the serialized form of the document, it manifests itself as a short sequence of marking that conforms to a specific syntax.
The HTML layout engines in modern web browsers perform DOCTYPE “sniffing” or “switching”, where the DOCTYPE in a document served as
text/html determines a layout mode, such as “quirks mode” or “default mode”.
text/html serialization of HTML5, which is not based on SGML, uses DOCTYPE for mode selection only. Because web browsers are implemented with special-purpose HTML parsers rather than general-purpose DTD-based parsers, they don’t use DTDs and never access them, even if a URL is provided. The DOCTYPE is retained in HTML5 as a “mostly useless but necessary” header just to trigger “default mode” in common browsers.
The general syntax for a document type declaration is:
<!DOCTYPE root-element PUBLIC "/quotedFPI/" "/quotedURI/" [ <!-- internal subset declarations --> ]>
<!DOCTYPE root-element SYSTEM "/quotedURI/" [ <!-- internal subset declarations --> ]>
In XML, the root element representing the document is the first element of the document. For example, in XHTML, the root element is , being the first element opened (after the declaration of the doctype) and the last one closed. The keywords SYSTEM and PUBLIC suggest what type of Document Type Definition (DTD) it is (one that is on a private system or one that is open to the public). If the keyword PUBLIC is chosen, it will be followed by a restricted form of “public identifier” called formal public identifier (FPI) enclosed in double quotes. The public identifier can be followed by a “system identifier” which is also enclosed in double quotes. For example, the FPI for XHTML 1.1 is “-//W3C//DTD XHTML 1.1//EN” and there are 3 possible system identifiers available for XHTML 1.1 depending on your needs. One is the URI reference “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“. If the SYSTEM keyword is chosen instead, only a system identifier is provided. This means that the XML parser must locate the DTD in a system-specific way, in this case via a URI reference of the DTD enclosed in double quotes. The last part, enclosed in literal square brackets (), is called an inner subset that can be used to add/edit entities or add/edit PUBLIC keyword behaviors. The inner subset is always optional (and sometimes even prohibited within simple SGML profiles, particularly those for basic HTML parsers that don’t implement a full SGML parser).
In XHTML documents, the doctype declaration must always explicitly specify a system identifier. On the other hand, in SGML-based documents such as HTML, the appropriate system identifier can be inferred automatically from the given public identifier. This association can, for example, be performed through a catalog file by resolving the FPI to a system identifier.
The first line of a World Wide Web page can be read as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="ar" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
This document type declaration for XHTML includes by reference a DTD, whose public identifier is
-//W3C//DTD XHTML 1.0 Transitional//EN and whose system identifier is
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd. An entity resolver can use any identifier to locate the referenced external entity. No inner subsets were indicated in this example or the next ones. The root element is declared as
html and therefore it is the first tag to open after the doctype declaration ends in this example and the next ones as well. The HTML tag is not part of the doctype declaration, but has been included in the examples for guidance.
Some common DTDs have been placed in lists. The W3C has produced a list of commonly used web DTDs, which contains the “bare” HTML5 DTD, older XHTML/HTML DTDs, common embedded XML-based formats DTDs such as MathML and SVG as well as “composite” documents that combine these formats. Both W3C HTML5 and its corresponding WHATWG version recommend that browsers only accept XHTML DTDs from certain FPIs and prefer to use internal logic rather than fetching external DTD files. Also, it specifies an “internal DTD” for XHTML, which is just a list of HTML entity names.
HTML 4.01 DTDs
The strict DTD disallows presentational markup on the grounds that Cascading Style Sheets should be used for this. This is how the Strict DTD looks like:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
The transitional DTD allows for some older attributes and PUBLIC that have been deprecated:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
if frames are used, the Frameset DTD should be used instead, like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html>
XHTML 1.0 DTDs
XHTMLDTDs are also Strict, Transitional, and Frameset.
XHTML Strict DTD. Not discontinued tags are supported and the code must be written correctly according to the XML Specification.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
XHTML Transitional DTD is like XHTML Strict DTD but obsolete tags are allowed.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
XHTML Frameset DTD is the only XHTML DTD that supports Frameset. The DTD is below.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
XHTML 1.1 DTD
XHTML 1.1 is the most current finalized revision of XHTML, introducing support for XHTML Modularization. XHTML 1.1 has the rigor of XHTML 1.0 Strict.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
Basic XHTML DTDs
Basic XHTML 1.0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
Basic XHTML 1.1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
DOCTYPE without DTD in HTML5
HTML5 use a
DOCTYPE statement that is too short, due to the lack of references to a DTD in URL or FPI form. All it contains is the tag name of the document’s root element,
HTML. In the words of the draft specification itself:
In other words,
With the exception of a missing URI or FPI string (the FPI string is treated as case sensitive by validators), this format (a case-insensitive match of the string
!DOCTYPE HTML) is the same as that found in the syntax of HTML 4.01 based on SGML
DOCTYPE. In both HTML4 and HTML5, formal syntax is defined as uppercase, even though both lowercase and mixed uppercase are also treated as valid.
At the XHTML5 The
DOCTYPE must be a case-sensitive match of the string “
<!DOCTYPE html>“. This is because in XHTML syntax all HTML element names must be in lowercase, including the root element referenced within HTML5
DOCTYPE is optional in XHTML5 and can simply be omitted. However, if the tag is rendered as XML and HTMLa DOCTYPE must be used.
- HTML document type overview
- Recommended DTDs to use in your web document – an informational (non-normative) publication of the W3C Quality Assurance
- DOCTYPE grid – another overview table [Last modified 27 November 2006]
- Quirks mode and transition mode
- Box model adjustments