def fix_xml_ampersands(xml_str):
 return re.sub(u'&(?!amp;|lt;|gt;|apos;|quot;|#x[0-9a-fA-F]{,4};|#[0-9]{,4};)', u'&amp;', xml_str)

def fix_xml_ampersands ( xml_str ) : return re . sub ( u'&(?!amp;|lt;|gt;|apos;|quot;|#x[0-9a-fA-F]{,4};|#[0-9]{,4};)' , u'&amp;' , xml_str )
replace all the & by &amp; in xml .