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::XMLGeneratorRust struct, which uses libxml2 for validation via Rust bindings. The originallibxml2library 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::generatemethod.- Parameters:
- Raises:
RuntimeError – Rust code panics
InvalidPathError – Invalid
filepathXSDValidatorError – XSDValidator returns an error
DataTypeInformationError – Unable to find any information for a custom type
DataTypeNotFoundError – Generator cannot find a matching datatype for an attribute
XSDParserError – The
xsd-parsercrate throws an errorDataTypesFormatError – Data type is in an invalid format
XMLBuilderError – The
xml-buildercrate encounters an errorInvalidXSDVersionError – XSD has an XML other than 1.0 or 1.1
InfiniteRecursionError – Infinite recursive element found
NoElementsError – XSD does not contain any elements
InvalidXSDError – XSD is invalid
NoIndependentElementsError – XSD does not contain any root elements
MultipleXSDRootsError – XSD contains multiple root elements
TypeGenerationError – Generator is unable to generate a specific type
RegexError – Regular expression error (XSD pattern or Rust)
IncompatiblePatternError – Type is constrained to match two incompatible Regex patterns
XSDEncodingError – XSD uses an unsupported encoding
ImplementationError – Unimplemented feature
InvalidXSDNameError – XSD contains an invalid name
LineEndingsError – Regex contains invalid line endings
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::validatemethod- Parameters:
filepath (str) – The path to the XSD file to be validated
- Raises:
RuntimeError – If Rust code panics
InvalidPathError – If
filepathis invalidXSDValidatorError – If XSDValidator returns an error
InvalidXSDError – If recursive elements are found
Example
from pyxmlgenerator import XMLGenerator generator = XMLGenerator() schema = "/path/to/schema.xsd" generator.validate(schema)
The validator uses the
xsdvalidator::XSDValidatorRust 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
xmlgeneratorRust crate- Returns:
The version of the Rust crate
- Return type: