WebReference.com - Part 4 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (3/4) | WebReference

WebReference.com - Part 4 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (3/4)

To page 1To page 2current pageTo page 4
[previous] [next]

Professional XML Schemas

Let's take a closer look at the schema. We start off declaring the namespace for XML Schema, which we use to prefix all of the elements defined by the XML Schema Recommendation:

<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">

We then define the root element DeliveryReceipt. Because it contains Customer and Items element elements (as opposed to being a text-only element), we have had to associate it with complex type using the complexType element. This also contains a sequence compositor, requiring that the Customer element appear before the Items element.

Between the closing sequence and complexType elements, we declare the two attributes that are carried by the DeliveryReceipt element: deliveryID, whose type is an integer, and dateReceived, whose type is a date type:

   <xs:element name = "DeliveryReceipt">
      <xs:complexType>
         <xs:sequence>
            <xs:element name = "Customer">
               ...
            </xs:element>
            <xs:element name = "Items">
               ...
            </xs:element>
         <xs:attribute name = "deliveryID" type = "xs:integer" />
         <xs:attribute name = "dateReceived" type = "xs:date" />
      </xs:complexType>
   </xs:element>

Inside the declaration of the DeliveryReceipt element we have a declaration of the Customer and Items elements. Both Customer and Items contain child elements, so we need to use a complexType element inside each of them, along with a compositor, which is the sequence element, to indicate the order in which they can appear. Customer and Items are made up of references to globally declared elements using the ref attribute:

            <xs:element name = "Customer">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element ref = "Name" />
                     <xs:element ref = "Address" />
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
            <xs:element name = "Items">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element ref = "DeliveryItem" 
                                 minOccurs = "1" maxOccurs = "unbounded"/>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>

We have already seen how we defined the Name and Address elements earlier in the chapter. The third globally declared element is the DeliveryItem element, which can occur one or more times. Note that we had to declare the occurrence constraints on the reference to the element, however, because you cannot add them to global declarations.

The DeliveryItem element also holds a quantity element, which is declared between the closing sequence and complexType elements. The quantity attribute has a type of integer:

   <xs:element name = "DeliveryItem">
      <xs:complexType>
         <xs:sequence>
            <xs:element name = "Description"  type = "xs:string" />
         </xs:sequence>
         <xs:attribute name = "quantity" type = "xs:integer" />
      </xs:complexType>
   </xs:element>

We specify the simple built-in string datatype on the Description element to restrict the allowable content of text-only elements; if we did not associate them with a type they could hold any well-formed combination of elements, attributes and characters that we had defined in the schema.


To page 1To page 2current pageTo page 4
[previous] [next]

Created: October 30, 2001
Revised: October 30, 2001


URL: http://webreference.com/authoring/languages/xml/schemas/chap1/4/3.html