XSD Elements

Defining and working with XML elements in schemas

🧩 XSD Elements

Elements are the building blocks of XML documents. XSD elements define what elements can appear in XML, their data types, and how they relate to each other.


<!-- Simple element definition -->
<xs:element name="title" type="xs:string"/>
                                    

Valid XML:

<title>Learning XSD</title>

Types of Elements

XSD supports different types of elements for various data structures. Understanding these types helps you model complex XML documents effectively.

📝

Simple Elements

Contain only text, no children

<xs:element name="age" type="xs:integer"/>
🏗️

Complex Elements

Contain child elements or attributes

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>...</xs:sequence>
  </xs:complexType>
</xs:element>
🔄

Empty Elements

No content, only attributes

<xs:element name="break">
  <xs:complexType/>
</xs:element>
🔀

Mixed Content

Text and child elements combined

<xs:complexType mixed="true">
  <xs:sequence>...</xs:sequence>
</xs:complexType>

🔹 Simple Elements

Simple elements contain only text data with no child elements or attributes.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- String element -->
  <xs:element name="name" type="xs:string"/>
  
  <!-- Integer element -->
  <xs:element name="age" type="xs:integer"/>
  
  <!-- Decimal element -->
  <xs:element name="price" type="xs:decimal"/>
  
  <!-- Boolean element -->
  <xs:element name="active" type="xs:boolean"/>
  
  <!-- Date element -->
  <xs:element name="birthdate" type="xs:date"/>
  
</xs:schema>

Valid XML:

<name>John Smith</name>
<age>30</age>
<price>19.99</price>
<active>true</active>
<birthdate>1994-05-15</birthdate>

🔹 Complex Elements with Sequence

Complex elements contain child elements in a specific order using xs:sequence.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="street" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="zip" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<address>
  <street>123 Main St</street>
  <city>New York</city>
  <zip>10001</zip>
  <country>USA</country>
</address>

🔹 Element Occurrence Indicators

Control how many times an element can appear using minOccurs and maxOccurs attributes.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <!-- Required element (default) -->
        <xs:element name="name" type="xs:string"/>
        
        <!-- Optional element (0 or 1) -->
        <xs:element name="description" type="xs:string" minOccurs="0"/>
        
        <!-- Multiple books (1 or more) -->
        <xs:element name="book" type="xs:string" maxOccurs="unbounded"/>
        
        <!-- Optional multiple reviews (0 or more) -->
        <xs:element name="review" type="xs:string" 
                    minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<library>
  <name>City Library</name>
  <book>XSD Guide</book>
  <book>XML Basics</book>
  <book>Web Development</book>
  <review>Great collection!</review>
</library>

🔹 Default and Fixed Values

Set default values or fixed values for elements.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="product">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        
        <!-- Default value if not provided -->
        <xs:element name="quantity" type="xs:integer" default="1"/>
        
        <!-- Fixed value, cannot be changed -->
        <xs:element name="currency" type="xs:string" fixed="USD"/>
        
        <!-- Optional with default -->
        <xs:element name="inStock" type="xs:boolean" 
                    default="true" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<product>
  <name>Laptop</name>
  <!-- quantity defaults to 1 -->
  <currency>USD</currency>
  <!-- inStock defaults to true -->
</product>

🔹 Empty Elements

Define elements that have no content but may have attributes.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="image">
    <xs:complexType>
      <xs:attribute name="src" type="xs:string" use="required"/>
      <xs:attribute name="alt" type="xs:string"/>
      <xs:attribute name="width" type="xs:integer"/>
      <xs:attribute name="height" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

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

🔹 Mixed Content Elements

Elements that can contain both text and child elements.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="paragraph">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element name="bold" type="xs:string" 
                    minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="italic" type="xs:string" 
                    minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<paragraph>
  This is <bold>important</bold> text with 
  <italic>emphasis</italic> included.
</paragraph>

🔹 Element Reference

Reuse element definitions by referencing them instead of redefining.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <!-- Define element once -->
  <xs:element name="email" type="xs:string"/>
  
  <!-- Reference it multiple times -->
  <xs:element name="contact">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="email"/>
        <xs:element name="phone" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="newsletter">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="email"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

🧠 Test Your Knowledge

What does maxOccurs="unbounded" mean?