XML Valid Documents
A well-formed XML document follows basic syntax rules. A valid XML document goes one step further — it also matches a specific structure defined in a DTD or XML Schema. Validity is about structure and content rules, not just syntax.
Think of well-formed as writing a grammatically correct sentence. Valid means the sentence also follows the rules of a specific form — like a job application form where certain fields must be filled in a specific format.
Well-Formed vs Valid
| Feature | Well-Formed | Valid |
|---|---|---|
| Correct XML syntax | Yes | Yes |
| Matches a DTD or Schema | Not required | Required |
| Parser can read it | Yes | Yes |
| Structure is guaranteed | No | Yes |
Why Validity Matters
Imagine a hospital system that receives patient XML files from different clinics. Each file must contain a patient ID, a date of birth, and at least one diagnosis. Without validity rules, one clinic might send files without the patient ID. The hospital system crashes when it tries to process a missing field. Validity rules prevent this — they reject any file that does not match the expected structure before the file reaches the system.
Two Ways to Define Validity Rules
1. DTD (Document Type Definition)
A DTD is an older, simpler way to define what elements and attributes an XML file must contain. It specifies which elements are allowed, what order they appear in, and whether they are required or optional.
<?xml version="1.0"?> <!DOCTYPE book [ <!ELEMENT book (title, author, price)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT price (#PCDATA)> ]> <book> <title>XML for Beginners</title> <author>Meena Iyer</author> <price>350</price> </book>
The DTD above says: a book must contain exactly three child elements — title, author, and price — in that order. If the author element is missing, the document is invalid.
2. XML Schema (XSD)
XML Schema is a more powerful, modern way to define rules. It controls data types, minimum and maximum values, string patterns, and much more. It is written in XML itself.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This Schema enforces that title and author must be strings, and price must be a decimal number. A file with price as "free" would fail validation.
Validity Check Flow Diagram
XML File Received
|
v
Is it Well-Formed?
No --> Parser rejects immediately
Yes --> Continue
|
v
Does it have a DTD or Schema reference?
No --> Treated as well-formed only (not validated)
Yes --> Continue
|
v
Does it match every rule in the DTD/Schema?
No --> Validation Error — file rejected
Yes --> Document is VALID ✓
A Real-World Validation Example
An online store sends order XML files to a warehouse system. The warehouse requires every order to have an order ID, a customer name, and at least one item with a quantity greater than zero.
Invalid Order (missing quantity)
<order>
<orderID>ORD-501</orderID>
<customer>Rahul Mehta</customer>
<item>
<name>Blue Pen</name>
<!-- quantity is missing -->
</item>
</order>
Valid Order
<order>
<orderID>ORD-501</orderID>
<customer>Rahul Mehta</customer>
<item>
<name>Blue Pen</name>
<quantity>10</quantity>
</item>
</order>
Attaching a Schema to an XML File
An XML file references its Schema using the xsi:noNamespaceSchemaLocation or xsi:schemaLocation attribute in the root element.
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd">
<title>Learning XML</title>
<author>Priya Nair</author>
<price>299.50</price>
</book>
Benefits of Validation
- Data integrity: Required fields are always present.
- Data type enforcement: Numbers stay numbers; dates stay dates.
- Error detection early: Catch problems at the data entry point, not deep in the system.
- Interoperability: Different systems agree on the same structure before exchanging data.
Key Points to Remember
- A valid XML document is well-formed AND matches its DTD or Schema.
- Validity rules define which elements exist, in what order, and with what data types.
- DTD is simple and older; XML Schema (XSD) is more powerful and modern.
- A document can be well-formed without being valid.
- Validators check XML files against their DTD or Schema and report rule violations.
