Section 7.4. Well-Formed XML


7.4. 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 means a complete list. There are over a hundred criteria that must be met for a document to be well-formed, but many of them follow common sense; for example, there must be at least one character between the brackets <>. But because the syntax rules must be read by machines (without common sense), the rules need to be explicit.

You can check whether the syntax of your XML document is correct using a well-formedness checker (also called a non-validating parser). There is a list of them at the Web Developer's Virtual Library at wdvl.com/Software/XML/parsers.html.




Web Design in a Nutshell
Web Design in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009879
EAN: 2147483647
Year: 2006
Pages: 325

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net