8️⃣ 🧩 XSD Schema (XML Schema Definition)
Estimated reading: 4 minutes 32 views

📘 XSD Example – Complete XML Schema and Instance Document

🧲 Introduction – Why Use an XSD Example?

Learning the theory behind XSD is important—but seeing a real, complete example is the best way to understand how XML Schema works in practice. A well-structured example shows how to define elements, attributes, data types, and validation rules, and how to link the schema to your XML document for seamless validation.

🎯 In this guide, you’ll learn:

  • How to create a complete XSD file with common elements and attributes
  • How to write an XML document that conforms to the schema
  • How to validate XML against XSD
  • Best practices for file structure and linking

📂 Folder Structure

project/
├── product.xsd      ← XML Schema
└── product.xml      ← XML file that uses the schema

🧾 product.xsd – XML Schema File

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

  <xs:element name="product">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:integer"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="price" type="xs:decimal"/>
        <xs:element name="category">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="electronics"/>
              <xs:enumeration value="furniture"/>
              <xs:enumeration value="clothing"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="currency" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

✅ This schema defines:

  • A root <product> element
  • Child elements: <id>, <name>, <price>, and <category>
  • An attribute currency on <product>
  • A restriction on the value of <category> using enumeration

📄 product.xml – Valid XML Document

<?xml version="1.0"?>
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="product.xsd"
         currency="USD">
  <id>101</id>
  <name>Wireless Headphones</name>
  <price>129.99</price>
  <category>electronics</category>
</product>

✅ This XML is valid:

  • It matches all element names and types
  • It includes the required attribute currency
  • It links to the XSD using xsi:noNamespaceSchemaLocation

🔍 Validating the XML

🧪 Tools You Can Use:

✅ These tools highlight mismatches like:

  • Missing elements or attributes
  • Wrong data types (e.g., a string in place of an integer)
  • Invalid values for restrictions (e.g., category not in list)

🧠 Best Practices

  • ✔️ Keep schema and XML files in the same directory for relative linking
  • ✔️ Use meaningful names and constraints for fields
  • ✔️ Include xsi:noNamespaceSchemaLocation in your XML for validation
  • ❌ Don’t skip required attributes if use="required" is set
  • ❌ Avoid hardcoding values unless necessary—prefer flexibility via enumerations

📌 Summary – Recap & Next Steps

This XSD example demonstrates how to structure and validate a complete XML document. It includes type enforcement, attribute declarations, and value restrictions—everything you need for a real-world XML + XSD implementation.

🔍 Key Takeaways:

  • Define structure and rules in .xsd
  • Link schema to XML with xsi:noNamespaceSchemaLocation
  • Validate with tools to catch structural or type errors

⚙️ Real-world relevance: Used in product feeds, invoice formats, B2B XML exchanges, form definitions, and config files.


❓ FAQs – XSD Example

❓ Is it required to link the schema in the XML?
✅ Yes, to validate using tools, use xsi:noNamespaceSchemaLocation="file.xsd".

❓ Can I define attributes and elements in the same complexType?
✅ Yes. Place elements in <xs:sequence> and attributes after it.

❓ What happens if the XML violates the schema?
✅ The document will fail validation—tools will show exact errors.

❓ Can I include optional elements?
✅ Yes, use minOccurs="0" on any <xs:element>.

❓ Can this structure be reused across multiple products?
✅ Yes. Define global types or reference reusable schemas via <xs:include>.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XSD Example

Or Copy Link

CONTENTS
Scroll to Top