XML Validity

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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 :

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


Well-Formed XML Code:

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

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.