About Me

Friday, 13 April 2012

XML Documents: Valid Versus Well-Formed

XML Documents: Valid Versus Well-Formed

In XML, there are three ways to measure a document's validity: valid, well-formed, and broken. You probably only care that your XML document works, but knowing the requirements for each kind of validity may shed some light on problems that arise in the future.

Well-Formed XML

A well-formed XML document has to follow several rules that most other markup languages also follow. These are generic rules that must be followed for a document to be well-formed, as well as valid. The rules are as follows:
  • One Root Element - The XML document may only have one root element. See our Root Element Lesson for more information.
  • Proper Nesting - XML elements must be closed in the order they are opened. See our Nesting Lesson for more information.
  • Well-Formed Entities - Any entities that are referenced in the document must also be well-formed. See our Entity Lesson for more information.
The first example below is incorrect, and the second is well-formed. Try to figure out the error in the first example.

Broken XML Code:

<email>
 <to>Mr. Garcia
  <body>Hello there! How are we today?</to>
 </body>
</email>

Well-Formed XML Code:

<email>
 <to>Mr. Garcia</to>
 <body>Hello there! How are we today?</body>
</email>
The error in the first example was:
  • The document suffers from improper nesting. The body element was opened inside the to element, yet body was not closed before the to element was closed!

Valid XML

First and foremost, a valid XML document must be well-formed. The well-formed requirement (see above) should be fairly straightforward. The key to making an XML document leap from well-formed to valid is slightly more difficult.
To be valid, an XML document must be validated. A document cannot be validated unless a Document Type Definition (DTD), internal or external, is referenced for the XML processor. For the XML document to be valid, it must follow all the rules set forth in the DTD.

Broken XML Files

Broken XML documents are simply those which fail to follow the rules required for a document to be either well-formed or valid. Many of the XML editors on the market today (free and purchased) are an excellent source of help when attempting to fix small errors in your XML code.

0 comments:

Post a Comment