gh-81055: Support CDATA sections in xml.etree.ElementTree - #156742
gh-81055: Support CDATA sections in xml.etree.ElementTree#156742serhiy-storchaka wants to merge 2 commits into
Conversation
CDATA is a new factory, like Comment and ProcessingInstruction, which creates a special element serialized as a CDATA section. Its content is character data: it is not escaped, "]]>" in it is split between two sections, and it is returned by itertext() and by the "text" serialization method. TreeBuilder gets the cdata_factory and insert_cdata arguments. When insert_cdata is set, a CDATA section in the input is kept as such instead of being parsed as text, so that the document can be written back unchanged. Expat reports the content of a CDATA section as ordinary character data, but it reports the boundaries, so the builder gets the start_cdata() and end_cdata() methods, and XMLParser calls them like comment() and pi(). _set_factories() takes the third factory, which the C implementation needs for itertext(), and the pyexpat capsule gets SetCdataSectionHandler.
Documentation build overview
|
|
CDATA can certainly be implemented as a tree element. The reason why I chose not to do this in lxml is that I don't think there is a reason to receive it during tree traversal and iteration. PIs and comments are structural elements with a meaning. CDATA, on the contrary, is really just a way of representing text in the serialised XML. It is only relevant for the parser and for the serialiser. There shouldn't be any actual use for it in the in-memory tree representation. IMHO, making it part of the tree nodes would make it get in the way when processing the tree, without any benefit to make up for it. |
|
What is CDATA is intermixed with character data? <elem>before<![CDATA[<spam> & ham]]>after</elem>You need a tree element to represent this. You could do this with an |
I understand that this is a difference between I think resolving CDATA to regular text in the parser is reasonable default behaviour for |
CDATAis a new factory, likeCommentandProcessingInstruction, which creates a special element serialized as a CDATA section. Its content is character data: it is not escaped,]]>in it is split between two sections, and it is returned byitertext()and by the"text"serialization method.TreeBuildersupports the cdata_factory and insert_cdata arguments. When insert_cdata is true, a CDATA section in the input is kept instead of being parsed as text, so a document can be written back unchanged. Expat reports the content of a CDATA section as ordinary character data, but it reports the boundaries, so the builder gets thestart_cdata()andend_cdata()methods, whichXMLParsercalls likecomment()andpi(). The pyexpat capsule getsSetCdataSectionHandlerfor that.Nothing changes by default: without insert_cdata the content of a CDATA section is still added to the tree as ordinary text.
xml.domhas hadCDATASectionnodes andDocument.createCDATASection()since DOM Level 1; this givesxml.etreethe equivalent, andlxmlhasetree.CDATAfor the serialization half.