ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD ʵÀý

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

Schema (XSD)
Schema (XSD) ½éÉ..
ΪºÎʹÓà XML Sch..
ÈçºÎ¶¨ÖÆ XSD
XSD - <schema..
XSD ¼òµ¥ÔªËØ
XSD ÊôÐÔ
XSD Ô¼ÊøÃæ
XSD ¸´ºÏÔªËØ
XSD ¸´ºÏ¿ÕÔªËØ
XSD ¸´ºÏ´¿ÔªËØ
XSD ¸´ºÏ´¿Îı¾
XSD »ìºÏÄÚÈݵĸ´..
XSD ָʾÆ÷¸´ºÏÀà..
XSD <any> ..
XSD <anyAttri..
XSD ÔªËØÌæ´ú
XSD ʵÀý
XSD ×Ö·û´®Êý¾ÝÀà..
XSD ÈÕÆÚÊý¾ÝÀàÐÍ
XSD СÊýÊý¾ÝÀàÐÍ

XSD ʵÀý


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2007-08-28 ä¯ÀÀ:5087 :: ::

This chapter will demonstrate how to write an XML Schema. You will also learn that a schema can be written in different ways.
±¾Õ½ڽ«Ê¾·¶ÈçºÎÊéдһ·ÝXML Schema¡£Ä㻹»áѧϰµ½ÈçºÎʹÓò»Í¬µÄ·½·¨Êéдschema¡£


An XML Document
Ò»·ÝXMLÎĵµ

Let's have a look at this XML document called "shiporder.xml":
ÏÈÈÃÎÒÃÇ¿´¿´ÏÂÃæÕâ·ÝÃûΪ "shiporder.xml" µÄXMLÎĵµ£º

<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>

<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>

<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>

<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>

</item>
</shiporder>

The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid". The "shiporder" element contains three different child elements: "orderperson", "shipto" and "item". The "item" element appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price" element.
ÉÏÊöÕâ·ÝXMLÎĵµÖаüº¬Á˸ùÄ¿Â¼ÔªËØ"shiporder"£¬Ëü°üº¬ÁËÒ»¸ö±ØÒªÊôÐÔ"orderid"£»"shiporder" ÔªËØ°üº¬ÁËÈý¸ö²»Í¬µÄ×ÓÔªËØ£º"orderperson"¡¢"shipto" ºÍ "item"¡£"item"ÔªËØ³öÏÖÁËÁ½´Î£¬Ëü°üº¬ÁËÒ»¸ö"title"ÔªËØ¡¢Ò»¸öÈÎÒâµÄ"note"ÔªËØ¡¢Ò»¸ö"quantity"ÔªËØºÍÒ»¸ö"price"ÔªËØ¡£

The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line:
xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder.xml").
ÉÏÃæµÄÒ»ÐÐ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ¸æËßXML½âÎöÆ÷£¬¸ÃÎĵµÓ¦¸Ã±»Ò»·Ý schema Ëù¼ìÑé¡£xsi:noNamespaceSchemaLocation="shiporder.xsd" Ö¸¶¨ÁËschema Ó¦¸Ã´¦µÄλÖã¨ÕâÀËüÓëÃûΪ "shiporder.xml" µÄÎļþһͬ´¦ÔÚÏàͬµÄÎļþ¼ÐÀ¡£


Create an XML Schema
´´½¨Ò»·Ý XML Schema

Now we want to create a schema for the XML document above.
ÏÖÔÚ£¬ÎÒÃÇÏ£ÍûΪÉÏÃæµÄÕâ·ÝXMLÎĵµ´´½¨Ò»·Ýschema¡£

We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema:
Ê×ÏÈ£¬´ò¿ªÃûΪ "shiporder.xsd" µÄÐÂÎļþ¡£ÎªÁË´´½¨È«ÐµÄschema£¬ÎÒÃÇ¿ÉÒÔ¼òµ¥µØ°´ÕÕXMLÎĵµµÄ½á¹¹£¬¶¨Òåÿһ¸öÎÒÃÇËù·¢ÏÖµÄÔªËØ£»ÔÚ¿ªÊ¼µÄʱºò£¬ÎÒÃÇÏȽøÐбê×¼µÄXMLÉùÃ÷£¬È»ºó¶¨Òåschema ÄÚµÄ xs:schema ÔªËØ£º

<?xml version="1.0" encoding="ISO-8859-1" ?>

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


</xs:schema>

