XML Schema attribute element
Definition and Usage
The attribute element defines an attribute.
Element Information
Occurrence | Defined once in the schema element. Referenced multiple times in complex types or attribute groups. |
Parent Element | attributeGroup, schema, complexType, restriction (simpleContent), extension (simpleContent), restriction (complexContent), extension (complexContent) |
Content | annotation, simpleType |
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 ? symbol indicates that this element can appear zero or one time in the attribute element.)
Attribute
default
Optional. Specifies the default value of the attribute. The default and fixed attributes cannot appear at the same time.
fixed
Optional. Specifies the fixed value of the attribute. The default and fixed attributes cannot appear at the same time.
form
Optional. Specifies the format of the attribute. The default value is the value of the attributeFormDefault attribute of the schema element containing this attribute. It can be set to the following values:
- "qualified" - Indicates that this attribute must be qualified by a namespace prefix and the non-colon name (NCName) of the attribute.
- "unqualified" - Indicates that this attribute does not need to be qualified by a namespace prefix and does not need to match the non-colon name (NCName) of this attribute, that is, the local name.
id
Optional. Specify the unique ID of the element.
name
Optional. Specify the name of the attribute. The name and ref attributes cannot appear at the same time.
ref
Optional. Specify the reference to the specified attribute. The name and ref attributes cannot appear at the same time. If ref appears, the simpleType element, form, and type cannot appear.
type
Optional. Specify the built-in data type or simple type. The type attribute can only appear when the content does not contain a simpleType element.
use
Optional. Specify how to use this attribute. The following values can be set:
- 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. Specify any other attributes with non-schema namespaces.
Instance
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 the uppercase letters A to Z.
Example 2
To declare an attribute using an existing attribute definition within 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 both a default value and a specified fixed value. If no other value is specified, the default value will be automatically assigned to the attribute. In the following example, the default value is "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
If no other value is specified, the attribute will be automatically assigned a fixed value. However, unlike the default value, if you specify a value other than the fixed value for the attribute, the document will validate it as invalid. In the following example, 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, please use the "use" attribute:
<xs:attribute name="lang" type="xs:string" use="optional"/>
Make the attribute mandatory:
<xs:attribute name="lang" type="xs:string" use="required"/>