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

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

current pageTo page 2To page 3To page 4
[next]

Professional XML Schemas

Example Schema: Delivery Receipt

[The following is a continuation of our series of excerpts from Chapter 1 of the Wrox Press title, Professional XML Schemas - Ed.]

In this example, we'll see how to create a schema for a delivery receipt called DeliveryReceipt.xsd. The schema contains constructs for names, addresses, and delivery items.

The delivery receipt is held within a root element called DeliveryReceipt, which has two attributes, deliveryID and dateReceived. The customer's name and address are then held within an element called Customer. Finally, the delivered items will be held within an Items element.

Here is a sample document marked up according to the DeliveryReceipt.xsd schema called DeliveryReceipt.xml:

<?xml version = "1.0" ?>
<DeliveryReceipt deliveryID = "44215" dateReceived = "2001-04-16" 
  xsi:noNamespaceSchemaLocation = "http://file_Location/DeliveryReceipt.xsd"
  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
   <Customer>
      <Name>
         <FirstName>Ray</FirstName>
         <MiddleInitial>G</MiddleInitial>
         <LastName>Bayliss</LastName>
      </Name>
      <Address>
         <AddressLine1>10 Elizabeth Place</AddressLine1>
         <AddressLine2></AddressLine2>
         <Town>Paddington</Town>
         <City>Sydney</City>
         <StateProvinceCounty>NSW</StateProvinceCounty>
         <ZipPostCode>2021</ZipPostCode>
      </Address>
   </Customer>
   <Items>
      <DeliveryItem quantity = "2">
         <Description>Small Boxes</Description>
      </DeliveryItem>
   </Items>
</DeliveryReceipt>

Note how we indicate to a parser that it will be able to find a schema to validate the document using the xsi:noNamespaceSchemaLocation attribute in the root element. We use this because the constructs in the schema do not belong to a namespace. In order to use this attribute, we also need to declare the namespace for the XML Schema for instance documents:

<DeliveryReceipt deliveryID = "44215" dateReceived = "2001-04-16" 
  xsi:noNamespaceSchemaLocation = "http://file_Location/DeliveryReceipt.xsd"
  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">

current pageTo page 2To page 3To page 4
[next]

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


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