Nine Rules for Creating Good XML
- Mar 7, 2008
- Rule 1: All XML Must Have a Root Element
- Rule 2: All Tags Must Be Closed
- Rule 3: All Tags Must Be Properly Nested
- Rule 4: Tag Names Have Strict Limits
- Rule 5: Tag Names Are Case Sensitive
- Rule 6: Tag Names Cannot Contain Spaces
- Rule 7: Attribute Values Must Appear Within Quotes
- Rule 8: White Space Is Preserved
- Rule 9: Avoid HTML Tags (Optional)
HTML is a forgiving language. It tolerates a host of sins, from imprecise markup to altogether missing elements, and can still generate a web page in the browser. XML, on the other hand, is basically a tyrant. Violate even the most trivial rule, and the browser or your application will crash. Some people find comfort in the uncompromising nature of XML, because it won’t work unless you build it correctly. It’s great to get instant feedback when you do something wrong!
There are nine basic rules for building good XML:
- All XML must have a root element.
- All tags must be closed.
- All tags must be properly nested.
- Tag names have strict limits.
- Tag names are case sensitive.
- Tag names cannot contain spaces.
- Attribute values must appear within quotes ("").
- White space is preserved.
- HTML tags should be avoided (optional).
XML that follows these rules is said to be "well formed." But don’t confuse well-formed XML with valid XML!
Now let’s look at the rules with some examples.
Rule 1: All XML Must Have a Root Element
A root element is simply a set of tags that contains your XML content.
<root> <author>Ernest Hemingway</author> <author>John Steinbeck</author> <author>James Joyce</author> </root>