A well-formed XML document adheres to a few basic rules. First, each XML document must have a single unique root element. In other words, the entire document is contained within a single element. Second, elements in a well-formed XML document cannot overlap, but must be properly nested. Third, the start and end tags for each element must match. XML tags are case-sensitive. Therefore, for example, <phone> and <Phone> are different tags. Fourth, all empty elements must be closed. Syntax for empty elements that was legal in HTML, such as <br>, is not allowed in well-formed HTML. The correct syntax in this case is <br></br> or <br />. Finally, reserved characters must be represented through appropriate entities, keywords that act as stand-ins for the desired character (for example, & represents an ampersand, and " represents quotation marks). Listing 1 is an XML document that illustrates these rules.
Listing 1A Basic Well-Formed XML Document
<contacts> <contact> <name>John Smith</name> <phone number="555-5555" ext="100" /> </contact> <contact> <name>Bob O'Connor</name> <phone number="555-5555" ext="101" /> </contact> </contacts>