Latest web development tutorials

XML Schema 複合元素– 僅含文本

僅含文本的複合元素可包含文本和屬性。


僅含文本的複合元素

此類型僅包含簡易的內容(文本和屬性),因此我們要向此內容添加simpleContent 元素。 當使用簡易內容時,我們就必須在simpleContent 元素內定義擴展或限定,就像這樣:

<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype">
....
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

或者:

<xs:element name="somename">
<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>