有四种类型的复合元素:
空元素
包含其他元素的元素
仅包含文本的元素
包含元素和文本的元素
注释:上述元素均可包含属性!
1.空的复合元素不能包含内容,只能含有属性。
一个空的 XML 元素:<product prodid="1345" />
"product"元素根本没有内容。为了定义无内容的类型,我们就要申明一个不包含任何元素的的元素类型:
<xs:element name="product">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:integer">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
在上面的例子中,我们定义了一个带有复合内容的复合类型。
complexContent元素给出的信号是,我们打算限定或者拓展某个复合类型的内容模型,而 integer 限定则声明了一个属性但不会引入任何的元素内容。
不过,也可以更加紧凑地声明此 "product" 元素:
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
也可以这样定义
<xs:element name="product" type= "prodtype"/>
<xs:complexType name="prodtype">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
2.复合类型仅包含元素
XML 元素"person",仅包含其他的元素:
<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>
对应的schema定义:
<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>
请留意这个 <xs:sequence>。它意味着被定义的元素必须按上面的次序出现在 "person" 元素中。
也可以这样定义:
<xs:element name="person" type="persontype"/>
<xs:complexType name="persontype">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
3.仅含文本复合元素
仅含文本的复合元素可包含文本和属性。
此类型仅包含简易的内容(文本和属性),因此我们要向此内容添加 simpleContent 元素。当使用简易内容时,我们就必须在simpleContent元素内定义扩展或限定,就像这样:
<xs:element name="某个名称">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype">
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
或者:
<xs:element name="某个名称">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="basetype">
....
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
提示:请使用extension或restriction元素来扩展或限制元素的基本简易类型。
这里有一个 XML 元素的例子"shoesize",其中仅包含文本:
<shoesize country="france">35</shoesize>
下面这个例子声明了一个复合类型,其内容被定义为整数值,并且 "shoesize" 元素含有名为 "country" 的属性:
<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
也可以这样定义:
<xs:element name="shoesize" type="shoetype"/>
<xs:complexType name="shoetype">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
4.混合的复合类型可包含属性、元素以及文本。
例如:带有混合内容的复合类型XML元素"letter",含有文本以及其他元素:
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
对应的schema定义:
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
注释:为了使字符数据可以出现在 "letter" 的子元素之间,mixed 属性必须被设置为 "true"。
<xs:sequence> 标签 (name、orderid 以及 shipdate ) 意味着被定义的元素必须依次出现在 "letter" 元素内部。
也可以这样定义:
<xs:element name="letter" type="lettertype"/>
<xs:complexType name="lettertype" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>