This isn't true.  If an id has an `&` in it, it will appear in the original XML as: `<id>&amp;</id>`.  When you get `.text` from lxml, you will have `"&"`.  When you use that string here, you will make `<id>&</id>`, which is not well-formed XML.  If you were parsing the XML with string operations, you'd get "&amp;" and could then use string operations safely.  Or if you were serializing with lxml, it would escape the characters for you.  But you are parsing with lxml (which decodes escapes), and than serializing with string operations, which don't do the reverse. You need to explicitly escape the XML special characters.  You can use `from xml.sax.saxutils import escape` to do it.