A comprehensive library for parsing XML schemas and generating code based on them.
This project originated as a fork of xsd-parser-rs but has since evolved into a complete rewrite.
If you enjoy the project and would like to support my work, you can buy me a coffee or send a tip via PayPal. Thanks a lot! 😊
This library is built around a staged transformation pipeline that converts XML schemas into Rust source code. Each stage handles a specific level of abstraction and produces a well-defined intermediate representation. This makes the library highly flexible, testable, and suitable for advanced customization or tooling.
-
Parsing: The parsing stage is handled by the
Parsertype. It loads XML schemas from files or URLs and uses pluggableResolvers to fetch and preprocess schema definitions. The result is captured in aSchemasmodel, which stores namespaces, prefixes, and the raw schema structure needed for further processing. -
Interpreting:: Interpreting is carried out by the
Interpreter. This stage analyzes the schema definitions stored in theSchemasmodel and converts them into normalized, abstract type descriptions. The resultingMetaTypesmodel encapsulates schema semantics such as complex types, enumerations, references, and groups in a language-agnostic form. -
Optimizing: Optimization is performed by the
Optimizer, which takes theMetaTypesand applies structural transformations. These include deduplication, simplification of unions, merging cardinalities, and resolving typedef aliases. The goal is to prepare the type graph for idiomatic translation into Rust while reducing complexity. -
Generating: The generation step uses the
Generatorto transform the abstract types into Rust-specific type data. It produces theDataTypesmodel by attaching names, Rust derivations, trait support, and rendering metadata. These enriched types form the basis for later rendering while still preserving schema semantics. -
Rendering: Rendering is handled by the
Renderer, which convertsDataTypesinto structured Rust code organized in aModule. It uses theRenderSteptrait to define individual rendering steps. Several built-in steps are available, including support forserdeorquick-xml. Users can also add customRenderStepimplementations to extend or modify the output.
-
Schemas: This model is built by theParserand contains the raw XML schema data, including namespaces, prefixes, and schema file content. It serves as the foundation for interpretation and supports multiple sources and resolver types. -
MetaTypes: Generated by theInterpreter, this model contains language-neutral type definitions. It includes data like complex types, references, enumerations, and groupings derived from schema structure. It is suitable for introspection, transformation, and optimization. -
DataTypes: Produced by theGenerator, this model holds enriched Rust-specific type data. Each type includes metadata for layout, naming, derivations, and other traits required for rendering idiomatic Rust code. This is the core input for the rendering process. -
Module: The final model is produced by theRenderer. It wraps the Rust source code output into a structured format, ready for file output or consumption as token streams. Modules support nested submodules, file splitting, and embedded metadata for customization.
This library provides the following features:
- Rust Code Generation: Convert any XML schema into Rust code.
- Layered Architecture: Add user-defined code to manipulate type information or generated code.
- User-Defined Types: Inject existing types into the generated code to reuse predefined structures.
serdeSupport: Generate code for serialization and deserialization usingserdewithserde_xmlorquick_xmlas serializer/deserializer.quick_xmlSupport: Direct serialization/deserialization support usingquick_xml, avoidingserdelimitations and leveraging asynchronous features.
Below you can find a short list of the most important changes for each released version.
This release introduces a series of architectural improvements, enhanced flexibility in code generation, and broader schema compatibility.
-
Refactored Pipeline Structure The internal code generation pipeline has been refactored to introduce a new
Rendererstep and an accompanyingDataTypemodel. This separation gives users more control over the rendering process, allows better extension points for customization, and prepares the architecture for further growth. -
Refactored Serde Support Support for
serdehas been moved into dedicated renderer steps. This makes it possible to support multiple versions ofserde-based implementations, such asserde-xml-rs0.7 and 0.8, without mixing code. Each renderer step now cleanly encapsulates the logic for one serialization backend. -
Implement Support for Unstructured Data Added support for
xs:anyandxs:anyAttributeby introducing an internal representation for unstructured XML data. This enables working with flexible or unknown schema elements and fixes a long-standing gap in schema coverage. -
Implement Support for
BigIntandBigUintSchemas defining integer types without upper bounds can now be mapped tonum::BigIntornum::BigUint, depending on context. This is useful when working with large numeric values. -
Improved Documentation Support XSD annotations (
xs:documentation) are now parsed and included as Rust doc comments in the generated code, improving type-level visibility and usability. -
Different Bug Fixes and Improvements
- Enum restrictions on text types are now correctly interpreted and rendered
- Complex types in the XML Catalog schema are now rendered correctly
- Introduced per-type
derivesettings for advanced customization - Various naming, escaping, and formatting issues were resolved across the pipeline.
- Generated names of nested elements now uses the name of the parent element as prefix to prevent name collisions.
- Implemented feature to generated boxed
quick_xmldeserializers to reduce stack usage during deserialization - Improved naming of the generated types
- Implemented feature to split generated code into multiple module files
- Improved and implemented advanced examples
- General bug fixes and improvements
- First official release of
xsd-parser
- Schema-Based Validation: Generate validators directly from schemas to validate XML data during reading or writing.
This crate is licensed under the MIT License.