[Previous] [Contents] [Next]

A Schema for a Data-Oriented XML Document


The example that has been used up to this point has been a document-oriented XML document with no data types besides string. To see how the other data types work, in this section we'll create an example using the Northwind Traders database. (This database can be found in Microsoft Access, Visual Studio, and Microsoft SQL Server 7.) For the Customer and Categories tables, you could create the schema shown below.


  <?xml version ="1.0"?>
  <schema targetNamespace = "http://www.northwind.com/Category"
      xmlns = http://www.w3.org/1999/XMLSchema
      xmlns:Categories = "http://www.northwind.com/Category">
  <simpleType name="String15" source="string"
          <maxLength= "15" />
          <minLength = "0"/>
      </simpleType>
      <simpleType name="String5" base="string">
          <maxLength= "5"/ >
          <minLength = "0"/>
      </simpleType>
      <simpleType name="String30" base="string">
          <maxLength= "30" />
          <minLength = "0"/>
      </simpleType>
      <simpleType name="String60" base="string">
          <maxLength= "60" />
          <minLength = "0"/>
      </simpleType>
     <simpleType name="String10" base="string">
          <maxLength= "10" />
          <minLength = "0"/>
      </simpleType>
      <simpleType name="String24" base="string">
          <maxLength= "24" />
          <minLength = "0"/>
      </simpleType>
     <simpleType name="String40" base="string">
          <maxLength= "40" />
          <minLength = "0"/>
      </simpleType>

      <element name = "Categories">
          <complexType content = "elementOnly">
              <group>
              <sequence>
              <element ref = "Categories.CategoryID"
                       minOccurs = "1" maxOccurs = "1" />
              <element ref = "Categories.CategoryName"
                       minOccurs = "1" maxOccurs = "1" />
              <element ref = "Categories.Description"
                       minOccurs = "0" maxOccurs = "1" />
              <element ref = "Categories.Picture" minOccurs = "0"
                       maxOccurs = "1"/>
              </sequence>
              </group>
          </complexType>
      </element>

      <element name = "Categories.CategoryID" type = "integer">
          <annotation>
              <documentation>Number automatically assigned to a new
                             category
              </documentation>
          </annotation>
      </element>

      <element name = "Categories.CategoryName" type = "String15">
          <annotation>
              <documentation>Name of food category</documentation>
          </annotation>
      </element>

      <element name = "Categories.Description" type = "string"/>
      <element name = "Categories.Picture" type = "binary">
          <annotation>
              <documentation> Picture representing the food category
              </documentation>
          </annotation>
      </element>

      <element name = "Customers">
          <complexType content = "elementOnly">
              <group>
                  <sequence>
                      <element ref = "Customers.CustomerID"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.CompanyName"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.ContactName"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.ContactTitle"
                               minOccurs = "0" maxOccurs = "1"/>
                      <element ref = "Customers.Address"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.City" minOccurs = "1"
                               maxOccurs = "1"/>
                      <element ref = "Customers.Region"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.PostalCode"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.Country"
                               minOccurs = "1" maxOccurs = "1"/>
                      <element ref = "Customers.Phone" minOccurs = "1"
                               maxOccurs = "1"/>
                      <element ref = "Customers.Fax" minOccurs = "0"
                               maxOccurs = "1"/>
                  </sequence>
              </group>
          </complexType>
      </element>

      <element name = "Customers.CustomerID" type = "CustomerIDField">
          <annotation>
              <documentation>
                  Unique five-character code based on customer name
              </documentation>
          </annotation>
      </element>

      <element name = "Customers.CompanyName" type = "String5"/>
      <element name = "Customers.ContactName" type = "String40"/>
      <element name = "Customers.ContactTitle" type = "String30"/>
      <element name = "Customers.Address" type = "String60">
          <annotation>
              <documentation>Street or post-office box</documentation>
          </annotation>
      </element>

      <element name = "Customers.City" type = "String15"/>
      <element name = "Customers.Region" type = "String15">
          <annotation>
              <documentation>State or province</documentation>
          </annotation>
      </element>

      <element name = "Customers.PostalCode" type = "String10"/>
      <element name = "Customers.Country" type = "String15"/>
      <element name = "Customers.Phone" type = "String24">
          <annotation>
              <documentation>
                  Phone number includes country code or area code
              </documentation>
          </annotation>
      </element>
      <element name = "Customers.Fax" type = "String24">
          <annotation>
              <documentation>
                  Fax number includes country code or area code
              </documentation>
          </annotation>
      </element>
  </schema>

Notice that Categories and Customers have been used as prefixes to identify what objects the elements belong to. If you look in the Northwind Traders database, you'll see that the field data types and the lengths for character data types match those in the database. The comments that were included in the Northwind Traders database were also used in the schema. You can see that it's fairly easy to convert a database table into a schema.

Now that we have discussed schemas, we'll need to cover namespaces and schemas. In the following section, we'll examine how to use namespaces in schemas.


[Previous] [Contents] [Next]