Declares an attribute.
<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>
Optional.
Optional.
If the value is unqualified, this attribute is not required to be qualified with the namespace prefix and is matched against the no-colon-name (NCName) of the attribute (local name).
If the value is qualified, this attribute must be qualified by combining the targetNamespace of the schema and the NCName of the attribute.
Optional.
Optional.
When an XML document is validated against a schema, each attribute in the document is validated against an attribute element in the schema.
Optional.
To declare an attribute using an existing attribute definition within a complex type, use the ref attribute to specify the existing attribute definition.
<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>
Optional.
Optional.
If specified, this attribute must have one of the following values.
optional | Attribute is optional and may have any value. This is the default. The following are equivalent.
<xs:attribute name="myattr" type="xs:string"/> <xs:attribute name="myattr" type="xs:string" use="optional"/> |
prohibited | Attribute cannot be used. This attribute is used in a restriction of another complex type to prohibit the use of existing attributes.
<xs:attributeGroup name="A"> <xs:attribute name="x" type="xs:NCName"/> <xs:attribute name="y" type="xs:QName"/> </xs:attributeGroup> <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 | Attribute must appear once.
The attribute is required and can contain any value allowed by this type definition of the attribute. <xs:attribute name="myattr" type="xs:string" use="required"/> This attribute is used in a restriction or extension of another complex type, to require that a specified attribute or attributes are present. <xs:attributeGroup name="A"> <xs:attribute name="x" type="xs:NCName"/> <xs:attribute name="y" type="xs:QName"/> </xs:attributeGroup> <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> If the attribute is declared as global (its parent element is schema), this attribute is required on all elements in the schema. |
Optional.
Number of occurrences | Defined one time in the schema element. Referred to multiple times in complex types or attribute groups. |
Parent elements | attributeGroup, schema, complexType, restriction (simpleContent), extension (simpleContent), restriction (complexContent), extension (complexContent) |
Contents | annotation, simpleType |
An attribute declaration associates a name with a type definition, which can be a built-in data type or a simple type.
Attribute declarations can be present as child elements of the schema, complexType, and attributeGroup elements (having global scope) or within complex type definitions. For complex types, attribute declarations can be present as local declarations or references to attributes with global scope.
In addition, attributes can appear by reference within attributeGroup and complexType elements.
In the following example, an attribute is declared by reference to a built-in type with a default value of test and used in a complexType element.
<xs:attribute name="mybaseattribute" type="xs:string" default="test" />
<xs:complexType name="myComplexType">
<xs:attribute ref="mybaseattribute"/>
</xs:complexType>
In the following example, a required attribute is declared directly within a complexType element.
<xs:complexType name="myComplexType">
<xs:attribute name="mybaseattribute" type="xs:string" use="required"/>
</xs:complexType>
In the following example, an attribute is declared by deriving from the built-in integer type (by restriction) and restricting the range of values to between "60"
and "95"
, inclusive.
<xs:attribute name="myHolidayLocationTemperature"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="60"/> <xs:maxInclusive value="95"/> </xs:restriction> </xs:simpleType> </xs:attribute>
In the following example, an attribute is declared as a list containing decimal values. (This allows an attribute such as shoeSizes="10.5 9 8 11" to contain a list of the values 10.5, 9, 8, and 11).
<xs:simpleType name="Sizes"> <xs:restriction base="xs:decimal"> <xs:enumeration value="10.5"/> <xs:enumeration value="9"/> <xs:enumeration value="8"/> <xs:enumeration value="11"/> </xs:restriction> </xs:simpleType> <xs:attribute name="shoeSizes"> <xs:simpleType> <xs:list itemType="Sizes"/> </xs:simpleType> </xs:attribute>
XML Schema Reference (XSD) | XML Schema Elements
For more information, see the W3C XML Schema Part 1: Structures Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-attribute.
W3C Namespaces in XML Recommendation