Module ploigos_step_runner.utils.xml

Shared utils for dealing with XML.

Functions

def get_xml_element(xml_file, element_name)

Gets a given element from a given xml file.

Raises

ValueError
If the given xml_file does not exist. If the given xml_file does not contain an element with the given element_name.

Returns

xml.etree.ElementTree.Element
The Element matching the given element_name.
def get_xml_element_by_path(xml_file_path, xpath, default_namespace=None, xml_namespace_dict=None, find_all=False)

Gets the XML element(s) from a given xml file given an xpath.

Parameters

xml_file_path : str
Path of the xml file
xpath : str
Xpath of the element you want
default_namespace : str
Optional string specifying the default namespace you are using in your xpath selector. This is the most common argument that will most likely be used. If your XML is namespaced, then even if your elements are in the default namespace, you must specify and use this namespace in both your xpath as well as specifying it here.
xml_namespace_dict : Dict[str, str]
Optional dictionary if default_namespace is not enough and you have multiple namespaces that you need to deal with in your xpath selector.
find_all : bool
If False find only the first matching Element. If True find all matching elements.

Returns

xml.etree.ElementTree.Element or [xml.etree.ElementTree.Element]
The Element(s) found given the xpath
def get_xml_element_if_present(xml_file, element_name)

Gets a given element from a given xml file, if the xml file has that element. Otherwise returns None.

Raises

ValueError
If the given xml_file does not exist.

Returns

xml.etree.ElementTree.Element
The Element matching the given element_name. Or None if the file does not contain an Element with element_name.
def get_xml_element_text_by_path(xml_file_path, xpath, default_namespace=None, xml_namespace_dict=None, find_all=False)

Gets the text of XML element(s) from a given xml file given an xpath.

Parameters

xml_file_path : str
Path of the xml file
xpath : str
Xpath of the element you want
default_namespace : str
Optional string specifying the default namespace you are using in your xpath selector. This is the most common argument that will most likely be used. If your XML is namespaced, then even if your elements are in the default namespace, you must specify and use this namespace in both your xpath as well as specifying it here.
xml_namespace_dict : Dict[str, str]
Optional dictionary if default_namespace is not enough and you have multiple namespaces that you need to deal with in your xpath selector.
find_all : bool
If False find only the first matching Element. If True find all matching elements.

Returns

str or [str]
The text of the XML elements found given the xpath