XSD 混合內容 XSD <any>
通過指標,我們可以控制在文檔中使用元素的方式。 指標
有七種指標: 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: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>
屬性群組
屬性群組通過 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: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>