XML Schema attributeElement


XML Schema 参考手册Complete XML Schema Reference Manual

Definition and Usage

The attribute element defines an attribute.

Element Information

  • Parent element(s): attributeGroup, schema, complexType, restriction (both simpleContent and complexContent), extension (both simpleContent and complexContent)

Syntax

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

(The ? sign declares that the element can appear zero or one time within the attribute element.)

Attributes Description
default Optional. Specifies the default value of the attribute. The default and fixed attributes cannot both appear.
fixed Optional. Specifies the fixed value of the attribute. The default and fixed attributes cannot both appear.
form

Optional. Specifies the format of the attribute. The default value is the value of the attributeFormDefault attribute of the schema element containing the attribute. Can be set to the following values:

  • "qualified" - Indicates that this attribute must be qualified by a namespace prefix and the attribute's colonless name (NCName).
  • "unqualified" - Indicates that this attribute does not need to be qualified by a namespace prefix, and does not need to match the attribute's colonless name (NCName), i.e., the local name.
id Optional. Specifies a unique ID for the element.
name Optional. Specifies the name of the attribute. The name and ref attributes cannot both appear.
ref Optional. Specifies a reference to the specified attribute. The name and ref attributes cannot both appear. If ref appears, the simpleType element, form, and type cannot appear.
type Optional. Specifies a built-in data type or simple type. The type attribute can only appear when the content does not contain a simpleType element.
use

Optional. Specifies how the attribute is used. Can be set to the following values:

  • optional - The attribute is optional and can have any value (default).
  • prohibited - The attribute cannot be used.
  • required - The attribute is required.
any attributes Optional. Specifies any other attribute with a non-schema namespace.

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

The above example indicates that the "code" attribute has a restriction. The only acceptable values are two letters from uppercase A to Z.

Example 2

To declare an attribute using an existing attribute definition in a complex type, use the ref attribute:

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

Example 3

An attribute can have either a default value or a specified fixed value. When no other value is specified, the default value is automatically assigned to the attribute. In the example below, the default value is "EN":

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

When no other value is specified, the fixed value is automatically assigned to the attribute. However, unlike the default value, if you specify a value other than the fixed value for the attribute, the document will be validated as invalid. In the example below, the fixed value is "EN":

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

Example 4

All attributes are optional by default. To explicitly specify an attribute as optional, use the "use" attribute:

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

Making an attribute required:

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


XML Schema 参考手册Complete XML Schema Reference Manual Other extensions