These two terms come up in every XML error message and tutorial, and they are often confused. They answer different questions, and the distinction explains why a file can parse in one tool and fail in another.
Well-formed: does it follow XML's syntax?
A document is well-formed if it obeys the grammar of XML itself. An XML parser must report a well-formedness error and stop, rather than guess. The core rules are:
- There is exactly one root element.
- Every start tag has a matching end tag, or the element is self-closing:
<br/>. - Elements nest properly:
<a><b></b></a>is fine and<a><b></a></b>is not. - Tag and attribute names are case-sensitive:
<Title>and<title>differ. - Attribute values are quoted, with single or double quotes.
- An element cannot have two attributes with the same name.
- The characters
<and&in text must be written as<and&. - Names follow the rules for allowed characters, for example starting with a letter or underscore, and containing no spaces.
- If there is an XML declaration, it must be the very first thing in the file.
- The text uses valid characters for the declared encoding.
A document that meets these is well-formed, even if its tags are nonsense.
Valid: does it follow a particular set of rules?
A document is valid if it is well-formed and conforms to a schema that defines which elements and attributes are allowed, in what order and with what content. Validity is only meaningful relative to a specific schema.
Schema languages include:
- DTD (Document Type Definition): the original, built into XML 1.0, with limited data typing.
- XML Schema (XSD): a W3C standard in XML syntax, with rich data types such as integers, dates and patterns.
- RELAX NG: a schema language many find simpler than XSD.
- Schematron: rule-based validation using assertions.
For example, a schema may say a <book> must contain exactly one <title>, followed by one or more <author> elements, and that year must be an integer. A document with <year>soon</year> is well-formed but not valid.
How the two relate
| Well-formed | Valid | |
|---|---|---|
| Question | Is the syntax correct? | Does it match a schema? |
| Needs a schema | No | Yes |
| Required for parsing | Yes | No |
| Failure result | Parser stops | Validator reports errors |
Every valid document is well-formed. Not every well-formed document is valid.
Common well-formedness errors
- Forgetting to close a tag
- Overlapping tags
- An unescaped ampersand in a URL or company name, such as
AT&T - Unquoted attribute values
- Two root elements, for example when two documents are concatenated
- A
<inside text, for instance in a code sample
The fix for the ampersand and bracket cases is escaping or CDATA; see XML special characters and CDATA.
Checking well-formedness
A parser does it automatically. Docento's Text & Markdown Editor parses .xml files in the browser as you type and shows a clear error with the line when the file is not well-formed. It checks well-formedness only. It does not validate against a DTD or XSD.
Checking validity
Use a validating parser or a command-line tool with your schema, such as xmllint --schema schema.xsd file.xml, or the validation features in your programming language's XML library. If a vendor supplies a schema, validate against it before sending files.
Why the distinction matters
Software that reads XML usually requires well-formedness (it cannot continue without it) and sometimes requires validity (to be sure the content is what it expects). A file that "opens fine" in a browser may still be rejected by a system that validates against a strict schema.
Takeaway
Well-formed means the XML syntax is correct, and every parser demands it. Valid means the document also conforms to a schema, which only some systems check. Fix well-formedness first, then validate against the schema you need.