僅含文本的復合元素可包含文本和屬性。
XSD 僅含文本復合元素
僅含文本的復合元素
此類型僅包含簡易的內容(文本和屬性),因此我們要向此內容添加 simpleContent 元素。當使用簡易內容時,我們就必須在 simpleContent 元素內定義擴展或限定,就像這樣:
<xs:element name="某個名稱"> <xs:complexType> <xs:simpleContent> <xs:extension base="basetype"> .... .... </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
或者:
<xs:element name="某個名稱"> <xs:complexType> <xs:simpleContent> <xs:restriction base="basetype"> .... .... </xs:restriction> </xs:simpleContent> </xs:complexType> </xs:element>
提示:請使用 extension 或 restriction 元素來擴展或限制元素的基本簡易類型。
這里有一個 XML 元素的例子,"shoesize",其中僅包含文本:
<shoesize country="france">35</shoesize>
下面這個例子聲明了一個復合類型,其內容被定義為整數值,并且 "shoesize" 元素含有名為 "country" 的屬性:
<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>
我們也可為 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>