XML with the correct syntax is referred to as "well-formed" XML.
The XML that is validated through a DTD is "legitimate" XML.
Well-written XML document
A "well-formed" XML document has the correct syntax.
A "well-formed" XML document adheres to the XML syntax rules described in the previous chapters:
The XML document must have a root element
The XML document must have a close tag
XML tags are case sensitive
XML elements must be nested correctly
XML attribute must be quoted
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<body>Don't forget the meeting this weekend!</body>
</note>
Validating an XML document
A legitimate XML document is a "well-formed" XML document that also adheres to the syntax rules of the document type definition (DTD):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>George</to>
<from>John</from>
<body>Don't forget the meeting this weekend!</body>
</note>
In the example above, the DOCTYPE declaration is a reference to an external DTD file. The following paragraphs show the contents of this file.
XML DTD
The role of a DTD is to define the structure of an XML document. It uses a range of legitimate 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)>
]>
XML Schema
The consortium supports an xml-based DTD substitute, which is named XmlSchema:
<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>
A generic Validator
To help you examine the syntax of the XML file, we created this tool so that you can check any XML file for syntax.