Xsd How To in XML
? How to Use XSD in XML — Step-by-Step Guide
An XSD (XML Schema Definition) file defines the structure, types, and rules for an XML document. Linking an XML file to its XSD allows validation — ensuring the XML is well-formed and valid.
? Step 1: Create Your XSD Schema
Save this as example.xsd:
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>? Step 2: Create the XML Document
Save this as example.xml, and link it to the XSD using xsi:noNamespaceSchemaLocation.
<?xml version="1.0" encoding="UTF-8"?><person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="example.xsd"> <name>John Doe</name> <age>35</age></person>? Step 3: Validate XML Against XSD
? Tools to validate:
Online Validators: https://www.freeformatter.com/xml-validator-xsd.html
VS Code with XML extension
Notepad++ with XML Tools plugin
Java or Python scripts using XML libraries
? XSD Linking Options
?? No Namespace
xsi:noNamespaceSchemaLocation="example.xsd"?? With Namespace (advanced)
<person xmlns="http://example.com/person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.com/person person.xsd">Then your XSD must define the targetNamespace.
? Summary: How to Use XSD in XML
| Step | Action |
|---|---|
| 1 | Write XSD schema (.xsd) |
| 2 | Write XML document (.xml) |
| 3 | Link XSD using xsi: in XML |
| 4 | Validate using a tool |
Would you like a downloadable .zip of this example, or want to include namespaces, attributes, or validation rules like min/max values?