XML Schema attributeElement
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
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:
|
| 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:
|
| any attributes | Optional. Specifies any other attribute with a non-schema namespace. |
Example 1
<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: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":
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":
Example 4
All attributes are optional by default. To explicitly specify an attribute as optional, use the "use" attribute:
Making an attribute required:
Complete XML Schema Reference Manual Other extensions