diff --git a/src/spinneret/utilities.py b/src/spinneret/utilities.py index e9af109..0c05b4d 100644 --- a/src/spinneret/utilities.py +++ b/src/spinneret/utilities.py @@ -2,6 +2,7 @@ from os import environ from json import load +from lxml import etree def load_configuration(config_file: str) -> None: @@ -18,3 +19,14 @@ def load_configuration(config_file: str) -> None: config = load(config) for key, value in config.items(): environ[key] = value + + +def delete_empty_tags(xml: etree._ElementTree) -> etree._ElementTree: + """Deletes empty tags from an XML file + + :param xml: The XML file to be cleaned. + :returns: The cleaned XML file. + """ + for element in xml.xpath(".//*[not(node())]"): + element.getparent().remove(element) + return xml diff --git a/tests/test_utilities.py b/tests/test_utilities.py new file mode 100644 index 0000000..c1da686 --- /dev/null +++ b/tests/test_utilities.py @@ -0,0 +1,25 @@ +"""Test utilities module""" + +from lxml import etree +from spinneret import datasets +from spinneret.utilities import delete_empty_tags + + +def test_delete_empty_tags(): + """Test that empty tags are removed from an XML file""" + + # Read test file + eml_file = datasets.get_example_eml_dir() + "/" + "edi.3.9.xml" + eml = etree.parse(eml_file) + + # Add an empty tag to the XML and check that it is added + # to the XML + empty_tag = etree.Element("empty_tag") + eml.getroot().append(empty_tag) + assert len(eml.xpath(".//empty_tag")) == 1 + + # Remove empty tags + eml = delete_empty_tags(eml) + + # Check that the empty tag has been removed + assert len(eml.xpath(".//empty_tag")) == 0