XmlContext

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

The service provider for binding operations metadata.

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

Variables
  • cache – Cache models metadata

  • xsi_cache – Index models by xsi:type

  • sys_modules – Number of imported modules

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) – The requested dataclass type

  • parent_ns (Optional[str]) – The inherited parent namespace

  • 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) – Qualified name

Return type

List[Type[~T]]

find_type(qname)[source]

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

Parameters

qname (str) – Qualified name

Return type

Optional[Type[~T]]

find_type_by_fields(field_names)[source]

Find a dataclass from all the imported modules that matches the given list of field names.

Parameters

field_names (Set) – A unique list of field names

Return type

Optional[Type[~T]]

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
  • clazz (Type) – The search dataclass type

  • qname (str) – Qualified name

Return type

Optional[Type]

build(clazz, parent_ns=None)[source]

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

Parameters
  • clazz (Type) – A dataclass type

  • parent_ns (Optional[str]) – The inherited parent namespace

Return type

XmlMeta

get_type_hints(clazz, parent_ns)[source]

Build the model fields binding metadata.

Parameters
  • clazz (Type) – The requested dataclass type

  • parent_ns (Optional[str]) – The inherited parent namespace

Return type

Iterator[XmlVar]

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
  • xml_type (Optional[str]) – The xml type (Text|Element(s)|Attribute(s)|Wildcard)

  • namespace (Optional[str]) – The field namespace

  • parent_namespace (Optional[str]) – The parent namespace

Return type

List[str]

classmethod default_namespace(namespaces)[source]

Return the first valid namespace uri or None.

Parameters

namespaces (List[str]) – A list of namespace options which may include valid uri(s) or one of the ##any, ##other, ##targetNamespace, ##local

Return type

Optional[str]

classmethod default_value(var)[source]

Return the default value/factory for the given dataclass 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) – A typing declaration

Return type

List

classmethod is_derived(obj, clazz)[source]

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

Parameters
  • obj (Any) – A dataclass instance

  • clazz (Type) – A dataclass type

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) – A dataclass type

Return type

str