XML Schema Attributes
Attributes provide additional information about elements. In an XML Schema, you declare attributes using xs:attribute. XSD attribute declarations give you control over the attribute name, data type, whether it is required, and what default or fixed values it can hold — all far more precisely than DTD allows.
Basic Attribute Declaration Syntax
<xs:attribute name="attribute-name" type="xs:datatype"/>
Simple Example
<xs:attribute name="language" type="xs:string"/>
This declares an optional attribute named language that holds a text string.
use Attribute — Required, Optional, or Prohibited
The use attribute on xs:attribute controls whether the XML attribute is required, optional, or not allowed.
| Value | Meaning |
|---|---|
| optional | Attribute may or may not appear (default) |
| required | Attribute must appear on every element |
| prohibited | Attribute must NOT appear |
<xs:attribute name="productID" type="xs:string" use="required"/> <xs:attribute name="discount" type="xs:decimal" use="optional"/> <xs:attribute name="internalCode" type="xs:string" use="prohibited"/>
default and fixed Values
default
When the attribute is absent, the parser substitutes the default value. The element must be valid even without the attribute.
<xs:attribute name="currency" type="xs:string" default="INR"/>
fixed
The attribute must have exactly this value, or it must be absent. Any other value fails validation.
<xs:attribute name="country" type="xs:string" fixed="India"/>
Declaring Attributes Inside a Complex Type
Attributes always belong to a complex type. They are declared after the content model (xs:sequence, xs:all, etc.) but before the closing </xs:complexType> tag.
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="category" type="xs:string" use="optional"/>
<xs:attribute name="currency" type="xs:string" default="INR"/>
</xs:complexType>
Valid XML for this type
<product id="P001" category="electronics"> <name>Tablet 10"</name> <price>14999</price> </product>
xs:ID — Unique Attribute
xs:ID ensures the attribute value is unique across the entire document. It behaves like a primary key in a database table.
<xs:attribute name="empID" type="xs:ID" use="required"/>
Valid XML
<employee empID="E001">Asha</employee> <employee empID="E002">Biju</employee>
Invalid XML — duplicate ID
<employee empID="E001">Asha</employee> <employee empID="E001">Biju</employee>
xs:IDREF — Linking Elements
xs:IDREF means the attribute value must match an xs:ID value that exists somewhere in the document. This creates links between elements.
<xs:attribute name="managerRef" type="xs:IDREF" use="optional"/>
Example XML
<employees> <employee empID="E001">Asha</employee> <employee empID="E002" managerRef="E001">Biju</employee> </employees>
Biju's managerRef points to Asha's empID. The validator checks that E001 exists in the document.
Restricting Attribute Values
Use xs:simpleType with xs:restriction inside the attribute declaration to limit the allowed values.
Enumeration — restrict to a list of values
<xs:attribute name="status">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="active"/>
<xs:enumeration value="inactive"/>
<xs:enumeration value="suspended"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
Pattern — restrict to a format
<xs:attribute name="pinCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
This attribute accepts only exactly 6 digits — a valid Indian postal code format.
Global vs Local Attribute Declarations
Global Attribute
Declared at the schema root level and referenced using ref in multiple complex types.
<xs:attribute name="lang" type="xs:string" default="en"/>
<xs:complexType name="ArticleType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="lang"/>
</xs:complexType>
Complete Example — Employee Directory Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="directory">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
<xs:element name="salary" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="empID" type="xs:ID" use="required"/>
<xs:attribute name="joinDate" type="xs:date" use="required"/>
<xs:attribute name="status">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="active"/>
<xs:enumeration value="inactive"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Key Points to Remember
- xs:attribute declares an attribute in an XML Schema.
- use="required" forces the attribute to appear; use="optional" makes it optional.
- default provides a fallback value when the attribute is absent.
- fixed locks the attribute to one specific value permanently.
- xs:ID ensures the attribute value is unique across the document.
- xs:IDREF creates a cross-reference to an xs:ID value elsewhere in the document.
- Restrictions inside xs:simpleType limit attributes to enumerated values or patterns.
