声明一个特性。
<attribute
default = string
fixed = string
form = (qualified | unqualified)
id = ID
name = NCName
ref = QName
type = QName
use = (optional | prohibited | required): optional
{any attributes with non-schema Namespace...}>
Content: (annotation?, (simpleType?))
</attribute>
特性
default
特性具有默认值。 如果此特性未在 XML 文档内的实例中指定,则此特性具有给定的值。 如果此属性未出现在实例文档中,则此属性应出现在后架构验证信息集中。 如果属性实际上不在实例文档中,则架构的处理器应当表现为如同该属性已指定为默认值一样。 Default 和 fixed 属性不能同时出现。可选。
fixed
属性具有固定值。 如果此属性出现于实例文档中,则其值必须等于给出的固定值。 如果此属性不出现,则此属性接收提供的值。 Default 和 fixed 属性不能同时出现。可选。
form
属性的格式。 默认值是包含该属性的 schema 元素的 attributeFormDefault 属性的值。 该值必须为下列字符串之一:“qualified”或“unqualified”。如果该值是非限定的,则此属性无须由命名空间前缀限定并且匹配此属性的无冒号名称 (NCName),即本地名称。
如果该值是限定的,则必须通过将架构的 targetNamespace 与属性的 NCName 相组合来限定此属性。
可选。
id
该元素的 ID。 id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。可选。
name
属性名。 该名称必须是在 XML 命名空间规范中定义的 NCName。 如果包含元素是 schema 元素,则是必选项。 Name 和 ref 属性不能同时出现。当根据一个架构对 XML 文档进行验证时,根据该架构中的 attribute 元素验证文档中的每一属性。
可选。
ref
在该架构(或由指定的命名空间指示的其他架构)中声明的属性的名称。 ref 值必须是限定名 (QName)。 类型可以包括命名空间前缀。 Name 和 ref 属性不能同时出现。 如果 ref 出现,则 simpleType 元素、form 和 type 不能出现。若要在复杂类型内使用现有属性定义声明一个属性,请使用 ref 属性指定现有属性定义。
<xs:attribute name="mybaseattribute"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:maxInclusive value="1000"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:complexType name="myComplexType"> <xs:attribute ref="mybaseattribute"/> </xs:complexType>
可选。
type
在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型或简单类型的名称。 该类型必须是 QName。 类型可以包括命名空间前缀。 type 属性只能在内容不包含 simpleType 元素时出现。可选。
use
一个指示符,指示如何使用属性。如果指定,该属性必须具有以下值之一。
optional
属性是可选的并且可以具有任何值。 这是默认设置。 以下是等价项。
<xs:attribute name="myattr" type="xs:string"/> <xs:attribute name="myattr" type="xs:string" use="optional"/>
prohibited
不能使用属性。 该特性用于其他复杂类型的限制中以禁止使用现有特性。
<xs:complexType name="A"> <xs:attribute name="x" type="xs:NCName"/> <xs:attribute name="y" type="xs:QName"/> </xs:complexType>
<xs:complexType name="B"> <xs:complexContent> <xs:restriction base="xs:A"> <xs:attribute name="x" use="required" /> <xs:attribute name="y" use="prohibited"/> </xs:restriction> </xs:complexContent> </xs:complexType>
required
属性必须出现一次。
该特性是必需的并且可以包含该特性的此类型定义允许的任何值。
<xs:attribute name="myattr" type="xs:string" use="required"/>
该属性用于其他复杂类型的限制或扩展中,以要求出现指定的一个或多个属性。
<xs:complexType name="A"> <xs:attribute name="x" type="xs:NCName"/> <xs:attribute name="y" type="xs:QName"/> </xs:complexType>
<xs:complexType name="B"> <xs:complexContent> <xs:restriction base="xs:A"> <xs:attribute name="x" use="required" /> <xs:attribute name="y" use="prohibited"/> </xs:restriction> </xs:complexContent> </xs:complexType>
如果将该属性声明为全局属性(其父元素是 schema),则对于该架构中的所有元素都需要该属性。
可选。
元素信息
出现次数 |
在 schema 元素中定义一次。 在复杂类型或属性组中引用多次。 |
父元素 |
attributeGroup、schema、complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent) |
内容 |
备注
属性声明将名称与类型定义关联,类型定义可以是内置数据类型或简单类型。
属性声明可作为 schema、complexType 和 attributeGroup 元素(具有全局范围)的子元素出现,或在复杂类型定义内出现。 对于复杂类型,属性声明可作为本地声明或对具有全局范围的属性的引用出现。
此外,属性可以在 attributeGroup 和 complexType 元素内的引用中出现。
示例
在以下示例中,通过对内置类型的引用声明一个属性,该属性的默认值为 test,该属性用在 complexType 元素中。
以下示例中在 complexType 元素内直接声明了一个 required 属性。
以下示例中声明了一个属性,具体方法是:从内置 integer 类型(通过限制)进行派生,将值的范围限制在 "60"
和 "95"
之间(包括 60 和 95)。
在以下示例中,属性被声明为包含十进制数值的列表。 (这允许诸如 shoeSizes="10.5 9 8 11" 之类的属性包含值 10.5、9、8 和 11 的列表)。
其他资源
有关更多信息,请参见“W3C XML Schema Part 1: Structures Recommendation”(W3C XML 架构第 1 部分:结构建议),网址为:www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-all。