def _get_leaf_nodes_up_to_level(root: ET.Element, level: int) -> List[ET.Element]:
    """
    Get collection of nodes up to certain level including leaf nodes.

    Args:
        root (ET.Element): XML Root Element
        level (int): Levels to traverse in the tree

    Returns:
        List[ET.Element]: List of target nodes

    """

    def traverse(current_node, current_level):
        if len(current_node) == 0 or level == current_level:
            # Keep leaf nodes and target level nodes
            nodes.append(current_node)
        elif current_level < level:
            # Move to the next level
            for child in current_node:
                traverse(child, current_level + 1)

    nodes = []
    traverse(root, 0)
    return nodes

    Get collection of nodes up to certain level including leaf nodes.

    Args:
        root (ET.Element): XML Root Element
        level (int): Levels to traverse in the tree

    Returns:
        List[ET.Element]: List of target nodes

    
llama-index-integrations/readers/llama-index-readers-file/llama_index/readers/file/xml/base.py