"""Common helper functions to manipulate xml document.""" from collections.abc import Mapping def get_conditional_text_value(data, default=None): """Retrieve conditional text value. Args: data (raw value): String or dictionary value default: Default value if data is not string or dictionary Returns: string, @UserDefinedValue or #text value """ if isinstance(data, str): return data elif isinstance(data, Mapping) and '@UserDefinedValue' in data: return data['@UserDefinedValue'] elif isinstance(data, Mapping) and '#text' in data: return data['#text'] return default def get_root_element(document, default=None): """Retrieve root element in document. Skip the first tag which is the xml namespace Args: document (dict): Document as dict type default: Default value if not a dict type or empty Returns: root element """ if document and isinstance(document, Mapping): return next(iter(document.items()))[1] else: return default