Ò»¸ö XSD ʵÀý
±¾½Ú»áΪÄúÑÝʾÈçºÎ±àдһ¸ö XML Schema¡£Äú»¹½«Ñ§Ï°µ½±àд schema µÄ²»Í¬·½·¨¡£
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>George Bush</orderperson> <shipto> <name>John Adams</name> <address>Oxford Street</address> <city>London</city> <country>UK</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>
ÉÏÃæµÄXMLÎĵµ°üÀ¨¸ùÔªËØ "shiporder"£¬ÆäÖаüº¬±ØÐëÃûΪ "orderid" µÄÊôÐÔ¡£"shiporder" ÔªËØ°üº¬Èý¸ö²»Í¬µÄ×ÓÔªËØ£º"orderperson"¡¢"shipto" ÒÔ¼° "item"¡£"item" ÔªËØ³öÏÖÁËÁ½´Î£¬Ëüº¬ÓÐÒ»¸ö "title"¡¢Ò»¸ö¿ÉÑ¡ "note" ÔªËØ¡¢Ò»¸ö "quantity" ÒÔ¼°Ò»¸ö "price" ÔªËØ¡£
ÉÏÃæÕâÒ»ÐÐ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"£¬¸æÖªXML½âÎöÆ÷¸ù¾Ýij¸ö schema À´ÑéÖ¤´ËÎĵµ¡£ÕâÒ»ÐУºxsi:noNamespaceSchemaLocation="shiporder.xsd" ¹æ¶¨ÁË schema µÄλÖã¨ÔÚÕâÀËüÓë "shiporder.xml" ´¦ÓÚÏàͬµÄÎļþ¼Ð£©¡£
´´½¨Ò»¸ö XML Schema
ÏÖÔÚ£¬ÎÒÃÇÐèҪΪÉÏÃæÕâ¸ö XML Îĵµ´´½¨Ò»¸ö schema¡£
ÎÒÃÇ¿ÉÒÔͨ¹ý´ò¿ªÒ»¸öеÄÎļþÀ´¿ªÊ¼£¬²¢°ÑÕâ¸öÎļþÃüÃûΪ "shiporder.xsd"¡£Òª´´½¨schema£¬ÎÒÃǽö½öÐèÒª¼òµ¥µØ×ñÑ XML ÎĵµÖеĽṹ£¬¶¨ÒåÎÒÃÇËù·¢ÏÖµÄÿ¸öÔªËØ¡£Ê×ÏÈÎÒÃÇ¿ªÊ¼¶¨ÒåÒ»¸ö±ê×¼µÄ XML ÉùÃ÷£º
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... ... </xs:schema>
ÔÚÉÏÃæµÄ schema ÖУ¬ÎÒÃÇʹÓÃÁ˱ê×¼µÄÃüÃû¿Õ¼ä (xs)£¬Óë´ËÃüÃû¿Õ¼äÏà¹ØÁªµÄ URI ÊÇ Schema µÄÓïÑÔ¶¨Ò壨Schema language definition£©£¬Æä±ê×¼ÖµÊÇ http://www.w3.org/2001/XMLSchema¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª¶¨Òå "shiporder" ÔªËØ¡£´ËÔªËØÓµÓÐÒ»¸öÊôÐÔ£¬ÆäÖаüº¬ÆäËûµÄÔªËØ£¬Òò´ËÎÒÃǽ«ËüÈ϶¨Îª¸´ºÏÀàÐÍ¡£"shiporder" ÔªËØµÄ×ÓÔªËØ±» xs:sequence ÔªËØ°üΧ£¬¶¨ÒåÁË×ÓÔªËØµÄ´ÎÐò£º
<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType> </xs:element>
È»ºóÎÒÃÇÐèÒª°Ñ "orderperson" ÔªËØ¶¨ÒåΪ¼òÒ×ÀàÐÍ£¨ÕâÊÇÒòΪËü²»°üº¬ÈκÎÊôÐÔ»òÕ߯äËûµÄÔªËØ£©¡£ÀàÐÍ (xs:string) µÄǰ׺ÊÇÓÉÃüÃû¿Õ¼äµÄǰ׺¹æ¶¨µÄ£¬´ËÃüÃû¿Õ¼äÓëָʾԤ¶¨ÒåµÄ schema Êý¾ÝÀàÐ굀 XML schema Ïà¹ØÁª£º
<xs:element name="orderperson" type="xs:string"/>
½ÓÏÂÀ´£¬ÎÒÐèÒª°ÑÁ½¸öÔªËØ¶¨ÒåΪ¸´ºÏÀàÐÍ£º"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>
ͨ¹ý schema£¬ÎÒÃÇ¿ÉʹÓà maxOccurs ºÍ minOccurs ÊôÐÔÀ´¶¨Òåij¸öÔªËØ¿ÉÄܳöÏֵĴÎÊý¡£maxOccurs ¶¨ÒåÄ³ÔªËØ³öÏÖ´ÎÊýµÄ×î´óÖµ£¬¶ø minOccurs Ôò¶¨ÒåÄ³ÔªËØ³öÏÖ´ÎÊýµÄ×îСֵ¡£maxOccurs ºÍ minOccurs µÄĬÈÏÖµ¶¼ÊÇ 1£¡
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ¶¨Òå "item" ÔªËØÁË¡£Õâ¸öÔªËØ¿ÉÔÚ "shiporder" ÔªËØÄÚ²¿³öÏÖ¶à´Î¡£ÕâÊÇͨ¹ý°Ñ "item" ÔªËØµÄ maxOccurs ÊôÐÔµÄÖµÉ趨Ϊ "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>
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔÉùÃ÷ "shiporder" ÔªËØµÄÊôÐÔÁË¡£ÓÉÓÚÕâÊÇÒ»¸ö±ØÑ¡ÊôÐÔ£¬ÎÒÃǹ涨 use="required"¡£
×¢ÊÍ£º´ËÊôÐÔµÄÉùÃ÷±ØÐë±»ÖÃÓÚ×îºó£º
<xs:attribute name="orderid" type="xs:string" use="required"/>
ÕâÊÇÕâ¸öÃûΪ "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>
·Ö¸î Schema
Ç°ÃæµÄÉè¼Æ·½·¨·Ç³£ÈÝÒ×£¬µ«µ±ÎĵµºÜ¸´ÔÓʱȴÄÑÒÔÔĶÁºÍά»¤¡£
½ÓÏÂÀ´½éÉܵÄÉè¼Æ·½·¨»ùÓÚÊ×ÏȶÔËùÓÐÔªËØºÍÊôÐԵ͍Ò壬ȻºóÔÙʹÓà ref ÊôÐÔÀ´ÒýÓÃËüÃÇ¡£
ÕâÊÇÓÃз½·¨Éè¼ÆµÄ schema Îļþ£º
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- ¼òÒ×ÔªËØµÄ¶¨Òå --> <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"/> <!-- ÊôÐԵ͍Òå --> <xs:attribute name="orderid" type="xs:string"/> <!-- ¸´ºÏÔªËØµÄ¶¨Òå --> <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>
ʹÓÃÖ¸¶¨µÄÀàÐÍ£¨Named Types£©
µÚÈýÖÖÉè¼Æ·½·¨¶¨ÒåÁËÀà»òÕßÀàÐÍ£¬ÕâÑùʹÎÒÃÇÓÐÄÜÁ¦Öظ´Ê¹ÓÃÔªËØµÄ¶¨Òå¡£¾ßÌåµÄ·½Ê½ÊÇ£ºÊ×ÏȶԼòÒ×ÔªËØºÍ¸´ºÏÔªËØ½øÐÐÃüÃû£¬È»ºóͨ¹ýÔªËØµÄ type ÊôÐÔÀ´Ö¸ÏòËüÃÇ¡£
ÕâÊÇÀûÓõÚÈýÖÖ·½·¨Éè¼ÆµÄ 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>
restriction ÔªËØÏÔʾ³öÊý¾ÝÀàÐÍÔ´×ÔÓÚ W3C XML Schema ÃüÃû¿Õ¼äµÄÊý¾ÝÀàÐÍ¡£Òò´Ë£¬ÏÂÃæµÄƬ¶ÎÒ²¾ÍÒâζ×ÅÔªËØ»òÊôÐÔµÄÖµ±ØÐëÊÇ×Ö·û´®ÀàÐ͵ÄÖµ£º
<xs:restriction base="xs:string">
restriction ÔªËØ³£±»ÓÃÓÚÏòÔªËØÊ©¼ÓÏÞÖÆ¡£Çë¿´ÏÂÃæÕâЩÀ´×ÔÒÔÉÏ schema µÄƬ¶Î£º
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
Õâ¶Î´úÂëָʾ³ö£¬ÔªËØ»òÊôÐÔµÄÖµ±ØÐëΪ×Ö·û´®£¬²¢ÇÒ±ØÐëÊÇÁ¬ÐøµÄÁù¸ö×Ö·û£¬Í¬Ê±ÕâЩ×Ö·û±ØÐëÊÇ 0-9 µÄÊý×Ö¡£