XML schema to help you model (1)

Source: Internet
Author: User
Tags date define contains documentation string web services xmlns zip
The XML XML schema is the recommended standard of the World Wide Web Consortium, which was officially released in May 2001, and has now finally been laid down for years of large-scale discussion and development, becoming the preferred data modeling tool in the globally recognized XML environment.

Because XML is a subset of SGML (the standard Universal Markup Language), it also inherits SGML's DTD for modeling. The advantage of using DTDs is that you can leverage a large number of existing DTD tools to keep the cost of development applications at a relatively low level. However, DTDs have many drawbacks:

1. DTDs are based on regular expressions and have limited descriptive capabilities;

2. DTDs are not supported by data types and lack capacity in most application environments;

3. The DTD has insufficient constraints to define the XML instance document in more detail;

4. DTDs are not structured enough, and the cost of reuse is relatively high;

5. DTDs do not use XML as a descriptive tool, and DTD construction and access does not have a standard programming interface for DTD maintenance using standard programming methods.

The XML Schema is designed for the shortcomings of these DTDs, and it uses XML as a descriptive tool with strong descriptive, scalability, and maintenance capabilities.


Introduction to XML schemas


The primary purpose of an XML schema is to define a class of XML documents (an XML application). Thus, the pattern's "instance Document" form is often used to describe an XML document that is consistent with a particular XML schema. In fact, document instances and schema documents do not have to exist in the form of documents, they can exist in the form of a stream of bytes passed between applications, or as a collection of database records and XML "information items." However, to simplify getting started, we always think of instances and schemas as documents or files, and assume that they are always in the form of document instances or schema documents.

Here we will combine an example to make a simple overview of the XML schema, and I hope that you can read this section with an initial grasp of the use method and the specific semantics of an XML Schema document instance. XML Schema is a basic tool to be used in Web services technology, but not all the attributes of XML Schema are widely used, so this article will not introduce the XML Schema specification systematically.

Before introducing the XML Schema syntax, consider an XML instance document Po.xml. It describes a home product purchase order generated by a home product purchase/payment application (see Code 1).

<!--code 1 Po.xml, purchase order XML Instance document-->
<?xml version= "1.0"?>
<purchaseorder orderdate= "1999-10-20" >
<shipto country= "US" >
<name>alice smith</name>
<street>123 Maple street</street>
<city>mill valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billto country= "US" >
<name>robert smith</name>
<street>8 Oak avenue</street>
<city>old town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>hurry, my lawn is going wild!</comment>
<items>
<item partnum= "872-aa" >
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>confirm this is electric</comment>
</item>
<item partnum= "926-aa" >
<productname>baby monitor</productname>
<quantity>1</quantity>
<USPrice>39.98</USPrice>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>

This purchase order consists of an element purchaseorder and its child elements shipto, BillTo, comment, and items. These child elements (except comment) also contain some other child elements in turn.

The leaf element contains a number instead of any child elements, such as Usprice. An element is called a compound type if it contains child elements or is a property, whereas an element is called a simple type if it contains only numbers, strings, or other data, but does not contain any child elements. In this instance document, the compound type and some simple types are defined in the schema document of the purchase order, while other standard simple types appear as simple types built into the XML schema.

Before studying the purchase order mode document for this instance document, let's first describe the link between the purchase order instance document and the schema document. An instance document does not necessarily need to refer to a schema document, and of course, in fact, many instance documents do refer to the schema document, and in order to make it easier to get started, we choose not to reference it at first. Also assume that the processor of any instance document can correctly handle the purchase order mode document even if it does not get any information from the purchase order instance document.


Purchase Order Mode document


In code 2, a schema document for the purchase order is given. File po.xsd before giving an explanation, you can use the knowledge of XML to try to understand this schema document.

<!--code 2 PO.xsd, purchase order Schema document-->
<xsd:schema xmlns:xsd= "Http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang= "en" >
Purchase Order schema for example.com.
Copyright example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>
<xsd:element name= "PurchaseOrder" type= "Purchaseordertype"/>
<xsd:element name= "comment" type= "xsd:string"/>
<xsd:complextype name= "Purchaseordertype" >
<xsd:sequence>
<xsd:element name= "ShipTo" type= "USAddress"/>
<xsd:element name= "BillTo" type= "USAddress"/>
<xsd:element ref= "comment" minoccurs= "0"/>
<xsd:element name= "Items" type= "items"/>
</xsd:sequence>
<xsd:attribute name= "OrderDate" type= "Xsd:date"/>
</xsd:complexType>
<xsd:complextype name= "USAddress" >
<xsd:sequence>
<xsd:element name= "name" type= "xsd:string"/>
<xsd:element name= "Street" type= "xsd:string"/>
<xsd:element name= "City" type= "xsd:string"/>
<xsd:element name= "state" type= "xsd:string"/>
<xsd:element name= "Zip" type= "Xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name= "Country" type= "Xsd:nmtoken"
fixed= "US"/>
</xsd:complexType>
<xsd:complextype name= "Items" >
<xsd:sequence>
<xsd:element name= "item" minoccurs= "0" maxoccurs= "unbounded" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name= "ProductName" type= "xsd:string"/>
<xsd:element name= "Quantity" >
<xsd:simpleType>
<xsd:restriction base= "Xsd:positiveinteger" >
<xsd:maxexclusive value= "/>"
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name= "Usprice" type= "Xsd:decimal"/>
<xsd:element ref= "comment" minoccurs= "0"/>
<xsd:element name= "ShipDate" type= "xsd:date" minoccurs= "0"/>
</xsd:sequence>
<xsd:attribute name= "Partnum" type= "SKU" use= "required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!--keeping unit, a code for identifying products-->
<xsd:simpletype name= "SKU" >
<xsd:restriction base= "Xsd:string" >
<xsd:pattern value= "\d{3}-[a-z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

The purchase order Mode document consists of a schema element and a series of child elements, most of which are element, complextype, and simpletype, which determine how and what the elements in the instance document are, and it is best to memorize the element, ComplexType, simpletype, these are the few elements that we'll always need to use.

Also, you can declare xmlns:xsd= "http://www.w3.org/2001/XMLSchema" by using a namespace declaration that appears in the schema element, so that each element in the schema document has one associated with the XML The namespace prefix "xsd:" associated with the schema namespace. Although it is syntactically possible to use any prefix form, the namespace prefix "xsd:" is contracted to represent the XML Schema namespace. Because the same prefix is used, the same association appears in the built-in simple type name, such as Xsd:string. The purpose of this form association is to indicate that the current element or simple type belongs to the built-in definition of the XML Schema language, not to the author's own vocabulary of the schema document. To make it clear and simple here, we only mention the names of the elements and the simple type names, ignoring their prefix "xsd:".



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.