Sometimes it's easier to understand how to write well-formed XML by seeing an example. The Web Writer newsletter is written using a form of XML - I call it AML or About Markup Language (go figure!). Here is a sample "AML" file. While this is a working document, it is not actually a well-formed or valid XML document.
There are some specific rules to create a well-formed XML document:
- The XML declaration must come first in every document.
- Comments are not valid within a tag. Comments may not contain two hyphens in a row, other than the beginning and end of the comment.
- Tags must have an end tag, or be closed within the singleton tag itself, for example <br />.
- All attributes of tags must be quoted, preferably double quotes unless the attribute itself contains a double quote.
- Every XML document must contain one element that completely contains all the other elements.
There are only two problems with the document that make it not well-formed:
- The first thing that the AML document needs is an XML declaration statement.
- The other problem is that there is no one element that completely encloses all the other elements. To fix this, I'll add an external container element: