select_first_of_type

returns elements that are the first of the type.

Source


def select_first_of_type(self):
    parents = list()
    tag = self[0].name()
    for node in self:
        duplicated = False
        for p in parents:
            if p == node.parent():
                duplicated = True
                break
        if not duplicated: parents.append(node.parent())
    ret = pyNodeList()
    for parent in parents:
        n = parent.child(tag)
        if isinstance(n, pyNodeList) and len(n) != 0: n = n.first()
        if n is not None: ret.append(n)
    if len(ret) == 1: return ret[0]
    else: return ret