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>