How to escape a string so that it can be used in an XML attribute?
I am not setting the attribute programmatically from python code — I just need to create a valid string which can be used as an XML attribute.
I have tried:
from xml.sax.saxutils import escape, quoteattr
print (escape('<&% "eggs and spam" &>'))
# >>> <&% "eggs and spam" &>
print (quoteattr('<&% "eggs and spam" &>'))
# >>> '<&% "eggs and spam" &>'
The problem is that both escape() and quoteattr() are not escaping the double quote character, i.e. ".
Of course, I can do a .replace('"', '"') on the escaped string, but I am assuming there should be a way to do it with the existing API (from the standard library or with third-party modules such as lxml).
Update: I've found that Python3's html.escape produces the expected result but I am reluctant to use it in an XML context since I'm assuming that HTML escaping might follow a different spec than what is required by the XML standard (https://www.w3.org/TR/xml/#AVNormalize).
<root>), add an attribute through the API, serialize the document to string, and finally remove the'<root attr="'and'" />'from the result. It would probably be 4 lines of code overall.