XSD Generator

Generate XML Schema from XML samples

code

XML Document (Input)

1
description

XSD Schema (Output)

Provide XML input and click "Generate XSD Schema" to see the output here.
Understanding XSD Schemas
TL;DR

XSD formally describes the structure, content, and constraints of an XML document. It's the blueprint that defines which elements, attributes, and data types are allowed.

What is XSD?

XSD (XML Schema Definition) is a W3C standard for describing the structure and constraints of XML documents. Think of it as a contract: the XSD defines what elements, attributes, and data types are allowed, and an XML validator checks whether a given document complies.

Before XSD, developers used DTDs (Document Type Definitions) to describe XML structure. DTDs were simple but limited — they couldn’t express data types (an element’s content was just “text”), couldn’t define complex constraints, and used a non-XML syntax. XSD was created to address all of these shortcomings while being expressed in XML itself.

XSD is foundational to enterprise integration. It powers SOAP web services (WSDL files reference XSD schemas), financial messaging standards like ISO 20022 and SWIFT MX, healthcare standards like HL7 CDA, and government data exchange formats worldwide.

Elements and Complex Types

XSD schemas are built from two fundamental concepts: elements and types.

An element declaration defines a named piece of data that can appear in an XML document:

<xs:element name="email" type="xs:string"/>

A complex type defines an element that contains child elements or attributes:

<xs:complexType name="PersonType">
  <xs:sequence>
    <xs:element name="firstName" type="xs:string"/>
    <xs:element name="lastName" type="xs:string"/>
    <xs:element name="email" type="xs:string" minOccurs="0"/>
  </xs:sequence>
  <xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>

The compositors (xs:sequence, xs:choice, xs:all) control how child elements appear:

  • xs:sequence — Children must appear in the specified order
  • xs:choice — Exactly one of the children must appear
  • xs:all — All children must appear, but in any order
XML to XSD Schema Mapping A simple XML document on the left with person, name, and age elements, mapped with arrows to corresponding XSD declarations on the right. XML Document <person> <name> Alice </name> <age> 30 </age> </person> XSD Schema <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>

Built-in Data Types

XSD provides a rich set of built-in data types that go far beyond DTD’s “everything is text” approach:

TypeDescriptionExample Values
xs:stringUnicode text"Hello", "Cafe\u0301"
xs:integerWhole number (unbounded)42, -7, 0
xs:decimalExact decimal number3.14, -0.001
xs:booleanTrue or falsetrue, false, 1, 0
xs:dateCalendar date (ISO 8601)2024-03-15
xs:dateTimeDate and time with timezone2024-03-15T14:30:00Z
xs:anyURIURI referencehttps://example.com

You can further constrain these types using facets — restrictions like minInclusive, maxInclusive, pattern, enumeration, minLength, and maxLength. For example, to define an age between 0 and 150:

<xs:simpleType name="AgeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="150"/>
  </xs:restriction>
</xs:simpleType>

XSD vs DTD vs RELAX NG

FeatureDTDXSDRELAX NG
SyntaxNon-XMLXMLXML or compact
Data typesNone44+ built-inExtensible
NamespacesLimitedFull supportFull support
ConstraintsBasicRich (facets, keys, identity)Moderate
Industry adoptionLegacyDominant (SOAP, ISO 20022)Niche (DocBook, TEI)
ComplexityLowHighMedium
W3C StandardYes (XML 1.0)YesOASIS (ISO/IEC 19757-2)

DTD is the original XML schema language, defined as part of the XML 1.0 specification itself. It uses a non-XML syntax and has no concept of data types — all element content is simply text. DTDs are still used in legacy systems and for declaring entities, but they are inadequate for modern validation needs.

XSD is the W3C’s answer to DTD’s limitations. It offers a comprehensive type system, namespace support, and powerful constraint mechanisms. However, its verbosity and complexity are frequent criticisms — even simple schemas can be verbose.

RELAX NG is a third option that aims for a middle ground: more powerful than DTD, simpler than XSD. It offers both an XML syntax and a more readable compact syntax. While technically superior in many ways, it has not achieved XSD’s market dominance and is primarily used in publishing (DocBook) and digital humanities (TEI).

Common Use Cases

  • SOAP web services: WSDL files use XSD to define the structure of request and response messages exchanged between services
  • Financial messaging (ISO 20022): The global financial messaging standard uses XSD to define every message type — from payment initiation (pain.001) to account statements (camt.053)
  • Configuration validation: Application config files in XML can be validated against XSD to catch errors before deployment
  • Data interchange: B2B integrations use XSD to formalize the contract between trading partners, ensuring both sides produce and consume conforming documents
  • Code generation: Tools like JAXB (Java), xsd.exe (.NET), and xmlschema (Python) generate classes from XSD, enabling type-safe XML processing without manual parsing

Try These Examples

Simple XSD Defining a Person Valid

A minimal XSD schema defining a 'person' element with required 'name' (string) and 'age' (integer) child elements in sequence. Any XML document with a <person> root containing these two elements in order will validate.

<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>
XML Violating Schema Invalid

This XML has the correct structure but 'not-a-number' is not a valid xs:integer. The XSD validator will reject this document with a type constraint violation on the 'age' element.

<person><name>Alice</name><age>not-a-number</age></person>