XML Schema complexType 元素


XML Schema 參考手冊 完整 XML Schema 參考手冊

定義和用法

complexType 元素定義複雜類型。複雜類型的元素是包含其他元素和/或屬性的 XML 元素。

元素資訊

  • 父元素: element, redefine, schema

語法

<complexType
id=ID
name=NCName
abstract=true|false
mixed=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>

(annotation?,(simpleContent|complexContent|((group|all|
choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))

</complexType>

(? 符號聲明在 complexType 元素中,元素可出現零次或一次,* 符號聲明元素可出現零次或多次。)

屬性 描述
id 可選。規定該元素的唯一的 ID。
name 可選。規定元素的名稱。
abstract 可選。規定在實例文檔中是否可以使用複雜類型。如果該值為 true,則元素不能直接使用該複雜類型,而是必須使用從該複雜類型派生的複雜類型。 默認值為 false。
mixed

可選。規定是否允許字元數據出現在該複雜類型的子元素之間。 默認值為 false。

  • 如果 simpleContent 元素是子元素,則不允許 mixed 屬性。
  • 如果 complexContent 元素是子元素,則該 mixed 屬性可被 complexContent 元素的 mixed 屬性重寫。
block

可選。防止具有指定派生類型的複雜類型被用來替代該複雜類型。該值可以包含 #all 或者一個列表,該列表是 extension 或 restriction 的子集:

  • extension - 防止通過擴展派生的複雜類型被用來替代該複雜類型。
  • restriction - 防止通過限制派生的複雜類型被用來替代該複雜類型。
  • #all - 防止所有派生的複雜類型被用來替代該複雜類型。
final

可選。防止從該 complexType 元素派生指定的類型。該值可以包含 #all 或者一個列表,該列表是 extension 或 restriction 的子集。

  • extension - 防止通過擴展派生。
  • restriction - 防止通過限制派生。
  • #all - 防止所有派生(擴展和限制)。
any attributes 可選。規定帶有 non-schema 命名空間的任何其他屬性。

實例 1

下麵的例子擁有一個名為 "note" 的複雜類型元素:

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
  <xs:element name="to" type="xs:string"/>
  <xs:element name="from" type="xs:string"/>
  <xs:element name="heading" type="xs:string"/>
  <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

實例 2

下麵的例子中有一個複雜類型 "fullpersoninfo",它通過使用三個補充的元素 (address、city 和 country) 對繼承的類型進行擴展,由另一個複雜類型 "personinfo" 派生而來:

<xs:element name="employee" type="fullpersoninfo"/>

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

<xs:complexType name="fullpersoninfo">
  <xs:complexContent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

在上面的例子中,上面的 "employee" 元素必須按順序包含下列元素:"firstname", "lastname", "address", "city" 以及 "country"。


XML Schema 參考手冊 完整 XML Schema 參考手冊