Well Formed versus Valid XML Documents

DITA

“Well formed” (or “Well-formed”) and “Valid” are two very important concepts in structured XML authoring.

Table of Contents

Well Formed

Well formed means, the XML tags used in the structured document follow a set of XML rules.
Two of the more important rules are 1) tags should be paired with opening and closing tags, and, 2) tag pairs should not cross over one another and nestle properly.
An example of a well formed XML document:

<letter>
	 <to>B. B. King</to>
	<body>Say hi to Lucille!</body>
</letter>
[Tags are paired. Opening tags are followed by closing tags and they are nestled properly.]

An example of a NOT well formed XML document:

<letter>
	 <to>B. B. King<body>
	</to>Say hi to Lucille!</body>
</letter>
[Tags are paired but they cross over and do not nestle properly.]

Another example of a NOT well formed XML document:

<letter>
	 <to>B. B. King</stop>
	<body>Say hi to Lucille!</end>
</letter>
[Tags are not paired.]

Valid

Valid means the XML code complies with the document’s DTD – Document Type Definition.

Thus it is possible for an XML document to be well-formed but not valid.