Well-Formed XML

Browsers often recover from sloppily written or illegal HTML. This is not the case with XML documents. Because XML languages vary, the rules for coding the document need to be followed to the letter to ensure proper interpretation by the XML client. In fact, the XML specification strictly prohibits XML parsers from trying to read or render documents with syntax errors. When a document follows the XML markup syntax rules, it is said to be well-formed . Documents that have incorrect syntax are referred to as malformed .

The primary rules for a well-formed XML document are:

  • There may be no whitespace (character spaces or line returns) before the XML declaration, if there is one.

  • An element must have both an opening and closing tag, unless it is an empty element.

  • If an element is empty, it must contain a closing slash before the end of the tag (for example, <br/>).

  • All opening and closing tags must nest correctly and not overlap.

  • There may not be whitespace between the opening < and the element name in a tag.

  • All element attribute values must be in straight quotation marks (either single or double quotes).

  • An element may not have two attributes with the same name.

  • Comments and processing instructions may not appear inside tags.

  • No unescaped < or & signs may occur in the character data of an element or attribute.

  • The document must have a single root element, a unique element that encloses the entire document. The root element may be used only once in the document.

This is by no ...

Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.