XML Schema complexType 元素

定义和用法

complexType 元素定义复杂类型。复杂类型的元素是包含其他元素和/或属性的 XML 元素。

元素信息

出现次数 在架构内为无限制;在元素内为一次。
父元素 element、redefine、schema
内容 annotation、simpleContent、complexContent、group、all、choice、sequence、attribute、attributeGroup、anyAttribute

语法

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