Perhaps the best way to illustrate the difference between a DTD and a schema is with an example - consider the following XML document, and its associated DTD. If I were to convert this DTD into an XML Schema, here's what it would look like: Let's dissect this a little: A schema is a well-formed XML document. It even begins with the standard XML document prolog, which specifies the XML version and document character set. The outermost element in a schema is the <xsd:schema> element, which includes a namespace declaration to associate the document with the XML Schema namespace. This namespace declaration tells the parser that elements specific to the schema will be declared with the "xsd:" prefix; this is necessary both to avoid clashes with user-defined element names, and to allow the parser to distinguish between schema instructions and non-schema elements. The outermost <xsd:schema> element is a container; it encloses the element and attribute definitions which constitute the validation rules of the schema. Don't worry about these for the moment, I'll be discussing them shortly. Once all the validation rules have ended, the document is closed with a Now, the schema itself is only one part of the puzzle - the other part is the document instance which will be validated against the schema. Typically, the two are linked together by adding a special attribute indicating the location of the schema to the XML document instance. Using the document instance above, we have The "xsi:noNamespaceSchemaLocation" attribute holds the location of the schema against which this document is to be validated. This attribute is used to validate specific document instances against a schema (which is why it comes from the XML Schema Instance namespace.) An alternative to this is the "xsi:schemaLocation" attribute, which specifies a schema location with a corresponding namespace location (a detailed explanation of this is beyond the scope of this tutorial, but take a look at the XML Schema specification for examples and more information.) In addition to these two methods, different XML processors may allow you to specify schema locations in different ways (for example, on the command line.) Finally, with your schema and document instance linked together, it's time to take them for a test drive. A good processor to use is XSV, an open-source XML Schema Validator from the University of Edinburgh and the W3C - you can download it from http://www.w3.org/XML/Schema#Tools. Alternatively, take a look at XMLSpy, a powerful and very cool XML editor available for download at http://www.xmlspy.com/, or visit the W3C's software page at http://www.w3.org/XML/Schema#Tools Here's what XSV has to say when I run it on my "movie.xml" document instance above:
blog comments powered by Disqus |