pyxmlgenerator

A Python package for generating XML instances from an input XML Schema (XSD)

This package may be used to read an XML Schema (XSD) file, validate it and use it to generate an XML instance that follows the input schema. The package provides a class XMLGenerator which may be used to validate an XSD or use it to generate a new instance. The package is a Python wrapper for the xmlgenerator Rust crate.

class pyxmlgenerator.XMLGenerator

The main class which manages the XML generator

A single object should be used for all required validations and generations.

Example

from pyxmlgenerator import XMLGenerator

generator = XMLGenerator()

schema = "/path/to/schema.xsd"

# Validates the XSD file and generates an XML instance
xml_string = generator.generate(schema)

print(xml_string)

This class is a wrapper the xmlgenerator::XMLGenerator Rust struct, which uses libxml2 for validation via Rust bindings. The original libxml2 library is written in C and includes a global initialiser/deinitialiser that should not be initialised more than once at a time. The limitation is that only one instance of the class should be created at any one time

generate(filepath, seed=None)

XML instance generator

This method wraps the xmlgenerator::XMLGenerator::generate method.

Parameters:
  • filepath (str) – The path to the XSD file to be validated

  • seed (int | None = None) – Seed the random number generator (default: None)

Raises:

Example

from pyxmlgenerator import XMLGenerator

generator = XMLGenerator()

schema = "/path/to/schema.xsd"

xml_string = generator.generate(schema)

print(xml_string)

Not all features of the XML schema specification are implemented. Unimplemented features will return an error.

validate(filepath)

XSD file validator

This method wraps the xmlgenerator::XMLGenerator::validate method

Parameters:

filepath (str) – The path to the XSD file to be validated

Raises:

Example

from pyxmlgenerator import XMLGenerator

generator = XMLGenerator()

schema = "/path/to/schema.xsd"

generator.validate(schema)

The validator uses the xsdvalidator::XSDValidator Rust struct. This struct wraps the libxml2 C library via Rust bindings. An XSD file is invalid if this library finds it to be invalid. The only additional checks track whether there is an infinite loop in the schema.

exception pyxmlgenerator.InvalidPathError

Invalid filepath or a non-existent file

exception pyxmlgenerator.XSDValidatorError

XSDValidator raised an error

exception pyxmlgenerator.DataTypeInformationError

No data found for custom type

exception pyxmlgenerator.DataTypeNotFoundError

Unable to find a given data type

exception pyxmlgenerator.XSDParserError

Error while parsing the XSD file

exception pyxmlgenerator.DataTypesFormatError

A data type is in an invalid format

exception pyxmlgenerator.XMLBuilderError

Error while building the final XML output

exception pyxmlgenerator.InvalidXSDVersionError

XSD specifies an invalid XML version

exception pyxmlgenerator.InfiniteRecursionError

Infinite recursion encountered

exception pyxmlgenerator.NoElementsError

No elements in XSD

exception pyxmlgenerator.InvalidXSDError

XSD is invalid

exception pyxmlgenerator.NoIndependentElementsError

No root element found in XSD

exception pyxmlgenerator.MultipleXSDRootsError

XSD contains multiple root elements

exception pyxmlgenerator.TypeGenerationError

Error generating a specific type

exception pyxmlgenerator.ImplementationError

Unimplemented feature in XSD

exception pyxmlgenerator.RegexError

Invalid XSD pattern or Regex

exception pyxmlgenerator.IncompatiblePatternError

XSD restricts type to incompatible Regex patterns

exception pyxmlgenerator.XSDEncodingError

XSD uses invalid encoding

exception pyxmlgenerator.InvalidXSDNameError

XSD contains invalid name

exception pyxmlgenerator.LineEndingsError

Invalid line endings in XSD pattern

pyxmlgenerator.version()

Get the version of the xmlgenerator Rust crate

Returns:

The version of the Rust crate

Return type:

str