XML defines white space as only #x20, #x9, #xD and #xA (XML 1.0, 2.3), but xml.dom and xml.etree.ElementTree use str.strip(), which also strips other characters, such as U+00A0 or U+000C. Such characters are content, and treating them as white space loses them.
ElementTree.indent() overwrites them:
>>> tree = ET.fromstring("<a>\xa0<b>x</b>\xa0</a>")
>>> ET.indent(tree, space=" ")
>>> ET.tostring(tree, encoding="unicode")
'<a>\n <b>x</b>\n</a>'
canonicalize(strip_text=True) strips them, which changes the canonical form of a document.
In xml.dom, Text.isWhitespaceInElementContent reports such a node as ignorable white space, and the node is removed from the document when parsing with the whitespace-in-element-content feature turned off.
Linked PRs
XML defines white space as only
#x20,#x9,#xDand#xA(XML 1.0, 2.3), butxml.domandxml.etree.ElementTreeusestr.strip(), which also strips other characters, such as U+00A0 or U+000C. Such characters are content, and treating them as white space loses them.ElementTree.indent()overwrites them:canonicalize(strip_text=True)strips them, which changes the canonical form of a document.In
xml.dom,Text.isWhitespaceInElementContentreports such a node as ignorable white space, and the node is removed from the document when parsing with thewhitespace-in-element-contentfeature turned off.Linked PRs