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.
xs:string — an untyped element defaults to allowing any content, which weakens your validation.Grouping Child Elements with sequence, choice and all
Most real elements contain other elements. Inside a complexType you decide how those children are arranged using one of three compositors:
| Compositor | Meaning |
|---|---|
xs:sequence | Children must appear in exactly the listed order |
xs:choice | Only one of the listed children may appear |
xs:all | Each child may appear at most once, in any order |
Here is a full schema for a course element that combines a sequence, a choice and attributes. Note that attribute declarations always come after the compositor inside the complex type:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="course">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:choice>
<xs:element name="online" type="xs:boolean"/>
<xs:element name="campus" type="xs:string"/>
</xs:choice>
<xs:element name="module" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="level" type="levelType" default="beginner"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="levelType">
<xs:restriction base="xs:string">
<xs:enumeration value="beginner"/>
<xs:enumeration value="intermediate"/>
<xs:enumeration value="advanced"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
The levelType simple type limits the attribute to three allowed words. Restrictions can also use xs:pattern for regular expressions, or xs:minInclusive and xs:maxInclusive for number ranges.
Elements That Have Text and Attributes Together
An element like <price currency="INR">4999</price> has a text value and an attribute. Because it has an attribute it needs a complex type, but because it has no child elements it uses simpleContent:
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Validating an Instance Document
Write a matching XML file and validate it. On Linux or macOS, the xmllint tool checks a document against a schema in one command:
xmllint --noout --schema course.xsd course.xml
If everything is valid, it prints that the file validates; otherwise it shows the line and the rule that failed. In Java, the javax.xml.validation package does the same job inside your application.
Mistakes That Break Schemas
- Placing
xs:attributebefore the sequence, which is not allowed. - Using both
defaultandfixedon one attribute, or combiningdefaultwithuse="required". - Forgetting that
minOccursdefaults to 1, so an element you meant to be optional becomes mandatory. - Repeating the same inline type in many places; define a named type once and reuse it with
type="...".
Element or Attribute: How to Decide
A common design question is whether a piece of data should be a child element or an attribute. Use an attribute for short, single values that describe the element, such as an identifier, a code or a flag. Use a child element when the value could grow into structured data, may repeat, needs its own attributes, or contains long text. Elements are also easier to extend later without breaking existing documents, so when you are unsure, an element is usually the safer choice. Whichever style you pick, apply it consistently across the whole schema so other developers can predict the structure.