In the schema above we use the standard namespace (xs), and the URI associated with this namespace is the Schema language definition, which has the standard value of http://www.w3.org/2001/XMLSchema.
ÔÚÉÏÊöschemaÖУ¬ÎÒÃÇʹÓÃÁ˱ê×¼µÄÃüÃû¿Õ¼ä(xs)£¬Óë¸ÃÃüÃû¿Õ¼äÏà¹ØÁªµÄURIÊÇSchemaÓïÑÔ¶¨Ò壬Ëü°üº¬Ò»¸ö±ê×¼Öµ£ºhttp://www.w3.org/2001/XMLSchema¡£

Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements, therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements:
½ÓÏÂÀ´£¬ÎÒÃDZØÐ붨Òå "shiporder” ÔªËØ¡£Õâ¸öÔªËØ°üº¬Ò»¸öÊôÐÔ£¬Í¬Ê±»¹°üº¬ÆäËüµÄÔªËØ¡£Òò´Ë£¬ÎÒÃǰÑËü¿´³É¸´ºÏÀàÐÍ¡£"shiporder" ÔªËØµÄ×ÓÔªËØÓÉ ”xs:sequence“ ÔªËØ°üΧ×Å£¬”xs:sequence“ ÔªËØ¶¨ÒåÁË×ÓÔªËØµÄÅÅÁÐ˳Ðò£º

<xs:element name="shiporder">

<xs:complexType>
<xs:sequence>
...
...
</xs:sequence>
...
</xs:complexType>
</xs:element>

Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema that indicates a predefined schema data type:
´Ëʱ£¬ÎÒÃDZØÐ뽫"orderperson"ÔªËØ¶¨ÒåΪ¼òµ¥ÀàÐÍ£¬ÒòΪËü²»°üº¬ÈκÎÊôÐÔ»òÆäËüÔªËØ¡£ÀàÐÍ£¨xs:string£©Ê¹ÓÃÃüÃû¿Õ¼äµÄǰ׺Ϊǰ׺£¬¸ÃÃüÃû¿Õ¼äÊÇÓëÖ¸¶¨Ç°×ºSchemaÊý¾ÝÀàÐ͵ÄXML SchemaÏà¹ØÁªµÄ£º

<xs:element name="orderperson" type="xs:string"/>

Next, we have to define two elements that are of the complex type: "shipto" and "item". We start by defining the "shipto" element:
½ÓÏÂÀ´£¬ÎÒÃDZØÐ붨ÒåÁ½¸ö¸´ºÏÀàÐÍÔªËØ£º"shipto" ºÍ "item"¡£Ê×Ïȶ¨Òå"shipto"ÔªËØ£º

<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>

<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>

</xs:sequence>
</xs:complexType>
</xs:element>

With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!
ͨ¹ýschema£¬ÎÒÃÇ¿ÉÒÔʹÓÃmaxOccurs£¨×î´ó³öÏÖ´ÎÊý£©ºÍminOccurs£¨×îС³öÏÖ´ÎÊý£©ÊôÐÔÀ´¶¨ÒåÒ»¸öÔªËØµÄ¿ÉÄܳöÏÖ´ÎÊý¡£maxOccurs Ö¸¶¨ÁËÒ»¸öÔªËØµÄ×î´ó³öÏÖ´ÎÊý£¬minOccursÖ¸¶¨ÁËÒ»¸öÔªËØµÄ×îС³öÏÖ´ÎÊý¡£maxOccurs
ºÍminOccursµÄĬÈÏÖµ¶¼ÊÇ1¡£

Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional. We have specified this by setting the minOccurs attribute to zero:
½ÓÏÂÀ´£¬ÎÒÃǶ¨Òå"item"ÔªËØ¡£Õâ¸öÔªËØ¿ÉÒÔÔÚ"shiporder"ÔªËØÖÐÖØ¸´³öÏÖ£¬²¢ÇÒ£¬ÕâÏî²Ù×÷¿ÉÒÔͨ¹ý½« "item" ÔªËØµÄ maxOccurs ÊôÐÔÉèÖÃΪ "unbounded" À´ÊµÏÖ£¬ÊôÐÔΪ "unbounded" Òâζ×Å "item" ÔªËØ¿ÉÒÔ¸ù¾Ý×÷ÕßµÄÒâÔ¸ÖØ¸´³öÏÖ¶à´Î¡£×¢Ò⣺"note"ÔªËØÊÇÈÎÒâµÄ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý½« minOccurs ÊôÐÔÉèÖÃΪ0À´ÊµÏÖ¡£

<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>

<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>

<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>

</xs:complexType>
</xs:element>

