XML Validation


XML validation helps maintain data integrity and consistency. It ensures that an XML document:

  • Has correct syntax.
  • Follows a predefined structure.

There are two main ways to validate XML:
DTD (Document Type Definition)
XSD (XML Schema Definition).

Key Differences Between DTD and XSD

DTD (Document Type Definition)

DTD defines the structure of an XML document using a set of rules. It can be internal (within the XML file) or external (referenced separately).

Example of an XML Document Using DTD:

Let’s undestand the line <!DOCTYPE note SYSTEM “note.dtd”>

- <!DOCTYPE ...>
This defines the document type and associates it with a DTD.
- note
The root element of the XML document. All other elements must be contained within this root element. In this case, the root element is <note>.
- SYSTEM
Indicates that the DTD is external, meaning it is located in a separate file rather than being embedded inside the XML document.
- "note.dtd"
Refers to the external DTD file named note.dtd. The XML parser will use this file to validate the structure and content of the XML document.

Example of an External DTD File (note.dtd)

<!ELEMENT defines elements and their structure.
#PCDATA means Parsed Character Data (text content).