API reference¶
The public entry points. The metaprogramming core (generated classes,
descriptors) is intentionally dynamic; type[SchemaBase] and the
XMLNode XMLNode protocol are the stable seams.
- class pyxsd.PyXSD(xmlFileInput, xsdFile=None, xmlFileOutput=False, transformOutputName=None, transforms=None, classFile=None, verbose=False, quiet=False, mode=BindingPolicy(invalid_value='drop', unresolved_type='error', undeclared_content='error', whitespace='xsd', namespaces='legacy'), namespace_schemas=None)[source]¶
Main class of the program that is in charge of data flow.
Has command line support when it is called as a script.
- executeAndWriteTransforms(rootInstance)[source]¶
Runs each transform in order and writes the transformed tree to the transform output, if one was requested.
- Parameters:
rootInstance (Any)
- Return type:
None
- parseXSD()[source]¶
Reads the given xsd file and creates a set of classes that correspond to the complex and simple type definitions.
- Return type:
None
- parseXML()[source]¶
Reads the given xml file in the context of the xsd file.
Produces instances of the above classes. Does validation. Returns a schema instance object.
- Return type:
- generateCorrectSchemaTags()[source]¶
Generates the proper schema information and namespace information for a tag.
ElementTree leaves the schema information in a form that is not valid XML on its own.
- Return type:
None
- writeXML(rootInstance, output)[source]¶
Sends a pythonic instance tree to the tree writer.
rootInstance: the root instance of a tree. Must be formatted in the program’s tree structure.output: the file object (or path) to write the tree to. Paths are opened and closed here; file objects passed by the caller are flushed but left open.
- getClasses()[source]¶
Returns the dictionary of classes created by ElementRepresentative for each type specified in the schema.
- loadClassFromFile(classFile)[source]¶
Loads a file with overlay classes into the class dictionary.
Overlay classes add to and override the schema type classes to allow for a user to create their own types without changing the schema file itself.
Consider this functionality experimental.
classFile: a string that specifies the location of a user-created overlay class file
- getXmlTree()[source]¶
Sends the xml file into the ElementTree library’s parser.
Allows for the program to get the schemaLocation before parsing the xml against the schema.
- Return type:
- getXmlOutputFileName()[source]¶
Creates a default name for the xml file that is parsed without any transforms. Uses the name from the input xml file.
- Return type:
- getTransformModuleAndLoad(className)[source]¶
Loads a transform class from its class name.
The module it is located in must share the class name, either in camelCase with a lowercase first letter (the historical convention) or in snake_case. The transform is looked up in the installed
pyxsd.transformspackage, then in the directory the program was called from, and then in the directory where the xml file is. If no exact module-name candidate matches, an underscore-insensitive fallback matches the class name against the available modules, so acronym spellings (SendTreeToPyXSD->send_tree_to_pyxsd) still resolve.className: a string of the transform class name being called
- Parameters:
className (str)
- Return type:
- transform(transforms, root)[source]¶
Calls the transforms specified by the user.
Each transform is loaded into memory by
getTransformModuleAndLoad(). The transform class is passed the instance of the root element when it is initialized. The transform object is called with the specified arguments and the new root instance is set to whatever the transform returns, which is usually the root, but it is not required. Any user who uses a transform that does not return the root tree instance should be aware that any transform that uses the root instance will fail to work and raise a fatal error.transforms: a list containing the transform calls in the order that they should be called. Each call looks likeTransformClass(arg1, arg2, key=value).root: the root instance of a tree. Must be formatted in the program’s tree structure.
- getTransformsFileName()[source]¶
Creates a default name for the xml file that is written after all of the transforms. Uses the name from the input xml file.
- Return type:
- getSchemaInfo(nameOrLocation)[source]¶
Extracts information from the schemaLocation tag or the noNamespaceSchemaLocation tag.
Depending on the value of the parameter
nameOrLocation, the function outputs the namespace, the schema location, or the tag type. This function is meant for use with other functions to easily grab bits of data that are used in various locations in the program.nameOrLocation: a one letter string that is either ‘l’, ‘n’, or ‘t’. If the variable is ‘l’, the location of the schema is returned. If it is ‘n’, the namespace is returned, if there is one. ‘t’ returns the tag name to indicate if the xml uses schemaLocation or noNamespaceSchemaLocation.
- getSchemaLocationPairs()[source]¶
Returns every
(namespace, location)hint in the instance.xsi:schemaLocationmay carry multiple namespace/location pairs;xsi:noNamespaceSchemaLocationyields a single pair withNonefor the namespace.
- makeFullName(ns, text)[source]¶
Makes a string that looks similar to some of the names in ElementTree when it contains namespace information.
ns: a string of the namespace used. For this function, this variable is usually set to a url.text: a string of the name of the tag that the full name is being created for.
- class pyxsd.exceptions.PyXSDWarning[source]¶
Warning category for recoverable, non-fatal conditions.
- class pyxsd.nodes.XMLNode(*args, **kwargs)[source]¶
Structural type of one node in a parsed instance tree.
Both the generated schema classes and the synthetic nodes built by
Transform.makeElemObjsatisfy this protocol:_name_: the element’s tag name._attribs_: attribute names mapped to their lexical values._children_: child nodes in document order (a child may beNonewhen an optional element is absent)._value_: the element’s text split on line boundaries, orNonewhen it has none.
The ValidationReport/ValidationIssue classes are documented in
Validation (detailed reference) rather than duplicated here.
Transforms¶
See The Transform class for the Transform/Displayer class
reference, iter_tree, and the built-ins.