w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD ָʾÆ÷¸´ºÏÀàÐÍ
XSD ָʾÆ÷¸´ºÏÀàÐÍ
::
We can control HOW elements are to be used in documents with indicators.
ͨ¹ýʹÓÃָʾÆ÷£¨Indicators£©£¬ÎÒÃÇ¿ÉÒÔ¿ØÖÆÎļþÖÐÔªËØµÄʹÓ÷½·¨¡£
There are seven indicators:
ָʾÆ÷Ò»¹²ÓÐ7ÖÖÀàÐÍ£¬¾ßÌåÈçÏ£º
Order indicators:
˳ÐòָʾÆ÷
Occurrence indicators:
ƵÂÊָʾÆ÷£º
Group indicators:
×éָʾÆ÷£º
Order indicators are used to define the order of the elements.
˳ÐòָʾÆ÷ÓÃÓÚ¶¨ÒåÔªËØµÄÅÅÁÐ˳Ðò¡£
All Indicator
È«²¿Ö¸Ê¾Æ÷
The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:
<all>ָʾÆ÷Ö¸Ã÷ÁË×ÓÔªËØ¿ÉÒÔ°´ÕÕÈÎÒâ´ÎÐò³öÏÖ£¬²¢ÇÒÿ¸ö×ÓÔªËØÖ»ÔÊÐí³öÏÖÒ»´Î£º
<xs:element name="person"> |
Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later).
×¢Ò⣺ÔÚʹÓÃ<all>ָʾÆ÷ʱ£¬Äã¿ÉÒÔ°Ñ<minOccurs>ָʾÆ÷ÉèÖÃΪ0»ò1£¬<maxOccurs>ָʾÆ÷Ö»ÄÜÉèÖÃΪ1£¨<minOccurs> ºÍ <maxOccurs> µÄ¾ßÌåÄÚÈÝÎÒÃǽ«ÔÚºóÃæµÄ²¿·ÖÖÐ×÷¾ßÌå²ûÊÍ£©¡£
Choice Indicator
Ñ¡ÔñָʾÆ÷
The <choice> indicator specifies that either one child element or another can occur:
<choice> ָʾÆ÷Ö¸Ã÷ÁËÈÎÒ»×ÓÔªËØ»òÆäËüÔªËØ¶¼¿ÉÒÔ³öÏÖ£º
<xs:element name="person"> |
Sequence Indicator
ÐòÁÐָʾÆ÷
The <sequence> indicator specifies that the child elements must appear in a specific order:
<sequence>ָʾÆ÷Ö¸¶¨ÁË×ÓÔªËØ±ØÐëÒÔÒ»¸öÖ¸¶¨µÄ˳Ðò³öÏÖ£º
<xs:element name="person"> |
Occurrence indicators are used to define how often an element can occur.
ƵÂÊָʾÆ÷Ϊһ¸öÔªËØÖ¸¶¨³öÏֵĴÎÊý¡£
Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.
×¢Ò⣺¶ÔÓÚËùÓеÄ"Order"ºÍ"Group"ָʾÆ÷(any¡¢all¡¢choice¡¢sequence¡¢group name ºÍ group reference)À´Ëµ£¬maxOccurs ºÍ minOccursµÄĬÈÏÖµ¶¼ÊÇ1¡£
maxOccurs Indicator
×î´óƵÂÊָʾÆ÷
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
<maxOccurs>ָʾÆ÷Ö¸¶¨ÁËÒ»¸öÔªËØ¿ÉÒÔ³öÏÖµÄ×î´ó´ÎÊý£º
<xs:element name="person"> |
The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.
ÉÏÊö°¸ÀýÖ¸¶¨ÁË"child_name"ÔªËØÔÚ"person"ÔªËØÀï×îÉÙ±ØÐë³öÏÖ1´Î£¨minOccursµÄĬÈÏֵΪ1£©£¬×î¶à¿ÉÒÔ³öÏÖ10´Î¡£
minOccurs Indicator
×îСƵÂÊָʾÆ÷
The <minOccurs> indicator specifies the minimum number of times an element can occur:
<minOccurs> ָʾÆ÷Ö¸¶¨ÁËÒ»¸öÔªËØ±ØÐë³öÏÖµÄ×îС´ÎÊý£º
<xs:element name="person"> |
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.
ÉÏÊö°¸ÀýÖ¸¶¨ÁË"child_name"ÔªËØ¿ÉÒÔ²»ÔÚ"person"ÔªËØÄÚ³öÏÖ£¨minOccursµÄĬÈÏֵΪ1£©£»»òÕߣ¬×î¶àÖ»ÔÊÐí³öÏÖ10´Î¡£
Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:
Ìáʾ£ºÎªÁËÊ¹ÔªËØ¿ÉÒÔ²»ÏÞ´ÎÊýµØÖظ´³öÏÖ£¬¿ÉÒÔÉèÖà maxOccurs="unbounded" ¡£
A working example:
ʵ¼Ê°¸Àý£º
An XML file called "Myfamily.xml":
ÏÂÃæÁоÙÁËÒ»¸öÃûΪ"Myfamily.xml"µÄXMLÎļþ£º
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <person> <person> <person> </persons> |
The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements.
ÉÏÊöXMLÎļþÖаüº¬ÁËÒ»¸öÃûΪ"persons"µÄ¸ùÔªËØ¡£ÔÚÕâ¸ö¸ùÔªËØÖУ¬ÎÒÃÇÒѾ¶¨ÒåÁË3¸ö"person"ÔªËØ¡£Ã¿¸ö"person"ÔªËØ±ØÐë°üº¬Ò»¸ö"full_name"ÔªËØ£¬¶øÇÒ×î¶àÖ»Äܰüº¬5¸ö"child_name"ÔªËØ¡£
Here is the schema file "family.xsd":
ÏÂÃæÁоÙÁËÒ»·ÝÃûΪ"family.xsd"µÄschemaÎļþ£º
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:element name="persons"> </xs:schema> |
Group indicators are used to define related sets of elements.
×éָʾÆ÷ÓÃÓÚ¶¨ÒåÏà¹ØµÄÔªËØ×é¡£
Element Groups
ÔªËØ×é
Element groups are defined with the group declaration, like this:
ÔªËØ×鱨Ðëͨ¹ý×éÉùÃ÷½øÐж¨Ò壬ÈçÏÂËùʾ£º
<xs:group name="groupname"> |
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:
Äã±ØÐëÔÚ×éÉùÃ÷ÀﶨÒåÒ»¸öall¡¢choice »ò sequence ÔªËØ¡£ÏÂÊö°¸Àý¶¨ÒåÁËÒ»¸öÃûΪ"persongroup"µÄ×飬²¢ÒªÇóÒ»×éÔªËØ±ØÐëÒÔÖ¸¶¨µÄ˳Ðò³öÏÖ£º
<xs:group name="persongroup"> |
After you have defined a group, you can reference it in another definition, like this:
¶¨ÒåÁËÒ»¸ö×éÖ®ºó£¬Äã¿ÉÒÔÔÚÁíÒ»¸ö×éÖÐÒýÓÃËü£¬ÈçÏÂËùʾ£º
<xs:group name="persongroup"> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> |
Attribute Groups
ÊôÐÔ×é
Attribute groups are defined with the attributeGroup declaration, like this:
ÊôÐÔ×鱨ÐëÓÉattributeGroup£¨ÊôÐÔ×飩ÉùÃ÷½øÐж¨Ò壬ÈçÏÂËùʾ£º
<xs:attributeGroup name="groupname"> |
The following example defines an attribute group named "personattrgroup":
ÏÂÊö°¸Àý¶¨ÒåÁËÃûΪ"personattrgroup"µÄÒ»¸öÊôÐÔ×飺
<xs:attributeGroup name="personattrgroup"> |
After you have defined an attribute group, you can reference it in another definition, like this:
¶¨ÒåÁËÒ»¸öÊôÐÔ×éÖ®ºó£¬ÄãÒ²¿ÉÒÔÔÚ±ðµÄ¶¨ÒåÖÐÒýÓÃËü£¬ÈçÏÂËùʾ£º
<xs:attributeGroup name="personattrgroup"> <xs:element name="person"> |