XML Validator
Validating XML means checking a document against a set of rules — first that it is well-formed, and optionally that it also conforms to a DTD or XML Schema defining its structure.
Well-Formed vs Valid: Two Different Bars
Every XML document has to clear a first, non-negotiable bar: it must be well-formed, meaning it obeys XML's basic syntax rules regardless of what the tags actually mean. Clearing a second, optional bar — being valid — means the document also matches a specific structure defined elsewhere, in a DTD (Document Type Definition) or an XML Schema (XSD), which says things like 'a <book> must contain exactly one <title> and at least one <author>.' A document can be perfectly well-formed and still invalid against a given schema, but it can never be valid without first being well-formed.
The Rules of Well-Formedness
- Exactly one root element contains everything else in the document
- Every opening tag has a matching closing tag (or is self-closed, like <br/>)
- Tags are properly nested — no overlapping, like <b><i>text</b></i>
- Attribute values are always quoted, with single or double quotes
- An element can't have two attributes with the same name
- Special characters like < and & are escaped as < and & in text content
Well-Formed vs Malformed XML
<!DOCTYPE html>
<html>
<body>
<pre>
<!-- Well-formed: single root, properly nested, closed tags -->
<order id="1042">
<customer>Priya Shah</customer>
<items>
<item qty="2">Notebook</item>
<item qty="1">Pen &amp; Pencil Set</item>
</items>
</order>
<!-- Malformed: overlapping tags and an unescaped ampersand -->
<!--
<order id="1042">
<customer>Priya Shah
<items>
<item qty="2">Notebook</customer>
<item qty="1">Pen & Pencil Set</item>
</items>
</order>
-->
</pre>
</body>
</html>Validating Against a DTD
A DTD is the oldest XML schema language, describing allowed elements, their nesting, and their attributes using a compact, non-XML grammar. It's declared inline with a DOCTYPE declaration or pulled in from an external .dtd file, and a validating parser checks the document's structure against it before (or instead of) processing the content.
A DTD and a Document That Validates Against It
<!DOCTYPE html>
<html>
<body>
<pre>
<!-- library.dtd -->
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, year)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ATTLIST book isbn CDATA #REQUIRED>
<!-- catalog.xml -->
<?xml version="1.0"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<book isbn="978-0-13-468599-1">
<title>Effective XML</title>
<author>Elliotte Rusty Harold</author>
<year>2003</year>
</book>
</library>
</pre>
</body>
</html>Validating Against an XML Schema (XSD)
An XSD Schema and a Matching Document
<!DOCTYPE html>
<html>
<body>
<pre>
<!-- library.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:gYear"/>
</xs:sequence>
<xs:attribute name="isbn" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<!-- catalog.xml -->
<?xml version="1.0"?>
<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="library.xsd">
<book isbn="978-0-13-468599-1">
<title>Effective XML</title>
<author>Elliotte Rusty Harold</author>
<year>2003</year>
</book>
</library>
</pre>
</body>
</html>Exercise: XML Validator
What are the two things an XML validator can check for a document?