Introduction
to the W3C XML Schema Recommendation
In May of 2001, the W3C
finalized its recommendation for the XML Schema Definition Language. This
standard allows an author to define simple and complex elements and the rules
governing how those elements and their attributes may show up within an instance
document. The author has a large amount of control over how the structure of a
conform-ing XML document must be created. The author can apply various
restrictions to the ele-ments and attributes within the document, from
specifying the length to specifying an enumerated set of acceptable values for
the element or attribute. With the XML Schema Definition Language, an XML
schema author possesses an incredible amount of control over the conformance of
an associated XML document to the specified schema. You can find more
information on the W3C at www.w3c.org. Additionally, the W3C XML Schema recommendation is separated into
three main documents:
• The XML Schema Primer (Part 0), which can be found at http://www.w3c.org/TR/xmlschema-0/.
• The XML Schema Structures (Part 1), which can be found at http://www.w3c.org/TR/xmlschema-1/.
• The XML Schema Data Types (Part 2), which can be found at http://www.w3c.org/TR/xmlschema-2/.
You can find additional
information on the XML Schema Definition Language at http://www.w3c.org/XML/Schema.
Sample
XML Document
The rest of this chapter is
devoted to creating and understanding the XML schema for the XML document shown
in Listing 4.1, which details a purchase order for various items that can
commonly be found in a grocery store. This document allows one individual to
receive the shipment of the goods and an entirely different individual to pay
for the pur-chase. This document also contains specific information about the
products ordered, such as how much each product is, how many were ordered, and
so on.
LISTING 4.1 PurchaseOrder.xml Contains
a Sample Purchase Order for Common Items
Found in a Grocery Store
<PurchaseOrder Tax=”5.76”
Total=”75.77”>
<ShippingInformation>
<Name>Dillon Larsen</Name> <Address>
<Street>123 Jones
Rd.</Street> <City>Houston</City>
<State>TX</State> <Zip>77381</Zip>
</Address>
<Method>USPS</Method>
LISTING 4.1 continued
<DeliveryDate>2001-08-12</DeliveryDate>
</ShippingInformation>
<BillingInformation>
<Name>Madi Larsen</Name> <Address>
<Street>123 Jones
Rd.</Street> <City>Houston</City>
<State>TX</State> <Zip>77381</Zip>
</Address>
<PaymentMethod>Credit
Card</PaymentMethod> <BillingDate>2001-08-09</BillingDate>
</BillingInformation>
<Order SubTotal=”70.01” ItemsSold=”17”>
<Product Name=”Baby
Swiss” Id=”702890” Price=”2.89”
➥ Quantity=”1”/>
<Product Name=”Hard
Salami” Id=”302340” Price=”2.34”
➥ Quantity=”1”/>
<Product Name=”Turkey”
Id=”905800” Price=”5.80”
➥ Quantity=”1”/>
<Product Name=”Caesar
Salad” Id=”991687” Price=”2.38”
➥ Quantity=”2”/>
<Product Name=”Chicken
Strips” Id=”133382” Price=”2.50”
➥ Quantity=”1”/>
<Product Name=”Bread”
Id=”298678” Price=”1.08”
➥ Quantity=”1”/>
<Product Name=”Rolls”
Id=”002399” Price=”2.24”
➥ Quantity=”1”/>
<Product Name=”Cereal”
Id=”066510” Price=”2.18”
➥ Quantity=”1”/>
<Product Name=”Jalapenos” Id=”101005”
Price=”1.97”
➥ Quantity=”1”/>
<Product Name=”Tuna”
Id=”000118” Price=”0.92”
➥ Quantity=”3”/>
<Product Name=”Mayonnaise” Id=”126860”
Price=”1.98”
➥ Quantity=”1”/>
<Product Name=”Top
Sirloin” Id=”290502” Price=”9.97”
➥ Quantity=”2”/>
<Product Name=”Soup”
Id=”001254” Price=”1.33”
➥ Quantity=”1”/>
<Product Name=”Granola
Bar” Id=”026460” Price=”2.14”
➥ Quantity=”2”/>
<Product Name=”Chocolate Milk”
Id=”024620” Price=”1.58”
➥ Quantity=”2”/>
<Product Name=”Spaghetti” Id=”000265”
Price=”1.98”
➥ Quantity=”1”/>
LISTING 4.1 continued
<Product Name=”Laundry
Detergent” Id=”148202” Price=”8.82”
➥ Quantity=”1”/> </Order>
</PurchaseOrder>
As you can see, Listing 4.1
represents a fairly small and simple order that could be placed online. It
contains the necessary information regarding how payment is to be made, how the
order is to be shipped, and on what day the order is to be delivered. The
preceding listing should by no means be construed as an all-inclusive document
for an online grocery store order; it has been constructed for use as an
example within this book only.
Until the XML Schema
Definition Language recommendation was finalized, most authors, in the face of
ever-changing standards, decided to stick with a finalized standard of DTDs.
For the preceding listing, an author might construct the DTD shown in
Listing 4.2.
LISTING 4.2 PurchaseOrder.dtd Contains
a Sample DTD for PurchaseOrder.xml
<?xml version=”1.0”
encoding=”UTF-8”?>
<!ELEMENT PurchaseOrder
(ShippingInformation, BillingInformation, Order)> <!ATTLIST PurchaseOrder
Tax CDATA #IMPLIED Total
CDATA #IMPLIED
>
<!ELEMENT ShippingInformation (Name,
Address, (((BillingDate,
➥ PaymentMethod)) |
((DeliveryDate, Method))))>
<!ELEMENT BillingInformation (Name,
Address, (((BillingDate,
➥ PaymentMethod)) | ((DeliveryDate,
Method))))> <!ELEMENT Order (Product+)>
<!ATTLIST Order
SubTotal CDATA
#IMPLIED
ItemsSold CDATA
#IMPLIED>
<!ELEMENT Name
(#PCDATA)>
<!ELEMENT Address (Street,
City, State, Zip)> <!ELEMENT BillingDate (#PCDATA)>
<!ELEMENT PaymentMethod
(#PCDATA)> <!ELEMENT DeliveryDate (#PCDATA)> <!ELEMENT Method
(#PCDATA)> <!ELEMENT Product EMPTY>
<!ATTLIST Product Name
CDATA #IMPLIED Id CDATA #IMPLIED
LISTING 4.2 continued
Price CDATA #IMPLIED Quantity
CDATA #IMPLIED
>
<!ELEMENT Street
(#PCDATA)> <!ELEMENT City (#PCDATA)> <!ELEMENT State (#PCDATA)>
<!ELEMENT Zip (#PCDATA)>
Schema
for XML Document
So, now that you’ve seen the
DTD for the XML document in Listing 4.1, what would the comparative XML schema
for it look like? Although the DTD in Listing 4.2 manages to describe the XML
document in Listing 4.1 in a total of 30 lines, creating an XML schema is not
quite so easy. Given the document in Listing 4.1, the XML schema for it is
shown in Listing 4.3.
LISTING 4.3 PurchaseOrder.xsd Contains
the Schema Definition for
PurchaseOrder.xml
<xsd:schema
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:annotation>
<xsd:documentation>
Purchase Order schema for an
online grocery store. </xsd:documentation>
</xsd:annotation>
<xsd:element name=”PurchaseOrder” type=”PurchaseOrderType”/>
<xsd:complexType
name=”PurchaseOrderType”> <xsd:all>
<xsd:element name=”ShippingInformation”
type=”InfoType” ➥ minOccurs=”1”
maxOccurs=”1”/>
<xsd:element
name=”BillingInformation” type=”InfoType” ➥ minOccurs=”1”
maxOccurs=”1”/>
<xsd:element name=”Order”
type=”OrderType” ➥ minOccurs=”1”
maxOccurs=”1”/>
</xsd:all>
<xsd:attribute
name=”Tax”> <xsd:simpleType>
<xsd:restriction
base=”xsd:decimal”> <xsd:fractionDigits value=”2”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name=”Total”>
<xsd:simpleType>
<xsd:restriction
base=”xsd:decimal”> <xsd:fractionDigits value=”2”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
<xsd:group
name=”ShippingInfoGroup”> <xsd:all>
<xsd:element
name=”DeliveryDate” type=”DateType”/> <xsd:element name=”Method”
type=”DeliveryMethodType”/>
</xsd:all>
</xsd:group>
<xsd:group name=”BillingInfoGroup”>
<xsd:all>
<xsd:element
name=”BillingDate” type=”DateType”/> <xsd:element name=”PaymentMethod”
type=”PaymentMethodType”/>
</xsd:all>
</xsd:group>
<xsd:complexType
name=”InfoType”> <xsd:sequence>
<xsd:element name=”Name”
minOccurs=”1” maxOccurs=”1”> <xsd:simpleType>
<xsd:restriction
base=”xsd:string”/> </xsd:simpleType>
</xsd:element>
<xsd:element
name=”Address” type=”AddressType” minOccurs=”1” ➥ maxOccurs=”1”/>
<xsd:choice minOccurs=”1”
maxOccurs=”1”> <xsd:group ref=”BillingInfoGroup”/> <xsd:group
ref=”ShippingInfoGroup”/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType
name=”DateType”> <xsd:restriction base=”xsd:date”/>
</xsd:simpleType>
<xsd:simpleType
name=”DeliveryMethodType”> <xsd:restriction base=”xsd:string”>
<xsd:enumeration
value=”USPS”/>
<xsd:enumeration
value=”UPS”/>
<xsd:enumeration
value=”FedEx”/>
<xsd:enumeration
value=”DHL”/>
<xsd:enumeration
value=”Other”/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType
name=”PaymentMethodType”> <xsd:restriction base=”xsd:string”>
<xsd:enumeration
value=”Check”/> <xsd:enumeration value=”Cash”/> <xsd:enumeration
value=”Credit Card”/>
<xsd:enumeration
value=”Debit Card”/> <xsd:enumeration value=”Other”/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType
name=”AddressType”> <xsd:all>
<xsd:element name=”Street”
minOccurs=”1”> <xsd:simpleType>
<xsd:restriction
base=”xsd:string”/> </xsd:simpleType>
</xsd:element>
<xsd:element name=”City”
minOccurs=”1” maxOccurs=”1”> <xsd:simpleType>
<xsd:restriction
base=”xsd:string”/> </xsd:simpleType>
</xsd:element>
<xsd:element name=”State”
type=”StateType” minOccurs=”1” ➥ maxOccurs=”1”/>
<xsd:element name=”Zip”
type=”ZipType” minOccurs=”1” ➥ maxOccurs=”1”/>
</xsd:all>
</xsd:complexType>
<xsd:simpleType
name=”ZipType”> <xsd:restriction base=”xsd:string”>
<xsd:minLength
value=”5”/> <xsd:maxLength value=”10”/>
<xsd:pattern
value=”[0-9]{5}(-[0-9]{4})?”/> </xsd:restriction>
</xsd:simpleType>
<xsd:simpleType
name=”StateType”> <xsd:restriction base=”xsd:string”>
<xsd:length value=”2”/>
<xsd:enumeration value=”AR”/>
<xsd:enumeration
value=”LA”/>
<xsd:enumeration value=”MS”/>
<xsd:enumeration
value=”OK”/>
<xsd:enumeration
value=”TX”/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType
name=”OrderType”> <xsd:sequence>
<xsd:element
name=”Product” type=”ProductType” ➥ minOccurs=”1”
maxOccurs=”unbounded”/>
</xsd:sequence>
<xsd:attribute
name=”SubTotal”> <xsd:simpleType>
<xsd:restriction
base=”xsd:decimal”> <xsd:fractionDigits value=”2”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name=”ItemsSold”
type=”xsd:positiveInteger”/> </xsd:complexType>
<xsd:complexType
name=”ProductType”> <xsd:attribute name=”Name” type=”xsd:string”/>
<xsd:attribute name=”Id”
type=”xsd:positiveInteger”/> <xsd:attribute name=”Price”>
<xsd:simpleType>
<xsd:restriction
base=”xsd:decimal”> <xsd:fractionDigits value=”2”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute
name=”Quantity” type=”xsd:positiveInteger”/> </xsd:complexType>
</xsd:schema>
Examining the preceding XML
schema, you can see that defining a schema for a docu-ment can become fairly
complicated. However, for all the extra complexity involved, the schema gives
the author virtually limitless control over how an XML document can be
validated against it. For instance, you may notice the use of the <xsd:choice> element. You’ll learn later
in the “Model Groups” section of this chapter that this element can be used to
indicate when one of a group of elements or attributes may show up, but not
all, as is the case with the DeliveryDate and BillingDate attributes.
Also, notice the use of the xsd namespace. This namespace
can be anything, but for con-vention in this chapter, we’ll use xsd to indicate an XML Schema
Definition Language element.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.