<xsd:attribute> Element 

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>

Attributes

  • default
    Attribute has a default value. If the attribute is not specified in an instance within an XML document, the attribute has the value given. If the attribute is not present in the instance document, then the attribute should appear in the post-schema-validation infoset. Processors of the schema should act as if the attribute was specified with the default value if it was not actually in the instance document. Default and fixed attributes cannot both be present.

    Optional.

  • fixed
    Attribute has a fixed value. If the attribute is present in an instance document, its value must be equal to the fixed value given. If it is not present, then the attribute receives the supplied value. Default and fixed attributes cannot both be present.

    Optional.

  • form
    The form for the attribute. The default value is the value of the attributeFormDefault attribute of the schema element containing the attribute. The value must be one of the following strings: "qualified" or "unqualified".

    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.

  • id
    The ID of this element. The id value must be of type ID and be unique within the document containing this element.

    Optional.

  • name
    The name of the attribute. The name must be an NCName as defined in the XML Namespaces specification. Required if the containing element is the schema element. Name and ref attributes cannot both be present.

    When an XML document is validated against a schema, each attribute in the document is validated against an attribute element in the schema.

    Optional.

  • ref
    The name of an attribute declared in this schema (or another schema indicated by the specified namespace). The ref value must be a qualified name (QName). The type can include a namespace prefix. Name and ref attributes cannot both be present. If ref is present, simpleType element, form, and type cannot be present.

    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.

  • type
    The name of a built-in data type or a simple type defined in this schema (or another schema indicated by the specified namespace). The type must be a QName. The type can include a namespace prefix. The type attribute can only be present when the content does not contain a simpleType element.

    Optional.

  • use
    An indicator of how the attribute is used.

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

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

    If the attribute is declared as global (its parent element is schema), this attribute is required on all elements in the schema.

    Optional.

Element Information

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

Remarks

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.

Examples

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>

Other Resources

For more information see the W3C XML Schema Part 1: Structures Recommendation at www.w3.org/TR/2001/REC-xmlschema-1-20010502\#element-all.

See Also

Reference

XML Schemas (XSD) Reference
XML Schema Elements