XML Schema attribute 元素

2018-09-12 10:17 更新

XML Schema attribute 元素


XML Schema 參考手冊 完整 XML Schema 參考手冊

定義和用法

attribute 元素定義一個(gè)屬性。

元素信息

  • 父元素: attributeGroup, schema, complexType, restriction (both simpleContent and complexContent), extension (both simpleContent and complexContent)

語法

<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>

(? 符號聲明該元素可在 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 屬性的值??梢栽O(shè)置為下列值:

  • "qualified" - 指示必須通過命名空間前綴和該屬性的無冒號名稱 (NCName) 來限定此屬性。
  • "unqualified" - 指示此屬性無須由命名空間前綴限定,且無須匹配此屬性的無冒號名稱 (NCName),即本地名稱。
id 可選。規(guī)定該元素的唯一的 ID。
name 可選。規(guī)定屬性的名稱。name 和 ref 屬性不能同時(shí)出現(xiàn)。
ref 可選。規(guī)定對指定的屬性的引用。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è)字母。

實(shí)例 2

如需使用在復(fù)雜類型中一個(gè)已有的屬性定義來聲明一個(gè)屬性,請使用 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>

實(shí)例 3

屬性既可以擁有默認(rèn)值,也可以擁有指定的固定值。在沒有其他的值被指定時(shí),會自動向?qū)傩再x予默認(rèn)值。在下面的例子中,默認(rèn)值是 "EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>

在沒有其他的值被指定時(shí),會自動向?qū)傩再x予固定值。但是與默認(rèn)值不同,如果您為屬性規(guī)定了固定值以外的其他值,文檔會驗(yàn)證為無效。在下面的例子中,固定值是 "EN":

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

實(shí)例 4

所有屬性默認(rèn)都是可選的。如需明確地規(guī)定屬性為可選,請使用 "use" 屬性:

<xs:attribute name="lang" type="xs:string" use="optional"/>

使屬性成為必需的屬性:

<xs:attribute name="lang" type="xs:string" use="required"/>


XML Schema 參考手冊 完整 XML Schema 參考手冊
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號