import re


def get_ns_dict(xml):
    """Take an xml string and return a dict of namespace prefixes to
    namespaces mapping."""
    
    nss = {} 
    def_cnt = 0
    matches = re.findall(r'\s+xmlns:?(\w*?)\s*=\s*[\'"](.*?)[\'"]', xml)
    for match in matches:
        prefix = match[0]; ns = match[1]
        if prefix == '':
            def_cnt += 1
            prefix = '_' * def_cnt
        nss[prefix] = ns
    return nss