

def add_namespaces(root, ns_keys):
    if isinstance(ns_keys, six.string_types):
        ns_keys = [ns_keys]
    namespaces = Namespaces()
    ns_keys = [(x, namespaces.get_namespace(x)) for x in ns_keys]
    if (etree.__name__ != 'lxml.etree'):
        existing_namespaces = set()
        for elem in root.getiterator():
            if (elem.tag[0] == '{'):
                (uri, tag) = elem.tag[1:].split('}')
                existing_namespaces.add(namespaces.get_namespace_from_url(uri))
        for (key, link) in ns_keys:
            if ((link is not None) and (key not in existing_namespaces)):
                root.set(('xmlns:%s' % key), link)
        return root
    else:
        new_map = root.nsmap
        for (key, link) in ns_keys:
            if (link is not None):
                new_map[key] = link
        new_root = etree.Element(root.tag, nsmap=new_map)
        for (a, v) in list(root.items()):
            new_root.set(a, v)
        for child in root:
            new_root.append(deepcopy(child))
        return new_root

dataset/ETHPy150Open geopython/OWSLib/owslib/util.py add_namespaces/original