XML Schema Elements
XML Schema, also called XSD (XML Schema Definition), defines the structure and content rules for an XML document. Unlike DTD, XSD is written in XML itself and supports data types, making it far more powerful. At the heart of every XSD is the element declaration — the rule that defines what an element looks like and what it contains.
Basic Element Declaration
Every element you want to use in an XML file must be declared in the Schema using the xs:element tag.
<xs:element name="city" type="xs:string"/>
This declares an element named city that holds a text string. In the XML file, this element looks like:
<city>Bangalore</city>
Common XSD Data Types for Elements
| XSD Type | Meaning | Example Value |
|---|---|---|
| xs:string | Any text | Hello World |
| xs:integer | Whole number | 42 |
| xs:decimal | Number with decimals | 19.99 |
| xs:boolean | True or false | true |
| xs:date | Calendar date | 2024-03-15 |
| xs:time | Time of day | 14:30:00 |
| xs:dateTime | Date and time together | 2024-03-15T14:30:00 |
Global vs Local Element Declarations
Global Elements
A global element is declared as a direct child of the xs:schema root. Any other element in the Schema can reference it. Global elements can serve as the root element of an XML document.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- Global element --> <xs:element name="catalog" type="CatalogType"/> <xs:element name="product" type="ProductType"/> </xs:schema>
Local Elements
A local element is declared inside a complex type definition. It only exists in that specific context. The same name can mean different things in different complex types.
<xs:complexType name="BookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/> <!-- local -->
<xs:element name="author" type="xs:string"/> <!-- local -->
<xs:element name="pages" type="xs:integer"/> <!-- local -->
</xs:sequence>
</xs:complexType>
minOccurs and maxOccurs
These two attributes control how many times an element appears. They are the XSD equivalent of DTD's ?, *, and + indicators.
| Behavior | minOccurs | maxOccurs |
|---|---|---|
| Exactly once (default) | 1 | 1 |
| Optional (0 or 1) | 0 | 1 |
| One or more | 1 | unbounded |
| Zero or more | 0 | unbounded |
| Exactly 3 times | 3 | 3 |
| Between 2 and 5 times | 2 | 5 |
<xs:element name="phoneNumber" type="xs:string"
minOccurs="1" maxOccurs="3"/>
This means a person can have between 1 and 3 phone numbers.
Element with a Default Value
A default value applies when the element appears in the XML but is empty.
<xs:element name="currency" type="xs:string" default="INR"/>
Element with a Fixed Value
A fixed value locks the element to one specific value. The element must either be absent or contain exactly that value.
<xs:element name="country" type="xs:string" fixed="India"/>
Referencing a Global Element
Instead of rewriting an element declaration, use the ref attribute to reference a globally declared element.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="address" type="xs:string"/>
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element ref="address"/> <!-- reuses the global address element -->
</xs:sequence>
</xs:complexType>
</xs:schema>
A Complete Schema Example
A university course registration system:
Schema: registration.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="registrations">
<xs:complexType>
<xs:sequence>
<xs:element name="student" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="studentID" type="xs:string"/>
<xs:element name="fullName" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="course" type="xs:string"
minOccurs="1" maxOccurs="5"/>
<xs:element name="enrollDate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid XML: registration.xml
<?xml version="1.0" encoding="UTF-8"?>
<registrations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="registration.xsd">
<student>
<studentID>STU-2024-001</studentID>
<fullName>Aisha Mathur</fullName>
<email>aisha@university.edu</email>
<course>XML and Web Technologies</course>
<course>Database Design</course>
<enrollDate>2024-01-10</enrollDate>
</student>
</registrations>
Element Declaration Diagram
<xs:element name="course" type="xs:string" minOccurs="1" maxOccurs="5"/>
| | | |
| | | +-- max 5 courses allowed
| | +------------------ at least 1 required
| +--------------------------------- must be text
+------------------------------------------------- element name
Key Points to Remember
- xs:element declares an element in an XML Schema.
- The type attribute sets the data type — xs:string, xs:integer, xs:date, and many more.
- Global elements are declared at the schema root and can be referenced anywhere.
- Local elements are declared inside complex types and exist only in that context.
- minOccurs and maxOccurs control how many times an element appears.
- Use "unbounded" for maxOccurs to allow unlimited repetition.
- default and fixed control the element value when it is absent or must be locked.
