def find_nodes_by_attribute()

in o2a/utils/xml_utils.py [0:0]


def find_nodes_by_attribute(root, attr, val, tag=None) -> List[ET.Element]:
    """
    Finds node with the attribute `attr` matching `val`. An optional tag can be
    specified which will narrow down the search space to only the tag passed in.

    :param root: Node's direct descendants to look under.
    :param attr: Attribute to match with `val`
    :param val: Required value of attribute
    :param tag: Optional, can decrease the search space even more.
    :return: List of matching XML nodes.
    """
    matching_nodes: List[ET.Element] = []
    search_space = find_nodes_by_tag(root, tag) if tag else root

    for node in search_space:
        if attr in node.attrib and node.attrib[attr] == val:
            matching_nodes.append(node)
    return matching_nodes