XSD 空元素
有關(guān) XML Schema 復(fù)合空元素的知識(shí),本節(jié)將為你描述。
空的復(fù)合元素不能包含內(nèi)容,只能含有屬性。
復(fù)合空元素:
一個(gè)空的 XML 元素:
<product prodid="1345" />
上面的 "product" 元素根本沒(méi)有內(nèi)容。為了定義無(wú)內(nèi)容的類型,我們就必須聲明一個(gè)在其內(nèi)容中只能包含元素的類型,但是實(shí)際上我們并不會(huì)聲明任何元素,比如這樣:
<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>
在上面的例子中,我們定義了一個(gè)帶有復(fù)合內(nèi)容的復(fù)合類型。complexContent 元素給出的信號(hào)是,我們打算限定或者拓展某個(gè)復(fù)合類型的內(nèi)容模型,而 integer 限定則聲明了一個(gè)屬性但不會(huì)引入任何的元素內(nèi)容。
但是,也可以更加緊湊地聲明此 "product" 元素:
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
或者您可以為一個(gè) complexType 元素起一個(gè)名字,然后為 "product" 元素設(shè)置一個(gè) type 屬性并引用這個(gè) complexType 名稱(通過(guò)使用此方法,若干個(gè)元素均可引用相同的復(fù)合類型):
<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
更多建議: