Пример #1
0
def add_iso_md_element(xml_obj: _Element, new_link: str):
    """ Adds a new MetadataURL element to the parent xml_obj

    Args:
        xml_obj (_Element): The parent xml object which holds all MetadataURL elements
        new_link (str): The link of the new metadata resource
    Returns:
        nothing
    """
    iso_elem = etree.Element("MetadataURL", {"type": "ISO19115:2003"},
                             nsmap={"xlink": "http://www.w3.org/1999/xlink"})
    iso_elem_format = etree.SubElement(iso_elem, "Format")
    iso_elem_format.text = "text/xml"
    iso_elem_resource = etree.SubElement(
        iso_elem, "OnlineResource", {
            "{http://www.w3.org/1999/xlink}type": "simple",
            "{http://www.w3.org/1999/xlink}href": new_link
        })

    # try to append where other metadaURL elements might already exist
    # if there are no other elements -> just append it at the end
    other_iso_md_elements = try_get_element_from_xml("./MetadataURL", xml_obj)
    if len(other_iso_md_elements):
        index = 0
        for other_iso in other_iso_md_elements:
            i = xml_obj.index(other_iso)
            if i > index:
                index = i
        index += 1
        xml_obj.insert(index, iso_elem)
    else:
        xml_obj.append(iso_elem)
Пример #2
0
def create_subelement(xml_elem: _Element, tag_name, after: str = None, attrib: dict = None, nsmap: dict = {}):
    """ Creates a new xml element as a child of xml_elem with the name tag_name

    Args:
        xml_elem: The xml element
        tag_name: The tag name for the new element
        after (str): The tag name of the element after which the new one should be inserted
        attrib: The attribute dict for the new element
    Returns:
         A new subelement of xml_elem
    """
    ret_element = etree.Element(tag_name, attrib=attrib, nsmap=nsmap)
    if after is not None:
        after_element = try_get_single_element_from_xml("./{}".format(after), xml_elem)
        after_element_index = xml_elem.index(after_element) + 1
        xml_elem.insert(after_element_index, ret_element)
    else:
        xml_elem.append(ret_element)
    return ret_element
Пример #3
0
def add_subelement(parent_elem: _Element, sub_element: _Element, after: str = None):
    """ Adds an existing xml element after

    Args:
        parent_elem: The parent xml element
        sub_element: The sub xml element
    Returns:
         parent_elem: The modified xml element, holding the subelement as a child
    """
    if after is not None:
        after_element = try_get_single_element_from_xml("./{}".format(after), parent_elem)
        if after_element is None:
            # If this element could not be found, we append this element at the end
            after_element_index = -1
        else:
            after_element_index = parent_elem.index(after_element) + 1
        parent_elem.insert(after_element_index, sub_element)
    else:
        parent_elem.append(sub_element)
    return parent_elem
Пример #4
0
def _get_child_element_index(parent_ele: _Element, child_ele: _Element) -> int:
    return parent_ele.index(child_ele)