什么是复合元素
- 空元素
<product pid="1345"/>
包含其他元素的元素
<employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee>
- 仅包含文本的元素
<food type="dessert">Ice cream</food>
包含元素和文本的元素
<description> It happened on <date lang="norwegian">03.03.99</date> .... </description>
定义复合元素
直接在元素中复合元素
<sequence>
中,意味着子元素必须以它们被声明的次序出现<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
使用type属性,引用要使用类型的名称
<sequence>
中,意味着子元素必须以它们被声明的次序出现<xs:element name="employee" type="personinfo"/> <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: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>
空元素
- 空元素
<product prodid="1345" />
限定空元素,实际上不声明任何元素即可,下面的例子只声明了一个属性
<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 限定则声明了一个属性但不会引入任何的元素内容。
更加紧凑的写法
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> </xs:element>
或者您可以为一个 complexType 元素起一个名字,然后为 “product” 元素设置一个 type 属性并引用这个 complexType 名称(通过使用此方法,若干个元素均可引用相同的复合类型)。
<xs:element name="product" type="prodtype"/> <xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType>
仅含元素
仅含元素
<person> <firstname>John</firstname> <lastname>Smith</lastname> </person>
限定仅含元素
<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” 元素中。或者您可以为 complexType 元素设定一个名称,并让 “person” 元素的 type 属性来引用此名称(如使用此方法,若干元素均可引用相同的复合类型):
<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>
仅含文本
- 仅含文本
<shoesize country="france">35</shoesize>
限定仅含文本
<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>
请使用 extension 或 restriction 元素来扩展或限制元素的基本简易类型。
声明了一个复合类型,其内容被定义为整数值,并且 “shoesize” 元素含有名为 “country” 的属性我们也可为 complexType 元素设定一个名称,并让 “shoesize” 元素的 type 属性来引用此名称(通过使用此方法,若干元素均可引用相同的复合类型):
<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>
包含元素和文本元素
混合元素
<letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>. </letter>
限定混合元素
<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”。 标签 (name、orderid 以及 shipdate ) 意味着被定义的元素必须依次出现在 “letter” 元素内部。
- 我们也可以为 complexType 元素起一个名字,并让 “letter” 元素的 type 属性引用 complexType 的这个名称(通过这个方法,若干元素均可引用同一个复合类型)
<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>