Skip to content

xml: characters which are not white space in XML are treated as white space #156658

Description

@serhiy-storchaka

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions