XML Schema element 元素
定義和用法
element 元素定義一個元素。
元素信息
- 父元素: schema, choice, all, sequence, group
語法
id=ID
name=NCName
ref=QName
type=QName
substitutionGroup=QName
default=string
fixed=string
form=qualified|unqualified
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
nillable=true|false
abstract=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>
annotation?,(simpleType|complexType)?,(unique|key|keyref)*
</element>
(? 符號聲明在element 元素中,該元素可出現零次或一次,* 符號聲明元素可出現零次或多次。)
属性 | 描述 |
---|---|
id | 可选。规定该元素的唯一的 ID。 |
name | 可选。规定元素的名称。如果父元素是 schema 元素,则此属性是必需的。 |
ref | 可选。对另一个元素的引用。ref 属性可包含一个命名空间前缀。如果父元素是 schema 元素,则不是使用该属性。 |
type | 可选。规定内建数据类型的名称,或者规定 simpleType 或 complexType 元素的名称。 |
substitutionGroup | 可选。规定可用来替代该元素的元素的名称。 该元素必须具有相同的类型或从指定元素类型派生的类型。 如果父元素不是 schema 元素,则不可以使用该属性。 |
default | 可选。为元素规定默认值(仅当元素内容是简单类型或 textOnly 时使用)。 |
fixed | 可选。为元素规定固定值(仅当元素内容是简单类型或 textOnly 时使用)。 |
form | 可选。该元素的形式。 默认值是包含该属性的 schema 元素的 elementFormDefault 属性的值。 该值必须是下列字符串之一: "qualified" 或 "unqualified"。 如果父元素是 schema 元素,则不能使用该属性。
|
maxOccurs | 可选。规定 element 元素在父元素中可出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串 "unbounded"。 默认值为 1。 如果父元素是 schema 元素,则不能使用该属性。 |
minOccurs | 可选。规定 element 元素在父元素中可出现的最小次数。该值可以是大于或等于零的整数。默认值为 1。 如果父元素是 schema 元素,则不能使用该属性。 |
nillable | 可选。指示是否可以将显式的零值分配给该元素。此项应用于元素内容并且不是该元素的属性。 默认值为 false。
如果nillable 為true,將使該元素的實例可以將nil 屬性設置為true。 nil 屬性被定義為實例的XML 架構命名空間的一部分。 |
abstract | 可選。 指示元素是否可以在實例文檔中使用。 如果該值為true,則元素不能出現在實例文檔中。 相反,substitutionGroup 屬性包含該元素的限定名(QName) 的其他元素必須出現在該元素的位置。 多個元素可以在其substitutionGroup 屬性中引用該元素。 默認值是false。 |
block | 可選。 派生的類型。 block 屬性防止具有指定派生類型的元素被用於替代該元素。 該值可以包含#all 或者一個列表,該列表是extension、restriction 或substitution 的子集:
|
final | 可選。 設置element 元素上final 屬性的默認值。 如果父元素不是schema 元素,則不能使用該屬性。 該值可以包含#all 或者一個列表,該列表是extension 或restriction 的子集:
|
any attributes | 可選。 規定帶有non-schema 命名空間的任何其他屬性。 |
實例1
下面的例子是一個schema,其中帶有四個簡單元素:"fname", "lname", "age" 以及"dateborn",類型是string、nonNegativeInteger 以及date:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="age" type="xs:nonNegativeInteger"/>
<xs:element name="dateborn" type="xs:date"/>
</xs:schema>
實例2
下面的例子是一個帶有復雜類型"note" 元素的schema。 "note" 元素包含四個簡單元素:"to", "from", "heading" 以及"body":
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
實例3
本例與例子2 相同,但是在此例中,我們選擇使用ref 屬性來引用元素名稱:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element ref="to"/>
<xs:element ref="from"/>
<xs:element ref="heading"/>
<xs:element ref="body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:schema>
完整XML Schema參考手冊