通過指示器,我們可以控制在文檔中使用元素的方式。
XSD 復合類型指示器
指示器
有七種指示器:
Order 指示器:
- All
- Choice
- Sequence
Occurrence 指示器:
- maxOccurs
- minOccurs
Group 指示器:
- Group name
- attributeGroup name
Order 指示器
Order 指示器用于定義元素的順序。
All 指示器
<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>
注釋:當使用 <all> 指示器時,你可以把 <minOccurs> 設置為 0 或者 1,而只能把 <maxOccurs> 指示器設置為 1(稍后將講解 <minOccurs> 以及 <maxOccurs>)。
Choice 指示器
<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>
提示:如需設置子元素出現任意次數,可將 <maxOccurs> (稍后會講解)設置為 unbounded(無限次)。
Sequence 指示器
<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 指示器
Occurrence 指示器用于定義某個元素出現的頻率。
注釋:對于所有的 "Order" 和 "Group" 指示器(any、all、choice、sequence、group name 以及 group reference),其中的 maxOccurs 以及 minOccurs 的默認值均為 1。
maxOccurs 指示器
<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>
上面的例子表明,子元素 "child_name" 可在 "person" 元素中最少出現一次(其中 minOccurs 的默認值是 1),最多出現 10 次。
minOccurs 指示器
<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>
上面的例子表明,子元素 "child_name" 可在 "person" 元素中出現最少 0 次,最多出現 10 次。
提示:如需使某個元素的出現次數不受限制,請使用 maxOccurs="unbounded" 這個聲明:
一個實際的例子:
名為 "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>Tony Smith</full_name> <child_name>Cecilie</child_name> </person> <person> <full_name>David Smith</full_name> <child_name>Jogn</child_name> <child_name>mike</child_name> <child_name>kyle</child_name> <child_name>mary</child_name> </person> <person> <full_name>Michael Smith</full_name> </person> </persons>
上面這個 XML 文件含有一個名為 "persons" 的根元素。在這個根元素內部,我們定義了三個 "person" 元素。每個 "person" 元素必須含有一個 "full_name" 元素,同時它可以包含多至 5 個 "child_name" 元素。
這是schema文件"family.xsd":
<?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 指示器
Group 指示器用于定義相關的數批元素。
元素組
元素組通過 group 聲明進行定義:
<xs:group name="組名稱"> ... </xs:group>
您必須在 group 聲明內部定義一個 all、choice 或者 sequence 元素。下面這個例子定義了名為 "persongroup" 的 group,它定義了必須按照精確的順序出現的一組元素:
<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>
在您把 group 定義完畢以后,就可以在另一個定義中引用它了:
<xs:groupname="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>
屬性組
屬性組通過 attributeGroup 聲明來進行定義:
<xs:attributeGroup name="組名稱"> ... </xs:attributeGroup>
下面這個例子定義了名為 "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>
在您已定義完畢屬性組之后,就可以在另一個定義中引用它了,就像這樣:
<xs:attributeGroupname="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>