ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

XML Tutorial• 6 min read

DTD: Document Type Definition Validation

Well-formed vs Valid XML: Defining allowed tags, sequences, and cardinalities

A DTD (Document Type Definition) defines the legal building blocks of an XML document, verifying that elements, attributes, and nesting rules are strictly followed.

Well-Formed vs Valid XML

Well-Formed XML: Meets basic XML syntax rules (closed tags, single root, quoted attributes).

Valid XML: A well-formed XML document that also complies with an explicit DTD or XSD Schema.

DTD syntax: <!ELEMENT note (to, from, heading, body)> enforces exact ordering and child elements.

Cardinality operators: + (one or more), * (zero or more), ? (zero or one).

Internal DTD Definition Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
  <!ELEMENT note (to, from, heading, body)>
  <!ELEMENT to (#PCDATA)>
  <!ELEMENT from (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body (#PCDATA)>
]>
<note>
  <to>Linus</to>
  <from>Ada</from>
  <heading>Kernel Merge</heading>
  <body>Branch 4.2 has been pulled successfully.</body>
</note>
The parser validates that note contains exactly to, from, heading, body in order.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

In XML, what is the difference between "Well-Formed" and "Valid"?