Skip to content

๐Ÿงพ Generate from XML Schema

Generate Python models from W3C XML Schema documents (.xsd).

๐Ÿš€ Quick Start

datamodel-codegen \
    --input purchase_order.xsd \
    --input-file-type xmlschema \
    --output-model-type pydantic_v2.BaseModel \
    --output model.py

๐Ÿ“ Example

purchase_order.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="purchaseOrder" type="PurchaseOrder"/>

  <xs:complexType name="PurchaseOrder">
    <xs:sequence>
      <xs:element name="id" type="xs:string"/>
      <xs:element name="total" type="xs:decimal"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

โœจ Generated model.py

from __future__ import annotations

from decimal import Decimal

from pydantic import BaseModel


class PurchaseOrder(BaseModel):
    id: str
    total: Decimal

Supported XML Schema Versions

The XML Schema parser supports XML Schema 1.0 and selected XML Schema 1.1 constructs. Use --schema-version auto to detect the version from versioning attributes and XSD 1.1 constructs, or pass --schema-version 1.0 / --schema-version 1.1 explicitly.

Supported XML Schema Features

The XML Schema parser converts XSD into the JSON Schema shape used by the normal model-generation pipeline. It supports the constructs needed to generate Python model definitions:

XSD construct Model generation behavior
Built-in simple types Maps XML Schema scalar types to Python scalar types
xs:complexType Generates a Python model class
xs:simpleType restrictions Generates constrained scalar fields where possible
xs:sequence, xs:choice, xs:all Generates fields from model particles
minOccurs, maxOccurs, nillable Preserves optionality, lists, and nullability
Attributes Generates model fields for XML attributes
xs:include, xs:import, xs:redefine, xs:override Resolves local schema composition
Namespaces Uses namespace context to avoid name collisions
Substitution groups and wildcards Generates compatible model shapes where possible

Limitations

The XML Schema input type is for generating Python model definitions. It does not implement XML parsing, XML serialization, or runtime XML validation.

๐Ÿ“– See Also