An Introduction to XML Schemas
(Page 1 of 7 )
XML Schemas help you control what elements appear in your XML documents. Similar to Document Type Definitions, they offer distinct advantages over the older syntax. Dan Wellman provides a basic overview, with examples to get you started working on your own.
Support file for this article available here.
XML Schemas are an area of XML syntax that is very similar in some ways to DTDs or Document Type Definitions; they govern, among other things, which elements should appear, and in which order, in your XML documents. They haven’t been around as long as DTDs; the XML Schema specification didn’t become an official W3C recommendation until May 2001, whereas DTDs were originally invented to work with SGML, or Standard General Markup Language (the metalanguage that most other markup languages are subsets of.)
The main differences between DTDs and Schemas are that XML Schemas are written in XML syntax and DTDs are not, and that Schemas are always external documents whereas DTDs can be internal or external. Aside from these two things, their purpose and function are fairly similar. A lot of people say that one of the main benefits of Schemas is that they are written in XML, and while it is true that you don’t have to learn a new language (assuming you already know a bit about XML), DTD language itself is not that difficult to master.
So why use a Schema instead of a DTD? The XML Schema specification offers everything a DTD can do; this is necessary so that existing DTDs can be rewritten as Schemas, but they also offer more in addition to that--they have a wider range of data types than DTDs, you can create custom data types, and Schemas are namespace aware whereas DTDs are not. DTDs are useful when dealing with elements containing text, but schemas offer far more control when dealing with documents whose elements contain letters and numbers.
Additionally, DTDs and Schemas are basically formatting structures, and one of the reasons for using XML is to separate data from formatting instructions. Only ever being able to use Schemas externally goes a long way to ensuring that this separation is maintained. Ultimately, most XML authoring software offers a DTD-to-Schema Conversion, but I haven’t seen any that want to do it the other way round for you.
Next: Choose Your Schema >>
More XML Articles
More By Dan Wellman