XSD Elements and Attributes

Estimated study time: 18 minutes.

XSD gives you fine-grained control over how elements and attributes are declared, right down to their exact data type and how many times they can appear.

Declaring Elements

<xs:element name="title" type="xs:string"/>
<xs:element name="seats" type="xs:integer"/>
<xs:element name="startDate" type="xs:date"/>

Occurrence Constraints

<xs:element name="module" type="xs:string"
            minOccurs="1" maxOccurs="unbounded"/>

minOccurs and maxOccurs control how many times an element can repeat — unbounded means no upper limit.

Declaring Attributes

<xs:element name="course">
  <xs:complexType>
    <xs:attribute name="id" type="xs:string" use="required"/>
    <xs:attribute name="level" type="xs:string" default="beginner"/>
  </xs:complexType>
</xs:element>

Key Attribute Options

  • use="required" — the attribute must be present.
  • use="optional" — the attribute may be omitted (the default).
  • default="value" — used automatically when the attribute is omitted.
  • fixed="value" — the attribute's value is locked to this constant.
💡 Tip: Always give elements an explicit type, even xs:string — an untyped element defaults to allowing any content, which weakens your validation.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →