xsdata.parser module

class xsdata.parser.SchemaParser(context, elements=<factory>)[source]

Bases: object

A simple parser to convert an xsd schema to an easy to handle data structure based on dataclasses.

The parser is a dummy as possible but it will try to normalize certain things like apply parent properties to children.

context: etree.iterparse = None
elements: List[BaseModel] = None
element_form: Optional[xsdata.models.enums.FormType] = None
attribute_form: Optional[xsdata.models.enums.FormType] = None
classmethod create(source)[source]

A shortcut class method to initialize the parser, parse the given source and return the generated Schema instance.

Return type

Schema

classmethod from_file(path)[source]

A shortcut class method for file path sources.

Return type

Schema

classmethod from_bytes(source)[source]

A shortcut class method for bytes source.

Return type

Schema

classmethod from_string(source)[source]

A shortcut class method for string source.

Return type

Schema

parse()[source]

Main parse procedure which depends heavily on binding data classes that have all the necessary attributes to match all the possible xsd elements and attributes.

Elements are initialized on the start event of the parser and assigned to the parent element on the end event. The procedure all includes start/end hooks for each element type.

Elements with no attributes and text are ignored.

Return type

Schema

start_schema(schema, *args)[source]

Collect the schema’s default form for attributes and elements for later usage.

start_element(element, *args)[source]

Assign the schema’s default form for elements if the given element form is None.

start_attribute(attribute, *args)[source]

Assign the schema’s default form for attributes if the given attribute form is None.

end_choice(choice, *args)[source]

Elements inside a choice are by definition optional, reset their min occurs counter.

assign_to_parent(element)[source]

Assign an element to its parent either in a list of same type objects or directly as an attribute.

:raise ValueError when we can’t assign or append the element to the

correct place. This practically will mean that we encountered a new not implemented element.