Latest web development tutorials

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參考手冊