This code example is in XHTML, a pattern that attempts to unify HTML with XML. Both languages are quite similar, but there are some crucial differences, so the need to specify DOCTYPE
.
The Doctype is a statement that associates the current document with a Document type definition ), this is responsible for defining the exact syntax used (XML or SGML , of which HTML is a dialect), what elements are allowed, what attributes, etc. The general syntax for this statement is:
<!DOCTYPE root-element PUBLIC "FPI" ["URI"] [
<!-- internal subset declarations -->
]>
or:
<!DOCTYPE root-element SYSTEM "URI" [
<!-- internal subset declarations -->
]>
In your example, the root element is html
(the main document tag), the type is public (because the HTML specification is open to the public), the first value in quotation marks is a unique identifier for that DTD , and the second value is the public URI of the document that describes it.
In this case, this is an XHTML. If it were HTML 4 strict mode, for example, would be:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
In the case of HTML5, the syntax allows you to omit the declaration of a DTD, saying only that "the document is HTML5" and nothing else:
<!DOCTYPE html>
Since xmlns
is nothing more than a namespace XML - as a document XHTML is a type of XML, it can have a namespace that determines the meaning of each element in that document. If you wanted to embed another type of document within XHTML, such as SVG or MathML, and those documents had elements with the same name as the XHTML elements, it could conflict. The use of namespaces helps in this, allowing you to prefix the "foreign" elements with an identifier saying that they are elements of a different type of document with their own namespace . Example:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Math Example</title>
</head>
<body>
<p>The following is MathML markup:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply> <log/>
<logbase>
<cn> 3 </cn>
</logbase>
<ci> x </ci>
</apply>
</math>
</body>
</html>
Font
In this example above, all elements within math
are considered elements of MathML, not XHTML, for validation purposes. In this case no prefixes were required (since the elements did not mix), but in other cases they might be needed:
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6' xml:lang="en" lang="en">
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
In the above section, the title
element belongs to the default of the document, whereas the number
element belongs to another namespace .