Xml Schema

XmlSchema is also commonly known as WXS (W3C XML Schema). It is the first of many XML-specific SchemaLanguagesForXml, designed to overcome DocumentTypeDefinition (DTD) shortcomings.

The specification has two parts:

An instance (and file extension) for this WXS version of the schema for ExtensibleMarkupLanguage is XmlSchemaDefinition? (XSD).

XML Schema not only addresses the structure of the document, but also addresses datatypes. Built-in datatypes include string, date, time, duration of time, decimal, integer, float, double, boolean, etc. Also, you can derive your own datatypes. For instance, you could constrain the contents of the <postal-code> element to five (or nine) digits.


An Example of XmlSchema in action:

A simple instance document:

 <?xml version="1.0"?>
 <WorldGeniuses>
      <genius>
          <name>AlbertEinstein</name>
          <reason>E=mc^2</reason>
      </genius>
      <genius>
          <name>ThomasEdison</name>
          <reason>Lightbulb, Phonograph</reason>
      </genius>
      <genius>
          <name>BenjaminFranklin</name>
          <reason>Bifocals, Lightning is an electrical phenomenon, DayTimer?</reason>
      </genius>
 </WorldGeniuses>
An XmlSchema document that describes the above instance:

 <?xml version="1.0"?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:element name="name" type="xs:string" />
     <xs:element name="reason" type="xs:string" />
     <xs:element name="genius">
         <xs:complexType>
             <xs:sequence>
                 <xs:element ref="name" />
                 <xs:element ref="reason" />
             </xs:sequence>    
         </xs:complexType>    
     </xs:element>
     <xs:element name="WorldGeniuses">
         <xs:complexType>
             <xs:sequence>
                 <xs:element ref="genius" maxOccurs="unbounded" />
             </xs:sequence>    
         </xs:complexType>    
     </xs:element>
 </xs:schema>
A not obvious benefit of this particular XmlSchema is it will also validate documents with a different root element, provided they match the schema. For instance, the following document is also valid:

 <?xml version="1.0" ?>
 <genius>
    <name>WardCunningham</name>
    <reason>Inventor of the Wiki</reason>
 </genius>
Although this modular behavior can be very handy, it might not be what you want. You can rewrite the schema to forbid this type of behavior.


When do you write a schema? Only if you want to share a common definition with others (customers, developers, ...)? For the sake of verifying schema conformance? Why do I need a schema if I still need a program that knows what to do with the XML content? Can I automatically generate a database schema from an XML schema?

Schema are useful to make sure that all the users of a particular data set are playing by the same rules when transporting the data. XML is a storage and transport mechanism, not a means to verify the content of the data in its real context. In other words, schema are good for verifying that an integer is an integer, but not that the value is correct when compared to other values in the system.

One of my (stationary, on-hold) projects is a video test pattern generator design that uses XML to tell the generator how to establish a pattern. Each of the pattern parameters can be tested via schema to see if it makes sense, but a large number of parameters are interdependent. Only the application (the generator) knows the actual relationship of the parameters to each other; the schema can only know if they are the right type and within a certain range. In this case the transport mechanism (XML) is responsible only for making sure the data didn't get garbled from source to sink. The application still needs to compare all the data against itself in context to know if it is usable.

This does not mean that the XML schema is useless or redundant. It only means that the appropriate mechanism must be employed to guarantee data integrity at the level of examination under discussion.


Role of XmlSchema has changed

Reference article Oct2003 from ZapThink?: http://searchwebservices.techtarget.com/tip/1,289483,sid26_gci931774,00.html


See Also: SchemaLanguagesForXml, ProcessingMarkupLanguages


CategoryXml CategorySemanticWeb


EditText of this page (last edited May 14, 2009) or FindPage with title or text search