XSD <schema> Element

Understanding the root element of XSD documents

📦 The <schema> Element

The <schema> element is the root element of every XSD document. It contains all element definitions, types, and attributes that define your XML structure.


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- All definitions go here -->
</xs:schema>
                                    

Schema Element Attributes

The schema element supports various attributes to configure namespaces, versions, and default behaviors. These attributes control how the schema is interpreted and applied.

🔗

xmlns:xs

Declares the XSD namespace

xmlns:xs="http://www.w3.org/2001/XMLSchema"
🎯

targetNamespace

Defines the schema's namespace

targetNamespace="http://example.com"
📌

elementFormDefault

Sets element qualification

elementFormDefault="qualified"
🔢

version

Specifies schema version

version="1.0"

🔹 Basic Schema Declaration

The simplest schema declaration includes only the required namespace attribute.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="message" type="xs:string"/>
  
</xs:schema>

Valid XML:

<message>Hello World</message>

🔹 Schema with Target Namespace

Use targetNamespace to create schemas for specific XML vocabularies. This helps avoid naming conflicts when combining multiple schemas.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.example.com/books"
  xmlns="http://www.example.com/books"
  elementFormDefault="qualified">
  
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="author" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Valid XML:

<book xmlns="http://www.example.com/books">
  <title>XSD Guide</title>
  <author>John Smith</author>
</book>

🔹 Schema Attributes Explained

Understanding each schema attribute helps you create well-structured and reusable schemas.

🔸 xmlns:xs (Required)

Declares the XML Schema namespace. Always use the standard W3C namespace URI.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

🔸 targetNamespace (Optional)

Defines the namespace for elements defined in this schema. Used when creating reusable schema libraries.

<xs:schema targetNamespace="http://www.mycompany.com/schema">

🔸 elementFormDefault (Optional)

Controls whether elements must be namespace-qualified. Values: "qualified" or "unqualified" (default).

<xs:schema elementFormDefault="qualified">

🔸 attributeFormDefault (Optional)

Controls whether attributes must be namespace-qualified. Values: "qualified" or "unqualified" (default).

<xs:schema attributeFormDefault="unqualified">

🔹 Multiple Namespace Example

Schemas can reference multiple namespaces for complex XML documents.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:tns="http://www.example.com/products"
  targetNamespace="http://www.example.com/products"
  elementFormDefault="qualified"
  version="1.0">
  
  <xs:element name="product">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="price" type="xs:decimal"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:ID" use="required"/>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

🔹 Schema Documentation

Add documentation to your schemas using annotation elements for better maintainability.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:annotation>
    <xs:documentation>
      This schema defines a customer record structure.
      Version: 1.0
      Author: Development Team
    </xs:documentation>
  </xs:annotation>
  
  <xs:element name="customer">
    <xs:annotation>
      <xs:documentation>
        Root element for customer data
      </xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="email" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

🔹 Schema Best Practices

Follow these guidelines when creating schema elements.

✅ Best Practices:

  • Always declare xmlns:xs: Required for all XSD elements
  • Use targetNamespace: For reusable, professional schemas
  • Set elementFormDefault: Explicitly define qualification behavior
  • Add version attribute: Track schema changes over time
  • Include documentation: Explain complex structures
  • Use meaningful namespaces: Follow your organization's URI conventions

🧠 Test Your Knowledge

Which attribute is required in the <schema> element?