def clean_xml_attribute(element, attribute, default=None):
    """
    Get an XML attribute value and ensure it is legal in XML
    :param element:
    :param attribute:
    :param default:
    :return:
    """

    value = element.attrib.get(attribute, default)
    if value:
        value = value.encode("utf-8", errors="replace").decode("utf-8", errors="backslashreplace")
        value = value.replace(u"\ufffd", "?")  # strip out the unicode replacement char

    return value