XML Schema (XSD)
XML Schema (XSD) is a more powerful, modern alternative to DTDs for validating XML, itself written in XML and supporting rich data types.
Why XSD Improved on DTD
Unlike DTDs, XSD is itself written in valid XML syntax, so the same tools that process XML can process schemas. It also supports proper data types — integers, dates, decimals — rather than treating everything as plain text.
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="course" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Common Mistakes
- Using xs:string for a value that should be strictly typed as a number or date, losing the benefit of type validation.
- Forgetting that XSD requires elements to appear in the exact sequence declared by xs:sequence, unless a different model group is used.
- Assuming DTD and XSD syntax are interchangeable — they are entirely different formats for the same general purpose.
- Overcomplicating a simple document's schema when a lighter DTD or even no schema at all would suffice.
Professional Tip
Because XSD is written in XML itself, you get the benefit of consistent tooling — the same editors, validators, and parsers that understand XML can work with your schemas directly, unlike the separate syntax DTDs require.
Your Turn
Write an XSD complexType definition for a <product> element with a string name and a decimal price, then describe how it improves on an equivalent DTD.
Mini Quiz
What is a key advantage of XSD over DTD?