Schemas are more powerful when validating an XML document because of their ability to clarify data types stored within the XML document. Because schemas can more clearly define the types of data that are to be contained in an XML document, they allow for a closer check on the accuracy of XML documents. Following example illustrates an XML schema for a sample purchase order.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:PO="http://www.plastics_supply.com/PurchaseOrder" targetNamespace="http://www.plastics_supply.com/PurchaseOrder">
<!-- Purchase Order schema -->
<xsd:element name="PurchaseOrder" type="PO:PurchaseOrderType"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:all> <xsd:element name="ShippingInformation" type="PO:Customer" minOccurs="1" maxOccurs="1"/>
<xsd:element name="BillingInformation" type="PO:Customer" minOccurs="1" maxOccurs="1"/> <xsd:element name="Order" type="PO:OrderType" minOccurs="1" maxOccurs="1"/> </xsd:all> </xsd:complexType>
<xsd:complexType name="Customer">
<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="PO:AddressType" minOccurs= "1" maxOccurs="1"/> <xsd:choice> <xsd:element name="BillingDate" type="xsd:date"/>
<xsd:element name="ShippingDate" type="xsd:date"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AddressType">
<xsd:sequence>
<xsd:element name="Street" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="PostalCode" type="xsd:decimal"/> <xsd:sequence> </xsd:complexType>
<xsd:complexType name="OrderType">
<xsd:sequence> <xsd:element name="Product" type="PO:ProductType" minOccurs= "1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="Total"> <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="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>
It depicts a purchase order for various items. This document allows a customer to receive the shipment of the goods at the customer’s manufacturing plant and billing information to be sent to the customer’s headquarters. This document also contains specific information about the products ordered, such as how much each product cost, how many were ordered, and so on. The root element of an XML schema document, such as the purchase order schema, is always the schema element. Nested within the schema element are element and type declarations. For instance, the purchase order schema consists of a schema element and a variety of sub-elements, most notably element complexType and simpleType that determine the appearance of elements and their content in instance documents. These components are explained in the following sections. The schema element assigns the XML schema namespace ("http://www.w3.org/ 2001/XMLSchema") as the default namespace. This schema is the standard schema namespace defined by the XML schema specification and all XML schema elements must belong to this namespace. The schema element also defines the targetNamespace attribute, which declares the XML namespace of all new types explicitly created within this schema. The schema element is shown to assign the prefix PO to the targetNamespace attribute. By assigning a target namespace for a schema, we indicate that an XML document whose elements are declared as belonging to the schema’s namespace should be validated against the XML schema. Therefore, the PO targetNamespace can be used within document instances so that they can conform to the purchase order schema. As the purpose of a schema is to define a class of XML documents, the term instance document is often used to describe an XML document that conforms to a particular schema.
No comments:
Post a Comment