XSD How To
Step-by-step guide to creating and using XSD schemas
🚀 How to Create XSD Schemas
Creating XSD schemas involves defining elements, attributes, and data types to validate XML documents. Learn the practical steps to build schemas from scratch.
<!-- Basic XSD template -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Your definitions here -->
</xs:schema>
Creating Your First XSD
Follow these simple steps to create a working XSD schema. Start with a basic structure and gradually add complexity as needed.
Steps to Create XSD:
- Create a new file with .xsd extension
- Add the XML declaration and schema root
- Define your elements and types
- Save and reference in your XML
🔹 Step 1: Create the Schema File
Start by creating a new file named "person.xsd" with the basic schema structure.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Schema content goes here -->
</xs:schema>
🔹 Step 2: Define Simple Elements
Add element definitions with data types. Simple elements contain only text and no child elements or attributes.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Define simple elements -->
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="email" type="xs:string"/>
</xs:schema>
Valid XML:
<firstname>John</firstname>
<lastname>Smith</lastname>
<age>30</age>
<email>[email protected]</email>
🔹 Step 3: Create Complex Elements
Combine simple elements into complex structures using complexType and sequence. This creates parent-child relationships in your XML.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid XML:
<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
<age>30</age>
</person>
🔹 Step 4: Link XSD to XML
Reference your XSD schema in the XML document using schema location attributes. This tells validators which schema to use.
<?xml version="1.0" encoding="UTF-8"?>
<person
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="person.xsd">
<firstname>John</firstname>
<lastname>Smith</lastname>
<age>30</age>
</person>
🔹 Complete Example
Here's a complete working example with both XSD and XML files.
📄 employee.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="position" type="xs:string"/>
<xs:element name="salary" type="xs:decimal"/>
<xs:element name="hireDate" type="xs:date"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
📄 employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<employee
id="12345"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
<name>Jane Doe</name>
<position>Software Engineer</position>
<salary>75000.00</salary>
<hireDate>2024-01-15</hireDate>
</employee>
🔹 Validating XML with XSD
Use online validators or programming tools to check if your XML follows the XSD schema rules.
Online Validators
Quick browser-based validation
Code Editors
Built-in validation features
Programming
Validate in your code
Command Line
Terminal-based tools
🔹 Common Mistakes to Avoid
Watch out for these common errors when creating XSD schemas.
❌ Common Errors:
- Wrong namespace: Always use http://www.w3.org/2001/XMLSchema
- Missing xs: prefix: All XSD elements need the xs: prefix
- Incorrect file reference: Check the path in xsi:noNamespaceSchemaLocation
- Type mismatches: Ensure data types match between XSD and XML
- Element order: Elements must appear in the order defined in xs:sequence