When two organizations agree to exchange business documents using XML, one of the issues they need to resolve is the definition of the documents they’ll exchange. For example, because Northwind Traders is a supplier to many businesses, Northwind Traders and its customers would have to establish the definition of and rules for working with a catalog document listing the goods available for order before they could build a working instance of a catalog. Among the questions they would need to ask are the following:
Of course, once the application developers had decided to use XML, they’d need to consider some more specific questions:
The answers to these sorts of questions form a schema, the purpose of which is to provide to developers a shared definition of what a particular type of XML document should look like. In this way, different organizations can all exchange documents that adhere to the same schema, allowing them to build XML-enabled applications to validate their documents and process them in a consistent fashion.
Schemas are the intended replacement for the older Document Type Definition (DTD). DTDs define XML documents in much the same way that schemas do but have limited support for data types and are written using non-XML syntax, which means that XML developers need to learn two different syntaxes and that XML parsers need to be able to parse DTDs as well as XML.
The World Wide Web Consortium (W3C) is the governing body responsible for controlling standards on the Internet. In fact, XML itself is a W3C standard. As I’m writing this chapter, the standard for the syntax to be used in XML schemas is in the process of being approved. In the meantime, Microsoft and other companies have proposed to the W3C that XML-Data syntax be used to define schemas for use in document exchange scenarios today.
XML-Data Reduced (XDR) is a subset of the XML-Data syntax that can be used to define XML schemas. Microsoft XML-aware products, including SQL Server and the Microsoft XML parser, support the XDR specification for schemas.
It’s not clear when the final XML schema standard will be approved by the W3C, and when it is, the standard is likely to be semantically different from the XML-Data proposal. Microsoft has committed to supporting the eventual W3C standard in all its XML-based products and technologies, including SQL Server. In the meantime, Microsoft products use the XDR for schema definitions, and it’s expected that any support added for the XML schema standard will be complemented by backward-compatibility for XDR.
So what does an XDR schema look like? Let’s take a simple example. Suppose a catalog schema must be defined for Northwind Traders. It might look something like the following example:
<?xml version="1.0"?> <Schema name="NWCatalogSchema" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <ElementType name="Description" dt:type="string"/> <ElementType name="Price" dt:type="fixed.14.4"/> <ElementType name="Product" model="closed"> <AttributeType name="ProductCode" dt:type="string"/> <attribute type="ProductCode" required="yes"/> <element type="Description" minOccurs="1" maxOccurs="1"/> <element type="Price" minOccurs="1" maxOccurs="1"/> </ElementType> <ElementType name="Category" model="closed"> <AttributeType name="CategoryID" dt:type="string"/> <AttributeType name="CategoryName" dt:type="string"/> <attribute type="CategoryID" required="yes"/> <attribute type="CategoryName" required="yes"/> <element type="Product" minOccurs="1" maxOccurs="*"/> </ElementType> <ElementType name="Catalog" model="closed"> <element type="Category" minOccurs="1" maxOccurs="1"/> </ElementType> </Schema>
You can also find this schema in the Demos\Chapter6\Schemas folder on the companion CD in a file named CatalogSchema.xml. This schema answers the questions posed earlier by defining the elements and attributes that you can use in a catalog document. It also uses the datatypes namespace to define the data types of the elements and attributes. Finally, by specifying a closed model for the elements, the schema ensures that only the elements defined in the schema can be used in a catalog document. Northwind Traders and its trading partners could use this schema to validate documents, such as the one shown here:
<?xml version="1.0"?> <Catalog xmlns= "x-schema:http://www.northwindtraders.com/schemas/CatalogSchema.xml"> <Category Category CategoryName="Beverages"> <Product ProductCode="1"> <Description>Chai</Description> <Price>18</Price> </Product> <Product ProductCode="2"> <Description>Chang</Description> <Price>19</Price> </Product> </Category> <Category Category CategoryName="Condiments"> <Product ProductCode="3"> <Description>Aniseed Syrup</Description> <Price>10</Price> </Product> </Category> </Catalog>
For more information about XDR schemas, visit the XDR Schema Developer’s Guide section of the MSDN Web site at http://msdn.microsoft.com/library/psdk/xmlsdk/xmlp7k6d.htm.