Complex Type: Attribute Declaration

Complex type elements contain attributes. In an XML schema document, the syntax for attribute declaration is:

    <xs:attribute name="attribute_name" type="data_type"
    use="optional/required"/>

where:

  • attribute_name indicates the name of an attribute.
  • data_type indicates the data type specified for the attribute.
  • use indicates that the attribute can be optional or required within an element.

An attribute can contain only the data type that is specified while declaring the attribute. For example, in the code snippet on the right, LocationID is the name of an attribute and xs:integer indicates that LocationID can contain only numeric values. In addition, optional indicates that the LocationID attribute is optional within the element declaration.

<xs:schema xmlns:xs="http://www.w3.org/20 01/XMLSchema">
  <xs:element name= "DeptDetails">
    <xs:complexType>

      <xs:attribute name="LocationID"       type="xs:integer" use="optional" />
    </xs:complexType>
  </xs:element>

// Additional declarations...
Click the Next button to continue.