XMLGenerator
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:
- file_to_string - For reading text files
- xsdvalidator - For validating input XSD
- regextranslator - For translating from XSD pattern style regular expressions to Rust style
- xsdtestdata - Test data manager for the xsdtests database
Additionally, the project also includes a Python wrapper for the xsdtestdata crate - pyxsdtestdata.
This allows the test data to be accessed directly from Python.
Dependencies
System requirements:
- Cargo - Installed via Rustup
- libxml2-rs - Rust bindings for the libxml2 C library
- Python - Required for Python bindings
pyxmlgenerator - uv - Recommended for installation of Python packages (Optional)
Build
To 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
Usage
The library can be used either from Python or Rust. Here are examples of each:
Python
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.
Rust
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.
Limitations
Not all features of the XSD specification have been implemented, if these features are encountered, an unimplemented error is thrown.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyxmlgenerator-0.2.3-cp310-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pyxmlgenerator-0.2.3-cp310-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 2.4 MB
- Tags: CPython 3.10+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e7137b592e1131f341e00239513c8975c3e44bbabef692a1fc00302d221a08f
|
|
| MD5 |
aa6efbae8317f4fae9c7ef3c3bfc3cae
|
|
| BLAKE2b-256 |
9b90e2ea4518ce32bdd5b73946edae01ba6f2fd2128f1281877990134dc80745
|