XML Validation


XML with correct syntax is called "well-formed" XML.

XML that passes DTD validation is "valid" XML.


Well-Formed XML Documents

A "well-formed" XML document has correct syntax.

The syntax rules described in previous chapters:

  • XML documents must have a root element
  • All 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="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Validating XML Documents

A valid XML document is a "well-formed" XML document that also conforms to the rules of a Document Type Definition (DTD):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

In the example above, the DOCTYPE declaration is a reference to an external DTD file. The following paragraph shows the contents of this file.


XML DTD

The purpose of a DTD is to define the structure of an XML document. It uses a series of legal elements to define the document structure:

<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

If you want to learn DTD, please visit ourHomepageto find the DTD tutorial.


XML Schema

W3C supports an XML-based alternative to DTD, named XML Schema:

<xs:element name="note">

<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:element>

If you want to learn XML Schema, please visit ourHomepageto find the Schema tutorial.


A Universal XML Validator

To help you check the syntax of XML files, we have created an XML validator so that you can perform syntax checks on any XML file.

See the next chapter.


Other Extensions