HTML vs. XHTML
Differences Between HTML and XHTML
Although HTML and XHTML appear to have similarities in their syntax, they are significantly different in many ways.
- Note: As the current WHATWG document is a draft, this section will need to track to a moving target.
There is a community who find it valuable to be able to serve HTML5 documents which are also valid XML documents. They may, for example, use XML tools to generate the document, and they and others may process the document using XML tools. These documents are served as text/html.
This language is sometimes called "polyglot". It is the overlap language of documents which are both HTML5 documents and XML documents.
This wiki web page is an example of such a document. You can parse it with an XML parser or an HTML parser.
|Feature||HTML Requirement||XHTML Requirement||Notes|
|Mime Type||Must use
||Must use an XML MIME type, such as
||It is the MIME type that determines what type of document you are using. Any document, including a document authored with the intention of being XHTML, served as |
Note that XHTML 1.0 previously defined that documents adhering to the compatibility guidelines were allowed to be served as
text/html, but HTML 5 now defines that such documents are HTML, not XHTML.
Syntax and Parsing
XHTML uses XML parsing requirements. HTML uses its own which are defined much more closely to the way browsers actually handle HTML today. The following table describes the differences between how each is parsed.
|Feature||HTML Requirement||XHTML Requirement||Notes|
|Parsing Modes||Three parsing modes are defined: no quirks mode, quirks mode and limited quirks mode. The mode is only ever changed from the default by the HTML parser, based on the presence, absence, or value of the DOCTYPE string.||XML parsing rules are used. There is only one mode.||The parsing modes in HTML also have an effect upon script and stylehsheet processing. XHTML is considered to be in no quirks mode for these purposes.|
|Error Handling||HTML does not have a well-formedness constraint, no errors are fatal. Graceful error handling and recovery procedures are thoroughly defined.||Well-formedness errors are fatal|
|Namespaces||Elements and attributes for known vocabularies (HTML, SVG and MathML) are implicitly assigned to appropriate namespaces, according to the rules specified in the parsing algorithm.||The rules defined in the Namespaces in XML specification apply. Namespaces must be explicitly declared.|
|Namespace attributes on HTML elements||Elements in the HTML namespace may have an
Attributes of the form
|The HTML namespace must be declared for HTML elements according to the rules defined by Namespaces in XML. The
|Namespace attributes on foreign elements||
Elements in the SVG namespace may have an
Elements in the MathML namespace may have an
Foreign elements may also have an
When parsed by an HTML parser, the
|The SVG and MathML namespaces must be declared for SVG and MathML elements, respectively, according to the rules defined by Namespaces in XML. The
|XLink attributes||Foreign elements may use the attributes
||XLink attributes may be specified on foreign elements using any prefix, subject to the conformance rules defined by Namespaces in XML. The XLink namespace must be declared according to the conformance rules defined by Namespaces in XML if XLink attributes are used within the document.|
Foreign elements may use the attributes
HTML elements may use the
|Any element, including HTML elements, may use the attributes
|Space characters||The space characters are defined as:
||The space characters are defined as:
||The difference is the inclusion of Form Feed.|
A DOCTYPE is a mostly useless, but required, header. The DOCTYPE is used during parsing to determing the parsing mode. The keywords "
Conforming HTML documents are required to use
When using the obsolete but conforming DOCTYPEs based on the HTML 4.0 and 4.01 Strict DTDs, the system identifier is optional. The obsolete but conforming DOCTYPEs based on XHTML 1.0 Strict and XHTML 1.1 may also be specified.
Use of an internal subset is forbidden. The system identifier is never de-referenced by HTML implementations.
The DOCTYPE is optional. XML rules for case sensitivity apply (everything is case sensitive).
Either of the DOCTYPEs defined in HTML5 may be used, or any other custom DOCTYPE. If the poublic identifier is specified, the system identifier must also be specified. The obsolete status of the obsolete permitted DOCTYPEs defined for HTML does not apply to XHTML. Any DOCTYPE may be used, subject to the conformance rules defined by XML.
Use of an internal subset is permitted according to the requirements of XML. Some validating XML processors may dereference the system identifier, if used, but most browsers use non-validating processors.
|Void Elements||Void elements only have a start tag; end tags must not be specified for void elements, and it is impossible for them to contain any content. A trailing slash may optionally be inserted at the end of the element's tag, immediately before the closing greater-than sign.||Void elements may use either the empty-element tag syntax (EmptyElemTag) or use a start tag immediately followed by an end tag, with no content in between. While it is possible for the element to contain content, this is non-conforming.|
|Raw text elements|
For some elements, the start and/or end tags are optional and are implied by certain specified conditions. For example, the end tag for the
Omitting the end tag for other elements is a parse error and various error recovery procedures are applied appropriately.
|End tags must be explicitly included for all elements, except empty elements using the EmptyElemTag syntax.|
|Unescaped Special Characters||
Unescaped ampersands (U+0026 AMPERSAND -
Unescaped less than signs (U+003C LESS-THAN SIGN -
|Unescaped ampersands and less-than signs may not appear within CharData or AttValue (basically, the normal text content of elements and attribute values.) Violation of this constraint is a well-formedness error.|
|Comment syntax||Comments must start with the four character sequence "
||The content of comments must not contain two consecutive U+002D HYPHEN-MINUS (-) characters, nor end with a hypen. Violating this is a well-formedness error.|
THIS PAGE IS IN THE PROCESS OF BEING REVISED
- HTML Parse Errors with special handling:
- End tags with attributes.
- Unexpected end tags (in HTML, an unexpected
</p>can cause the start tag to be implied before it).
- The sequence of characters "
]]>" in content when it does not mark the end of a
CDATAsection is a well-formedness error in XHTML, but valid in HTML.
- In XHTML:
CDATAsection. In HTML, it's a bogus comment.
- In XHTML,
<?foo ...?>is a processing instruction. In HTML, it's a bogus comment.
- In HTML, the trailing slash used for the empty element syntax is a parse error for non-void elements (see below), but is ignored in all cases.
- In HTML, the
styleelements are parsed as
CDATAelements. (Note: the definition of
CDATAdiffers from that in XML). In XML, they're parsed as normal elements (which means that things that look like comments are treated as real comments, and things that look like start tags actually are start tags).
- In HTML, the
textareaelements are parsed as
RCDATAelements. (Note: The definition of
RCDATAdiffers from that in SGML and there is no
- In HTML, if scripting is enabled, the
noscriptelement is parsed as an
CDATAelement. If scripting is disabled, it's parsed as a normal element. In XHTML, the element is always parsed as a normal element, and can't really be used to stop content from being present when script is disabled.
- In HTML, the
noframeselements are parsed as
CDATAelements. In XHTML, they are parsed as normal elements, and therefore do not stop content from being used.
- White space characters in attribute values are normalized to spaces in XHTML.
- In HTML, elements with optional tags are implied in certain conditions.
- In HTML, tags for certain elements, which appear out of context, are ignored. This includes
plaintextelement has a special parsing requirement in HTML. (It is, however, forbidden.)
- In HTML, a line feed that immediately follows a
textareastart tag is ignored.
- Many other special handling of edge cases and error conditions, not all of which are listed here, occur in HTML.
- In HTML, the
doctypeis required. In XHTML, it is optional.
- In HTML, the DOCTYPE is case insensitive. (e.g.
<!doctype html>, or any case variation of that is acceptable). In XHTML, the DOCTYPE, if used, is case sensitive and must be well-formed XML. i.e.
<!DOCTYPE html>, with optional PUBLIC and/or SYSTEM identifiers.
- In XHTML, tag names and attribute names are case sensitive. In HTML, they are case insensitive.
- In XHTML, non-empty elements require both a start and an end tag. In HTML, certain elements allow the omission of either or both:
- In XHTML, empty elements may use either the empty element syntax (
<br/>) or have an end tag immediately follow the start tag (
<br></br>). In HTML, the empty element syntax (trailing slash) is allowed on void elements, but forbidden on other elements. However, it serves no purpose whatsoever and can be omitted. End tags for void elements are forbidden.
- HTML allows attribute minimisation (i.e. omitting the equals sign and the value), XHTML does not.
- HTML allows the use of unquoted attribute values, XHTML does not.
- XHTML allows the use of
CDATAsections, HTML does not.
- XHTML allows the use of processing instructions, HTML does not.
- In HTML, all entity references are predefined and do not require a DTD. But because there is no DTD for XHTML5, entity references cannot be used in XHTML. (excluding the 5 predefined entities:
- You can provide your own DTD for use with your own validating parser, but be aware that browsers do not use validating parsers and will not read the DTD.
- The valid set of unicode characters in XML 1.0 is limited beyond that in HTML.
- Namespace prefixes are permitted in XHTML. They are forbidden in HTML.
HTML Elements with Optional Tags
|Element||Start Tag||End Tag|
- The namespace declaration (
xmlnsattribute) is required in XHTML. The xmlns attribute is also allowed to appear on any element in HTML on the condition that is has the value
- In HTML, the xmlns attribute has absolutely no effect. It is basically a talisman. It is allowed merely to make migration to and from XHTML mildly easier. When parsed by an HTML parser, the attribute ends up in the null namespace
- In XML (with an XML Namespaces-aware parser), an xmlns attribute is part of the namespace declaration mechanism, and an element cannot actually have an xmlns attribute in the null namespace. In DOM implementations, the attribute ends up in the "
- XHTML allows non XHTML elements and attributes (in different namespaces) to be used, HTML does not.
- XHTML uses the
xml:langattribute, HTML uses
- XML ID introduces
xml:id, which could be used in XHTML. In HTML it has no effect.
- In HTML, the
noscriptelement may be used. In XHTML, it is forbidden.
- XHTML can use
xml:base, HTML cannot.
- In XHTML,
tableelements may contain child
trelements. In the HTML serialisation, due to backwards compatibility constraints, this is not possible (though it may be done through DOM manipulation).
- In XHTML, the XML declaration may be used to specify the character encoding. In HTML, the XML declaration is forbidden
- In HTML, the
metaelement with a
charsetattribute may be used instead. It is forbidden in XHTML unless it specifies 'UTF-8' (case insensitively) and is ignored if included.
- The default character encoding for XHTML is, according to XML rules,
UTF-16. If the encoding is unspecified in HTML, it should be determined through implementation specific heuristics or fallback to a default value (Note: this section of the spec is not yet finished).
document.writeln()cannot be used in XHTML, they can in HTML.
- In XHTML, the use of the
innerHTMLproperty requires that the string be a well-formed fragment of XML.
- DOM APIs are case sensitive in XHTML and some are case insensitive in HTML. (This does not apply to elements which are not in the HTML namespace)
- Element.tagName and Node.nodeName return the value in uppercase.
- Document.createElement() is case insensitive (the canonical form is lowercase).
- Element.setAttributeNode() will change the attribute name to lowercase.
- Element.setAttribute() is case insensitive (the canonical form is lowercase).
- Document.getElementsByTagName() and Element.getElementsByTagName() are case insensitive.
- Document.renameNode(). If the new namespace is the HTML namespace, then the new qualified name will be lowercased before the rename takes place.
- In HTML, Document.createElement() will create an element in the HTML namespace. In XML (including XHTML), the namespace is defined by both DOM2 and DOM3 to be null.
- In XHTML, browsers lack interoperability in this area. In Firefox and Safari, the namespace is dependent upon the MIME type. In Opera, it's dependent upon the root element.
- XPath expressions targeted at pre-HTML5 browsers need to use the XHTML namespace for XHTML and null for HTML. (HTML5 browsers would use the XHTML namespace even in HTML.)
- Selectors, as used in CSS, match case sensitively in XHTML, but case insensitively in HTML.
- CSS requires special handling of the body element in HTML for painting backgrounds on the canvas, which do not apply to XHTML.
Differences Between HTML4 and HTML5
Differences Between DOM Level 2.0, 3.0 and the HTML 5 DOM APIs
This section might belong on a separate page.
- TODO (need to talk about the changes to the DOM API that HTML5 is making, compared with DOM2 and DOM3)