空的複合元素不能包含内容,隻能含有屬性。 |
複合空元素
一個空的 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 限定則聲明了一個屬性但不會引入任何的元素内容。
<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>