XML Schema attribute 元素
定義和用法
attribute 元素定義一個(gè)屬性。
元素信息
出現(xiàn)次數(shù) | 在 schema 元素中定義一次。 在復(fù)雜類型或?qū)傩越M中引用多次。 |
父元素 | attributeGroup、schema、complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent) |
內(nèi)容 | annotation、simpleType |
語法
<attribute default=string fixed=string form=qualified|unqualified id=ID name=NCName ref=QName type=QName use=optional|prohibited|required any attributes > (annotation?,(simpleType?)) </attribute>
(? 符號(hào)聲明該元素可在 attribute 元素中出現(xiàn)零次或一次。)
屬性
default
可選。規(guī)定屬性的默認(rèn)值。default 和 fixed 屬性不能同時(shí)出現(xiàn)。
fixed
可選。規(guī)定屬性的固定值。default 和 fixed 屬性不能同時(shí)出現(xiàn)。
form
可選。規(guī)定屬性的格式。默認(rèn)值是包含該屬性的 schema 元素的 attributeFormDefault 屬性的值。可以設(shè)置為下列值:
- "qualified" - 指示必須通過命名空間前綴和該屬性的無冒號(hào)名稱 (NCName) 來限定此屬性。
- "unqualified" - 指示此屬性無須由命名空間前綴限定,且無須匹配此屬性的無冒號(hào)名稱 (NCName),即本地名稱。
id
可選。規(guī)定該元素的唯一的 ID。
name
可選。規(guī)定屬性的名稱。name 和 ref 屬性不能同時(shí)出現(xiàn)。
ref
可選。規(guī)定對(duì)指定的屬性的引用。name 和 ref 屬性不能同時(shí)出現(xiàn)。如果 ref 出現(xiàn),則 simpleType 元素、form 和 type 不能出現(xiàn)。
type
可選。規(guī)定內(nèi)建的數(shù)據(jù)類型或簡單類型。type 屬性只能在內(nèi)容不包含 simpleType 元素時(shí)出現(xiàn)。
use
可選。規(guī)定如何使用該屬性?稍O(shè)置下面的值:
- optional - 屬性是可選的并且可以具有任何值(默認(rèn))。
- prohibited - 不能使用屬性。
- required - 屬性的必需的。
any attributes
可選。規(guī)定帶有 non-schema 命名空間的任何其他屬性。
實(shí)例
例子 1
<xs:attribute name="code"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:attribute>
上面的例子指示 "code" 屬性有一個(gè)限定。唯一可接受的值是大寫字母 A 到 Z 中的兩個(gè)字母。
例子 2
如需使用在復(fù)雜類型中一個(gè)已有的屬性定義來聲明一個(gè)屬性,請(qǐng)使用 ref 屬性:
<xs:attribute name="code"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:complexType name="someComplexType"> <xs:attribute ref="code"/> </xs:complexType>
例子 3
屬性既可以擁有默認(rèn)值,也可以擁有指定的固定值。在沒有其他的值被指定時(shí),會(huì)自動(dòng)向?qū)傩再x予默認(rèn)值。在下面的例子中,默認(rèn)值是 "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
在沒有其他的值被指定時(shí),會(huì)自動(dòng)向?qū)傩再x予固定值。但是與默認(rèn)值不同,如果您為屬性規(guī)定了固定值以外的其他值,文檔會(huì)驗(yàn)證為無效。在下面的例子中,固定值是 "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
例子 4
所有屬性默認(rèn)都是可選的。如需明確地規(guī)定屬性為可選,請(qǐng)使用 "use" 屬性:
<xs:attribute name="lang" type="xs:string" use="optional"/>
使屬性成為必需的屬性:
<xs:attribute name="lang" type="xs:string" use="required"/>