XSD 空元素
空复杂元素不能包含内容,只能包含属性。
复杂空元素
一个空的 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)的复杂类型。complexContent 元素表示我们打算限制或扩展复杂类型的模型,而 integer 的限制(restriction)声明了一个属性,但没有引入任何元素内容。
然而,可以更简洁地声明 "product" 元素,如下所示:
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
或者,您可以为 complexType 元素命名,然后让 "product" 元素有一个 type 属性引用 complexType 的名称(如果使用此方法,则多个元素可以引用同一个 complex type):
<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>