XSD複雜指示符

2019-10-16 23:17:58

指示符控制元素在XML文件中的組織方式。 有七種型別的指標,分為三大類。

順序指示符

  • all - 子元素可以按任何順序出現。
  • choice - 只能選擇一個子元素。
  • sequence - 子元素只能按指定順序出現。

出現指示符

  • maxOccurs - 子元素只能出現maxOccurs次數。
  • minOccurs - 子元素必須出現minOccurs次數。

分組指示符

  • group - 定義相關的元素集。
  • attributeGroup - 定義相關的屬性集。

1. 順序指示符

使用<all>student元素可以在XML文件中以任何順序使用:firstnamelastnamenicknamemarks這些子元素。如下所示 -

<xs:complexType name = "StudentType" mixed = "true">
   <xs:all>
      <xs:element name = "firstname" type = "xs:string"/>
      <xs:element name = "lastname" type = "xs:string"/>
      <xs:element name = "nickname" type = "xs:string"/>
      <xs:element name = "marks" type = "xs:positiveInteger"/>
   </xs:all>
   <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>

<xs:element name = 'student' type = 'StudentType' />

使用<choice>student元素只能包含XML文件中的:firstnamelastnamenicknamemarks子元素之一。如下所示 -

<xs:complexType name = "StudentType" mixed = "true">
   <xs:choice>
      <xs:element name = "firstname" type = "xs:string"/>
      <xs:element name = "lastname" type = "xs:string"/>
      <xs:element name = "nickname" type = "xs:string"/>
      <xs:element name = "marks" type = "xs:positiveInteger"/>
   </xs:choice>
   <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>

<xs:element name = 'student' type = 'StudentType' />

使用<sequence>student元素只能在XML文件中以指定的順序使用firstnamelastnamenicknamemarks子元素。

<xs:complexType name = "StudentType" mixed = "true">
   <xs:sequence>
      <xs:element name = "firstname" type = "xs:string"/>
      <xs:element name = "lastname" type = "xs:string"/>
      <xs:element name = "nickname" type = "xs:string"/>
      <xs:element name = "marks" type = "xs:positiveInteger"/>
   </xs:sequence>
   <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>

<xs:element name = 'student' type = 'StudentType' />

出現指示符
使用<maxOccurs>student元素在XML文件中最多可以有兩個nickname,如下程式碼 -

<xs:complexType name = "StudentType" mixed = "true">
   <xs:all>
      <xs:element name = "firstname" type = "xs:string"/>
      <xs:element name = "lastname" type = "xs:string"/>
      <xs:element name = "nickname" type = "xs:string" maxOccurs="2"/>
      <xs:element name = "marks" type = "xs:positiveInteger"/>
   </xs:all>
   <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>

<xs:element name = 'student' type = 'StudentType' />

使用<minOccurs>student元素在XML文件中至少要有兩個nickname。如下程式碼 -

<xs:complexType name = "StudentType" mixed = "true">
   <xs:all>
      <xs:element name = "firstname" type = "xs:string"/>
      <xs:element name = "lastname" type = "xs:string"/>
      <xs:element name = "nickname" type = "xs:string" minOccurs="2"/>
      <xs:element name = "marks" type = "xs:positiveInteger"/>
   </xs:all>
   <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>

<xs:element name = 'student' type = 'StudentType' />

分組指標符

<group>用於對一組相關元素進行分組。 在這裡,建立了一組name,然後使用該組來定義student元素。

<xs:group name = "infogroup">
   <xs:sequence>
      <xs:element name = "firstname" type = "xs:string"/>
      <xs:element name = "lastname" type = "xs:string"/>
      <xs:element name = "birthdate" type = "xs:date"/>
   </xs:sequence>
</xs:group>

<xs:element name = "student" type = "studentType"/>

<xs:complexType name = "studentType">
   <xs:sequence>
      <xs:group ref = "infogroup"/>
      <xs:element name = "marks" type = "xs:integer"/>
   </xs:sequence>
</xs:complexType>

<attributeGroup>用於對相關的屬性集進行分組。 在這裡,我們建立了一組name,然後使用該組來定義student元素的屬性。

<xs:attributeGroup name = "infogroup">
   <xs:sequence>
      <xs:attribute name = "firstname" type = "xs:string"/>
      <xs:attribute name = "lastname" type = "xs:string"/>
      <xs:attribute name = "birthdate" type = "xs:date"/>
   </xs:sequence>
</xs:attributeGroup>

<xs:element name = "student" type = "studentType"/>

<xs:complexType name = "studentType">
   <xs:sequence>
      <xs:attributeGroup ref = "infogroup"/>
      <xs:element name = "marks" type = "xs:integer"/>
   </xs:sequence>
</xs:complexType>