XML - Validation
Validation (in XML) means checking whether an XML document follows the rules defined in a DTD (Document Type Definition) or an XSD (XML Schema Definition).
-
Well-formed XML:
-
Basic requirement: the XML follows the syntax rules (tags are properly opened/closed, attributes quoted, etc.).
-
Example:
<book><title>XML Guide</title></book>
is well-formed.
-
-
Valid XML:
-
Goes one step further: the document not only is well-formed, but it also conforms to a specific structure described in a DTD or XSD.
-
A DTD/XSD acts like a blueprint or contract for the XML.
-
Validation checks:
-
Correct element order and nesting
-
Allowed/required attributes
-
Data types of values (especially with XSD, which supports types like integer, date, etc.)
-
-
Example with DTD
DTD:
<!DOCTYPE book [
<!ELEMENT book (title, author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
]>
Valid XML:
<book>
<title>XML Guide</title>
<author>A. Smith</author>
</book>
Invalid XML (missing author
):
<book>
<title>XML Guide</title>
</book>
Example with XSD
XSD allows stricter rules (like data types):
<xs:element name="price" type="xs:decimal"/>
Valid XML: <price>19.99</price>
Invalid XML: <price>abc</price>
In short:
-
Validation = ensuring XML follows the structure and rules set by a DTD or XSD.
-
This makes XML data reliable, consistent, and easier to exchange between systems.