cdm_data_loaders.coercion.xml ¶
Convert lxml XML elements into typed Python dictionaries according to an XSD schema.
make_xml_element_converter ¶
make_xml_element_converter(
schema: XMLSchema,
xml_tag: str,
*,
max_recursion_depth: int = DEFAULT_MAX_RECURSION_DEPTH
) -> Callable[[Element], dict[str, Any]]
Build a reusable element converter for xml_tag that resolves the XSD target once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
XMLSchema
|
Loaded XMLSchema instance. |
required |
xml_tag
|
str
|
Target XML element name (local name or Clark-notation |
required |
max_recursion_depth
|
int
|
Maximum structural recursion depth before JSON serialization. |
DEFAULT_MAX_RECURSION_DEPTH
|
Returns:
| Type | Description |
|---|---|
Callable[[Element], dict[str, Any]]
|
Function converting an lxml Element into a sparse dictionary. |
Source code in src/cdm_data_loaders/coercion/xml.py
make_xml_level_converters ¶
make_xml_level_converters(
schema: XMLSchema,
level: int,
*,
root_tag: str | None = None,
max_recursion_depth: int = DEFAULT_MAX_RECURSION_DEPTH
) -> dict[str, Callable[[Element], dict[str, Any]]]
Build reusable element converters for all XML element types at depth level in an XSD schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
XMLSchema
|
Loaded XMLSchema instance. |
required |
level
|
int
|
Non-negative depth relative to the root element(s). |
required |
root_tag
|
str | None
|
Optional explicit root element name to anchor the traversal. |
None
|
max_recursion_depth
|
int
|
Maximum structural recursion depth before JSON serialization. |
DEFAULT_MAX_RECURSION_DEPTH
|
Returns:
| Type | Description |
|---|---|
dict[str, Callable[[Element], dict[str, Any]]]
|
Mapping of element local tag name to its element-to-dict converter function. |
Source code in src/cdm_data_loaders/coercion/xml.py
convert_xml_element ¶
convert_xml_element(
element: Element,
schema: XMLSchema,
xml_tag: str,
*,
max_recursion_depth: int = DEFAULT_MAX_RECURSION_DEPTH
) -> dict[str, Any]
Convert an lxml Element matching xml_tag into a typed dictionary according to the XSD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
XML element to convert. |
required |
schema
|
XMLSchema
|
Loaded XMLSchema instance. |
required |
xml_tag
|
str
|
Target XML element name (local name or Clark-notation |
required |
max_recursion_depth
|
int
|
Maximum structural recursion depth before JSON serialization. |
DEFAULT_MAX_RECURSION_DEPTH
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Sparse dictionary of coerced field values matching the generated PyArrow schema. |