We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify use="required".
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔÉùÃ÷"shiporder"ÔªËØµÄÊôÐÔÁË¡£ÒòΪÕâÊDZØÒªÊôÐÔ£¬ÎÒÃÇ¿ÉÒÔÖ¸¶¨ use="required"¡£

Note: The attribute declarations must always come last:
×¢Ò⣺ÊôÐÔÉùÃ÷±ØÐë×ÜÊÇ·ÅÔÚ×îºó£º

<xs:attribute name="orderid" type="xs:string" use="required"/>

Here is the complete listing of the schema file called "shiporder.xsd":
ÏÂÃæÁоÙÁËÒ»·ÝÍêÕûµÄÃûΪ "shiporder.xsd" µÄ schema Îļþ£º

<?xml version="1.0" encoding="ISO-8859-1" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>

<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>

<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>

<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>

<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>

</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>

</xs:complexType>
</xs:element>
</xs:schema>

 


Divide the Schema
»®·ÖSchema

The previous design method is very simple, but can be difficult to read and maintain when documents are complex.
ÉÏÊöÉè¼Æ·½·¨·Ç³£¼òµ¥£¬µ«ÊÇ£¬ÒòΪÎļþÊǸ´ºÏÀàÐÍ£¬ËùÒÔÄÑÓÚÔĶÁºÍά»¤¡£

The next design method is based on defining all elements and attributes first, and then referring to them using the ref attribute.
ÏÂÃæµÄÉè¼Æ·½·¨ÊÇ£ºÏȶ¨ÒåËùÓеÄÔªËØºÍÊôÐÔ£¬È»ºóʹÓà ref ÊôÐÔÒýÓÃËüÃÇ¡£

Here is the new design of the schema file ("shiporder.xsd"):
ÏÂÃæÊÇschemaÎļþµÄÈ«ÐÂÉè¼Æ·½Ê½("shiporder.xsd")£º

<?xml version="1.0" encoding="ISO-8859-1" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>

<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>

<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>

<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>

<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>

</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>

<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>

<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>

</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

 


Using Named Types
ʹÓÃÖ¸¶¨µÄÀàÐÍ

The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element.
µÚÈýÖÖÉè¼Æ·½·¨¶¨ÒåÁËÀà»òÀàÐÍ¡£Í¨¹ýΪ¼òµ¥ÀàÐÍÔªËØºÍ¸´ºÏÀàÐÍÔªËØÃüÃû£¬²¢Í¨¹ýÔªËØµÄ type [ ÀàÐÍ ] ÊôÐÔÖ¸ÏòÕâÐ©ÔªËØ£¬ÎÒÃǾÍÄܹ»ÖØÐÂʹÓÃÔªËØ¶¨Òå¡£

Here is the third design of the schema file ("shiporder.xsd"):
ÏÂÃæÁоÙÁË schema Îļþ("shiporder.xsd")µÄµÚÈý·ÝÉè¼Æ¼Ü¹¹£º

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">

<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">

<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>

<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>

<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">

<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>

<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>

</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>

<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>

</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>

The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype. So, the following fragment means that the value of the element or attribute must be a string value:
Ô¼ÊøÔªËØÖ¸Ã÷Á˸ÃÊý¾ÝÀàÐÍÊÇ´ÓÒ»¸ö W3C XML Schema ÃüÃû¿Õ¼äÊý¾ÝÀàÐÍÖÐÅÉÉú³öÀ´µÄ¡£Òò´Ë£¬ÏÂÃæµÄƬ¶ÎÒâζ×ÅÔªËØ»òÊôÐÔµÄÖµ±ØÐëÊÇÒ»¸ö×Ö·û´®µÄÖµ£º

<xs:restriction base="xs:string">

The restriction element is more often used to apply restrictions to elements. Look at the following lines from the schema above:
Ô¼ÊøÔªËØ³£³£ÓÃÓÚÎªÔªËØÌí¼ÓÔ¼ÊøÌõ¼þ¡£Çë¿´ÉÏÊöschemaÖеļ¸ÐÐÄÚÈÝ£º

<xs:simpleType name="orderidtype">

<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>

This indicates that the value of the element or attribute must be a string, it must be exactly six characters in a row, and those characters must be a number from 0 to 9.
ÉÏÊöÎļþÖ¸¶¨ÁËÔªËØ»òÊôÐÔµÄÖµ±ØÐëÊÇ×Ö·û´®£¬ÆäÖµ±ØÐëÊÇÒ»×é6¸ö´Ó0µ½9Ö®µÄÊý×Ö¡£

ÆÀÂÛ (0) All