ÍøÂçѧԺ 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 ÐÞ¸Ä:2008-04-05 ä¯ÀÀ:4942 :: ::

We can control HOW elements are to be used in documents with indicators.
ͨ¹ýʹÓÃָʾÆ÷£¨Indicators£©£¬ÎÒÃÇ¿ÉÒÔ¿ØÖÆÎļþÖÐÔªËØµÄʹÓ÷½·¨¡£


Indicators
ָʾÆ÷

There are seven indicators:
ָʾÆ÷Ò»¹²ÓÐ7ÖÖÀàÐÍ£¬¾ßÌåÈçÏ£º

Order indicators:
˳ÐòָʾÆ÷

  • All
    È«²¿
  • Choice
    Ñ¡Ôñ
  • Sequence
    ÐòÁÐ

Occurrence indicators:
ƵÂÊָʾÆ÷£º

  • maxOccurs
    ×î´ó³öÏÖ´ÎÊý
  • minOccurs
    ×îС³öÏÖ´ÎÊý

Group indicators:
×éָʾÆ÷£º

  • Group name
    ×éÃû
  • attributeGroup name
    ÊôÐÔ×éÃû³Æ

Order 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">
<xs:complexType>

<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>

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

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">
<xs:complexType>
<xs:choice>

<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>

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

Sequence Indicator
ÐòÁÐָʾÆ÷

The <sequence> indicator specifies that the child elements must appear in a specific order:
<sequence>ָʾÆ÷Ö¸¶¨ÁË×ÓÔªËØ±ØÐëÒÔÒ»¸öÖ¸¶¨µÄ˳Ðò³öÏÖ£º

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

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

 


Occurrence Indicators
ƵÂÊָʾÆ÷

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">

<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>

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

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">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>

<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>

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

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"

xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>

<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>

</person>
<person>
<full_name>Stale Refsnes</full_name>
</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:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">

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

minOccurs="0" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>

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

 


Group Indicators
×éָʾÆ÷

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">

...
</xs:group>

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">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>

<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>

</xs:group>

After you have defined a group, you can reference it in another definition, like this:
¶¨ÒåÁËÒ»¸ö×éÖ®ºó£¬Äã¿ÉÒÔÔÚÁíÒ»¸ö×éÖÐÒýÓÃËü£¬ÈçÏÂËùʾ£º

<xs:group name="persongroup">

<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>

<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>

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

Attribute Groups
ÊôÐÔ×é

Attribute groups are defined with the attributeGroup declaration, like this:
ÊôÐÔ×鱨ÐëÓÉattributeGroup£¨ÊôÐÔ×飩ÉùÃ÷½øÐж¨Ò壬ÈçÏÂËùʾ£º

<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>

The following example defines an attribute group named "personattrgroup":
ÏÂÊö°¸Àý¶¨ÒåÁËÃûΪ"personattrgroup"µÄÒ»¸öÊôÐÔ×飺

<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>

<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

After you have defined an attribute group, you can reference it in another definition, like this:
¶¨ÒåÁËÒ»¸öÊôÐÔ×éÖ®ºó£¬ÄãÒ²¿ÉÒÔÔÚ±ðµÄ¶¨ÒåÖÐÒýÓÃËü£¬ÈçÏÂËùʾ£º

<xs:attributeGroup name="personattrgroup">

<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>

</xs:element>

ÆÀÂÛ (5) 1 All