XML Schema attribute 元素


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

定義和用法

attribute 元素定義一個屬性。

元素資訊

  • 父元素: 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 元素中出現零次或一次。)

屬性 描述
default 可選。規定屬性的默認值。default 和 fixed 屬性不能同時出現。
fixed 可選。規定屬性的固定值。default 和 fixed 屬性不能同時出現。
form

可選。規定屬性的格式。默認值是包含該屬性的 schema 元素的 attributeFormDefault 屬性的值。可以設置為下列值:

  • "qualified" - 指示必須通過命名空間首碼和該屬性的無冒號名稱 (NCName) 來限定此屬性。
  • "unqualified" - 指示此屬性無須由命名空間首碼限定,且無須匹配此屬性的無冒號名稱 (NCName),即本地名稱。
id 可選。規定該元素的唯一的 ID。
name 可選。規定屬性的名稱。name 和 ref 屬性不能同時出現。
ref 可選。規定對指定的屬性的引用。name 和 ref 屬性不能同時出現。如果 ref 出現,則 simpleType 元素、form 和 type 不能出現。
type 可選。規定內建的數據類型或簡單類型。type 屬性只能在內容不包含 simpleType 元素時出現。
use

可選。規定如何使用該屬性。可設置下麵的值:

  • optional - 屬性是可選的並且可以具有任何值(默認)。
  • prohibited - 不能使用屬性。
  • required - 屬性的必需的。
any attributes 可選。規定帶有 non-schema 命名空間的任何其他屬性。

實例 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" 屬性有一個限定。唯一可接受的值是大寫字母 A 到 Z 中的兩個字母。

實例 2

如需使用在複雜類型中一個已有的屬性定義來聲明一個屬性,請使用 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

屬性既可以擁有默認值,也可以擁有指定的固定值。在沒有其他的值被指定時,會自動向屬性賦予默認值。在下面的例子中,默認值是 "EN":

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

在沒有其他的值被指定時,會自動向屬性賦予固定值。但是與默認值不同,如果您為屬性規定了固定值以外的其他值,文檔會驗證為無效。在下面的例子中,固定值是 "EN":

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

實例 4

所有屬性默認都是可選的。如需明確地規定屬性為可選,請使用 "use" 屬性:

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

使屬性成為必需的屬性:

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


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