Skip to main content

XML Validation

An XML document is valid if its contents match against a Document Type Declaration (DTD) or a XML Schema. This implies that the document must have a correct syntax and all the constraints defined in the schema are satisfied.

Well Formed XML Documents

An XML document is well-formed if its syntax is valid, i.e. if it satisfies the following rules:

  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML attribute values must be quoted
<?xml version="1.0"  encoding="UTF-8"?>  
<mail>
<to>Alice</to>
<from>Bob</from>
<subject>Wow!</subject>
<body>Don't forget me this weekend!</body>
</mail>
note

Remember that the XML parser is responsible for validating XML documents,

XML Documents with Errors

Errors in XML documents will stop your XML applications.

W3C specification states that a program should stop processing an XML document if it finds an error.

Valid vs Well Formed XML Document

A well formed XML document is different from a valid XML document.

A valid XML document must be well formed and must conform to a Document Type Definition (DTD) or to a XML Schema

note

Both DTD and XML Schema define the rules and legal elements and attributes for an XML document.

Learn more with our XML DTD Tutorial and XML Schema Tutorial