Sanitize any user-submitted data to ensure that it can be used in XML

def _clean_for_xml(data):
    """ Sanitize any user-submitted data to ensure that it can be used in XML """

    # If data is None or an empty string, don't bother
    if data:
        # This turns a string like "Hello…" into "Hello&#8230;"
        data = data.encode("ascii", "xmlcharrefreplace").decode("ascii")
        # However it's still possible that there are invalid characters in the string,
        # so simply remove any of those characters
        return _illegal_xml_chars_re.sub("", data)
    return data