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 &lt; and &amp; in text content

Well-Formed vs Malformed XML

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!-- Well-formed: single root, properly nested, closed tags --&gt;
&lt;order id="1042"&gt;
  &lt;customer&gt;Priya Shah&lt;/customer&gt;
  &lt;items&gt;
    &lt;item qty="2"&gt;Notebook&lt;/item&gt;
    &lt;item qty="1"&gt;Pen &amp;amp; Pencil Set&lt;/item&gt;
  &lt;/items&gt;
&lt;/order&gt;

&lt;!-- Malformed: overlapping tags and an unescaped ampersand --&gt;
&lt;!--
&lt;order id="1042"&gt;
  &lt;customer&gt;Priya Shah
  &lt;items&gt;
    &lt;item qty="2"&gt;Notebook&lt;/customer&gt;
    &lt;item qty="1"&gt;Pen &amp; Pencil Set&lt;/item&gt;
  &lt;/items&gt;
&lt;/order&gt;
--&gt;
</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>
&lt;!-- library.dtd --&gt;
&lt;!ELEMENT library (book+)&gt;
&lt;!ELEMENT book (title, author, year)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
&lt;!ELEMENT author (#PCDATA)&gt;
&lt;!ELEMENT year (#PCDATA)&gt;
&lt;!ATTLIST book isbn CDATA #REQUIRED&gt;

&lt;!-- catalog.xml --&gt;
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE library SYSTEM "library.dtd"&gt;
&lt;library&gt;
  &lt;book isbn="978-0-13-468599-1"&gt;
    &lt;title&gt;Effective XML&lt;/title&gt;
    &lt;author&gt;Elliotte Rusty Harold&lt;/author&gt;
    &lt;year&gt;2003&lt;/year&gt;
  &lt;/book&gt;
&lt;/library&gt;
</pre>

</body>
</html>

Validating Against an XML Schema (XSD)

An XSD Schema and a Matching Document

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!-- library.xsd --&gt;
&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;xs:element name="library"&gt;
    &lt;xs:complexType&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="book" maxOccurs="unbounded"&gt;
          &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
              &lt;xs:element name="title" type="xs:string"/&gt;
              &lt;xs:element name="author" type="xs:string"/&gt;
              &lt;xs:element name="year" type="xs:gYear"/&gt;
            &lt;/xs:sequence&gt;
            &lt;xs:attribute name="isbn" type="xs:string" use="required"/&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
&lt;/xs:schema&gt;

&lt;!-- catalog.xml --&gt;
&lt;?xml version="1.0"?&gt;
&lt;library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="library.xsd"&gt;
  &lt;book isbn="978-0-13-468599-1"&gt;
    &lt;title&gt;Effective XML&lt;/title&gt;
    &lt;author&gt;Elliotte Rusty Harold&lt;/author&gt;
    &lt;year&gt;2003&lt;/year&gt;
  &lt;/book&gt;
&lt;/library&gt;
</pre>

</body>
</html>
AspectDTDXML Schema (XSD)
SyntaxIts own compact, non-XML grammarWritten in XML itself
Data typesNone — everything is essentially textRich built-in types: integers, dates, decimals...
Namespace supportVery limitedFull namespace awareness
ReusabilityMinimal — hard to composeSupports types, inheritance, and reusable groups
ReadabilityTerse, quick to skimVerbose but explicit and tool-friendly
Note: Tip: Most XML tooling — browsers, IDEs, and command-line validators like xmllint — check well-formedness automatically and for free. Schema validation against a DTD or XSD is usually an extra, explicit step, so don't assume a document is valid just because it opened without an error.

Exercise: XML Validator

What are the two things an XML validator can check for a document?