This project provides a Python package pyxmlgenerator,
which generates XML instances matching the provided XML Schema (XSD) input.
The package is written in Rust with PyO3 Python bindings allowing direct use from Python.
The pyxmlgenerator package is a wrapper for the xmlgenerator Rust crate,
which generates the XML from a given schema.
The project includes several additional Cargo crates to achieve this goal:
Additionally, the project also includes a Python wrapper for the xsdtestdata crate - pyxsdtestdata.
This allows the test data to be accessed directly from Python.
System requirements:
pyxmlgeneratorTo build the Rust xmlgenerator library, simply run:
cargo build
To run the Rust test suite, use:
cargo test
To build the Python wrapper pyxmlgenerator, run:
uv sync --package pyxmlgenerator
To run the Python test suite, use:
uv run pytest
The library can be used either from Python or Rust. Here are examples of each:
The following example illustrates a similar workflow in Python, using the built-in argparse and pathlib libraries to read and validate command-line input.
import pathlib
import argparse
import pyxmlgenerator
from xmlschema importXMLSchema
parser = argparse.ArgumentParser()
parser.add_argument("filepaths", type=pathlib.Path, nargs='+')
args = parser.parse_args()
paths = args.filepaths
xml_generator = pyxmlgeneratorXMLGenerator()
for path in paths:
path_str = str(path)
# Validate file
try:
xml_generator.validate(path_str)
except pyxmlgenerator.XSDValidatorError:
print(f"Invalid file: {path}")
exit(0)
print(f"Valid file: {path}")
try:
output = xml_generator.generate()
except:
print("Error running generator")
exit(0)
print("Output generated!")
# Generate sc
schema =XMLSchema(path_str)
if schema.is_valid(output):
print("Output is valid")
else:
print("Output does not match input schema")
This example uses Python's xmlschema library to validate output XML strings.
The following is an example of a simplified program which reads a list of filepaths from command-line arguments and generates example XML output strings for each valid XSD.
use std::env;
use std::path::PathBuf;
use std::str::FromStr;
use xmlgenerator:XMLGenerator;
fn main() {
let generator =XMLGenerator::new();
let args: Vec<String> = env::args().collect();
if args.is_empty() {
println!("No files provided!");
println!("Usage: ./example [files] ...");
return;
}
println!("Reading {} input files...", args.len());
for arg in args {
let path = PathBuf::from_str(arg.as_str()).unwrap();
// Validate file
if let Err(error) = generator.validate(&path) {
println!("Invalid file: {}", path);
return;
}
println!("Valid input file: {}", path);
let output = match generator.generate(&path, None) {
Ok(out) => out,
Err(e) => {
eprintln!("Error running generator: {}", path);
eprintln!("Error: {}", e);
return;
}
};
println!("Output generated!");
println!("Output:");
println!(output);
}
}
NOTE: Only a single XMLGenerator` should be created. Multiple instances of this class cause undefined behaviour.
Not all features of the XSD specification have been implemented, if these features are encountered, an unimplemented error is thrown.