def elem_to_dict(elem, ns, key_map, value_map={}):
    """Convert an XML etree.Element to a desired dictionary
    as specified by the key map and value map.
    Args:
        elem (etree.Element): An ancestor element
            of the tags specified in the key map.
        ns (string): The namespace to use
            when searching for XML tags.
        key_map (dict): A mapping from desired
            dictionary keys to XML tag names.
        value_map (dict): A mapping from XML tag names to
            dictionaries of mappings from XML text
            values to desired dictionary values.
    Returns:
        The desired dictionary.
    """
    to_dict = {}
    for k, v in key_map.iteritems():
        field = elem.find('.//{0}{1}'.format(ns, v))
        if field is not None:
            text = field.text
            to_dict[k] = value_map.get(v, {}).get(text, text)
    return to_dict
