xsdata.formats.dataclass.context module

class xsdata.formats.dataclass.context.XmlContext(element_name=<function XmlContext.<lambda>>, attribute_name=<function XmlContext.<lambda>>, cache=<factory>, xsi_cache=<factory>)[source]

Bases: object

Generate and cache the necessary metadata to bind an xml document data to a dataclass model.

Parameters
  • element_name (Callable) – Default callable to convert field names to element tags

  • attribute_name (Callable) – Default callable to convert field names to attribute tags

  • cache (Dict[Type, XmlMeta]) – Cache models metadata

  • xsi_cache (Dict[str, List[Type]]) – Index models by xsi:type

element_name()
attribute_name()
cache: Dict[Type, xsdata.formats.dataclass.models.elements.XmlMeta]
xsi_cache: Dict[str, List[Type]]
sys_modules: int = 0
fetch(clazz, parent_ns=None, xsi_type=None)[source]

Fetch the model metadata of the given dataclass type, namespace and xsi type.

Parameters
  • clazz (Type) – A dataclass model

  • parent_ns (Optional[str]) – The parent dataclass namespace if present.

  • xsi_type (Optional[str]) – if present it means that the given clazz is derived and the lookup procedure needs to check and match a dataclass model to the qualified name instead.

Return type

XmlMeta

build_xsi_cache()[source]

Index all imported dataclasses by their xsi:type qualified name.

find_types(qname)[source]

Find all classes that match the given xsi:type qname.

  • Ignores native schema types, xs:string, xs:float, xs:int, …

  • Rebuild cache if new modules were imported since last run

Parameters

qname (str) –

Return type

Optional[List[Type]]

find_type(qname)[source]

Return the most recently imported class that matches the given xsi:type qname.

Parameters

qname (str) –

Return type

Optional[Type]

find_subclass(clazz, qname)[source]

Compare all classes that match the given xsi:type qname and return the first one that is either a subclass or shares the same parent class as the original class.

Parameters
Return type

Optional[Type]

build(clazz, parent_ns=None)[source]

Fetch from cache or build the metadata object for the given class and parent namespace.

Parameters
Return type

XmlMeta

get_type_hints(clazz, parent_ns)[source]

Build the model class fields metadata.

Parameters
Return type

Iterator[XmlVar]

build_choices(clazz, parent_name, parent_namespace, choices)[source]
Parameters
classmethod resolve_namespaces(xml_type, namespace, parent_namespace)[source]

Resolve the namespace(s) for the given xml type and the parent namespace.

Only elements and wildcards are allowed to inherit the parent namespace if the given namespace is empty.

In case of wildcard try to decode the ##any, ##other, ##local, ##target.

Parameters
Return type

List[str]

classmethod default_namespace(namespaces)[source]

Return the first valid namespace uri or None.

Parameters

namespaces (List[str]) –

Return type

Optional[str]

classmethod default_value(var)[source]

Return the default value/factory for the given field.

Parameters

var (Field) –

Return type

Any

classmethod real_types(type_hint)[source]

Return a list of real types that can be used to bind or cast data.

Parameters

type_hint (Any) –

Return type

List

classmethod is_derived(obj, clazz)[source]

Return whether the given obj is derived from the given dataclass type.

Parameters
Return type

bool

classmethod is_element_list(type_hint, is_tokens)[source]
Parameters
  • type_hint (Any) –

  • is_tokens (bool) –

Return type

bool

classmethod default_xml_type(clazz)[source]

Return the default xml type for the fields of the given dataclass with an undefined type.

Parameters

clazz (Type) –

Return type

str

classmethod get_subclasses(clazz)[source]
Parameters

clazz (Type) –

local_name(name, xml_type=None)[source]
Parameters
Return type

str