XSD Attributes

Defining and using attributes in XML schemas

🏷️ XSD Attributes

Attributes provide additional information about elements. XSD attributes define what attributes elements can have, their data types, and whether they're required or optional.


<!-- Simple attribute definition -->
<xs:attribute name="id" type="xs:integer" use="required"/>
                                    

Valid XML:

<product id="12345">Laptop</product>

Attribute Properties

Attributes have several properties that control their behavior and validation. Understanding these properties helps create robust XML schemas.

📛

Name

Identifies the attribute

<xs:attribute name="color"/>
🔤

Type

Defines the data type

<xs:attribute type="xs:string"/>

Use

Required, optional, or prohibited

<xs:attribute use="required"/>
💾

Default/Fixed

Sets default or fixed values

<xs:attribute default="blue"/>

🔹 Simple Attribute Definition

Define attributes with name and type for basic validation.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="product">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <!-- Simple attribute -->
          <xs:attribute name="id" type="xs:integer"/>
          <xs:attribute name="category" type="xs:string"/>
          <xs:attribute name="price" type="xs:decimal"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<product id="101" category="Electronics" price="999.99">
  Laptop Computer
</product>

🔹 Required vs Optional Attributes

Control whether attributes must be present using the "use" property.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="user">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
      </xs:sequence>
      
      <!-- Required attribute -->
      <xs:attribute name="id" type="xs:integer" use="required"/>
      
      <!-- Optional attribute (default) -->
      <xs:attribute name="email" type="xs:string" use="optional"/>
      
      <!-- Prohibited attribute -->
      <xs:attribute name="legacy" type="xs:string" use="prohibited"/>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<!-- id is required, email is optional -->
<user id="12345" email="[email protected]">
  <name>John Doe</name>
</user>

<!-- Also valid without email -->
<user id="67890">
  <name>Jane Smith</name>
</user>

🔹 Default and Fixed Values

Set default values for optional attributes or fixed values that cannot change.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="item">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <!-- Default value if not specified -->
          <xs:attribute name="quantity" type="xs:integer" default="1"/>
          
          <!-- Fixed value, cannot be changed -->
          <xs:attribute name="currency" type="xs:string" fixed="USD"/>
          
          <!-- Optional with default -->
          <xs:attribute name="available" type="xs:boolean" default="true"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<!-- Uses default quantity=1, available=true -->
<item currency="USD">Keyboard</item>

<!-- Overrides default quantity -->
<item quantity="5" currency="USD" available="false">
  Mouse
</item>

🔹 Attributes with Restrictions

Apply restrictions to attribute values using simpleType definitions.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="shirt">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <!-- Attribute with enumeration -->
          <xs:attribute name="size">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:enumeration value="S"/>
                <xs:enumeration value="M"/>
                <xs:enumeration value="L"/>
                <xs:enumeration value="XL"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
          
          <!-- Attribute with pattern -->
          <xs:attribute name="sku">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:pattern value="[A-Z]{3}-[0-9]{4}"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<shirt size="L" sku="TSH-1234">Blue Cotton Shirt</shirt>

🔹 Attribute Groups

Reuse sets of attributes across multiple elements using attribute groups.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Define attribute group -->
  <xs:attributeGroup name="commonAttributes">
    <xs:attribute name="id" type="xs:integer" use="required"/>
    <xs:attribute name="created" type="xs:date"/>
    <xs:attribute name="modified" type="xs:date"/>
  </xs:attributeGroup>
  
  <!-- Use in multiple elements -->
  <xs:element name="article">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string"/>
      </xs:sequence>
      <xs:attributeGroup ref="commonAttributes"/>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="comment">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="text" type="xs:string"/>
      </xs:sequence>
      <xs:attributeGroup ref="commonAttributes"/>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<article id="1" created="2024-01-15" modified="2024-01-20">
  <title>XSD Tutorial</title>
</article>

<comment id="2" created="2024-01-16">
  <text>Great article!</text>
</comment>

🔹 Attributes on Empty Elements

Elements with only attributes and no content are common in XML.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="link">
    <xs:complexType>
      <xs:attribute name="href" type="xs:anyURI" use="required"/>
      <xs:attribute name="title" type="xs:string"/>
      <xs:attribute name="target" type="xs:string" default="_self"/>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="image">
    <xs:complexType>
      <xs:attribute name="src" type="xs:anyURI" use="required"/>
      <xs:attribute name="alt" type="xs:string" use="required"/>
      <xs:attribute name="width" type="xs:positiveInteger"/>
      <xs:attribute name="height" type="xs:positiveInteger"/>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<link href="https://example.com" title="Example" target="_blank"/>

<image src="photo.jpg" alt="Photo" width="800" height="600"/>

🔹 Attribute Best Practices

Follow these guidelines when working with attributes in XSD.

✅ Best Practices:

  • Use for metadata: Attributes work best for IDs, dates, and flags
  • Keep simple: Attributes should contain simple data, not complex structures
  • Mark required clearly: Use use="required" for mandatory attributes
  • Provide defaults: Set sensible default values when appropriate
  • Use restrictions: Limit values with enumerations or patterns
  • Group common attributes: Use attributeGroup for reusability
  • Avoid too many: Too many attributes make XML hard to read

🧠 Test Your Knowledge

What does use="required" mean for an attribute?