W3C

HTML 5

A vocabulary and associated APIs for HTML and XHTML

← 8.6 Named character referencesTable of contents10 Rendering →

9 The XHTML syntax

This section only describes the rules for XML resources. Rules for text/html resources are discussed in the section above entitled "The HTML syntax".

9.1 Writing XHTML documents

The syntax for using HTML with XML, whether in XHTML documents or embedded in other XML documents, is defined in the XML and Namespaces in XML specifications. [XML] [XMLNS]

This specification does not define any syntax-level requirements beyond those defined for XML proper.

XML documents may contain a DOCTYPE if desired, but this is not required to conform to this specification. This specification does not define a public or system identifier, nor provide a format DTD.

According to the XML specification, XML processors are not guaranteed to process the external DTD subset referenced in the DOCTYPE. This means, for example, that using entity references for characters in XHTML documents is unsafe if they are defined in an external file (except for <, >, &, " and ').

9.2 Parsing XHTML documents

This section describes the relationship between XML and the DOM, with a particular emphasis on how this interacts with HTML.

An XML parser, for the purposes of this specification, is a construct that follows the rules given in the XML specification to map a string of bytes or characters into a Document object.

An XML parser is either associated with a Document object when it is created, or creates one implicitly.

This Document must then be populated with DOM nodes that represent the tree structure of the input passed to the parser, as defined by the XML specification, the Namespaces in XML specification, and the DOM Core specification. DOM mutation events must not fire for the operations that the XML parser performs on the Document's tree, but the user agent must act as if elements and attributes were individually appended and set respectively so as to trigger rules in this specification regarding what happens when an element in inserted into a document or has its attributes set. [XML] [XMLNS] [DOMCORE] [DOMEVENTS]

Certain algorithms in this specification spoon-feed the parser characters one string at a time. In such cases, the XML parser must act as it would have if faced with a single string consisting of the concatenation of all those characters.

When an XML parser creates a script element, it must be marked as being "parser-inserted". If the parser was originally created for the XML fragment parsing algorithm, then the element must be marked as "already executed" also. When the element's end tag is parsed, the user agent must run the script element. If this causes there to be a pending external script, then the user agent must pause until that script has completed loading, and then execute it.

Since the document.write() API is not available for XML documents, much of the complexity in the HTML parser is not needed in the XML parser.

When an XML parser reaches the end of its input, it must stop parsing, following the same rules as the HTML parser.

9.3 Serializing XHTML fragments

The XML fragment serialization algorithm for a Document or Element node either returns a fragment of XML that represents that node or raises an exception.

For Documents, the algorithm must return a string in the form of a document entity, if none of the error cases below apply.

For Elements, the algorithm must return a string in the form of an internal general parsed entity, if none of the error cases below apply.

In both cases, the string returned must be XML namespace-well-formed and must be an isomorphic serialization of all of that node's child nodes, in tree order. User agents may adjust prefixes and namespace declarations in the serialization (and indeed might be forced to do so in some cases to obtain namespace-well-formed XML).

For Elements, if any of the elements in the serialization are in no namespace, the default namespace in scope for those elements must be explicitly declared as the empty string. (This doesn't apply in the Document case.) [XML] [XMLNS]

If any of the following error cases are found in the DOM subtree being serialized, then the algorithm raises an INVALID_STATE_ERR exception instead of returning a string:

These are the only ways to make a DOM unserializable. The DOM enforces all the other XML constraints; for example, trying to set an attribute with a name that contains an equals sign (=) will raised an INVALID_CHARACTER_ERR exception.

9.4 Parsing XHTML fragments

The XML fragment parsing algorithm for either returns a Document or raises a SYNTAX_ERR exception. Given a string input and an optional context element context, the algorithm is as follows:

  1. Create a new XML parser.

  2. If there is a context element, feed the parser just created the string corresponding to the start tag of that element, declaring all the namespace prefixes that are in scope on that element in the DOM, as well as declaring the default namespace (if any) that is in scope on that element in the DOM.

    A namespace prefix is in scope if the DOM Core lookupNamespaceURI() method on the element would return a non-null value for that prefix.

    The default namespace is the namespace for which the DOM Core isDefaultNamespace() method on the element would return true.

  3. Feed the parser just created the string input.

  4. If there is a context element, feed the parser just created the string corresponding to the end tag of that element.

  5. If there is an XML well-formedness or XML namespace well-formedness error, then raise a SYNTAX_ERR exception and abort these steps.

  6. If there is a context element, then return the child nodes of the root element of the resulting Document, in tree order.

    Otherwise, return the children of the Document object, in tree order.