Skip to content

Draft: Add incremental serialisation foundation

Daniel Hillier requested to merge topic/default/nsmap into branch/default

Incremental serialisation foundation

Adds the foundation for incremental serialization. The et_xmlfile.elementtreemod file contains write_elem_start which can write the opening tag (and associated attrs, namespace declerations etc.) and indicate whether the element needs closing to the calling function. et_xmlfile.elementtreemod._serialize_ns_xml is an example of how this can be used to serialize a document.

The code is derived from cPython's ElementTree code (thus the additions to the Licence file) and incorporates a few bug fixes and feature enhancements around namespaces. They are:

Bug fixes:

  • conflicts of registered namespaces and default_namespace uri
  • incorrect handling of attributes with a URI that is the same as thedefault_uri (attributes in that namespace must have a prefix - unprefixed attrs are not in the default namespace)

Feature:

  • Add local namespace map arg, nsmap to write()`

I've created some PRs for cPython but they haven't been reviewed yet.

In particular, the local namespace change is important as this allows serialisation of elements where all the namespaces are not known before serialization.

This merge request also includes some hacky tests which leverage cPython's Lib/test/test_xml_etree.py to test the validity / compatibility of these changes. Where the behaviour intentionally differs from cPython's behaviour, those tests are modified if possible or skipped if they're no longer relevant. They're currently included to run in the tox setup but may not be compatible with pypy.

Also happy to change that testing approach if you would prefer.


Elements with nsmap from the source document

This work started as a personal project to have access to the namespace mappings of the original document from the Element object, similar to how lxml works. This allows you access to the effective prefix to uri mapping at any element in the parsed document. It also allows serialization to utilise the namespace mappings found on the Element.

This functionality is included in et_xmlfile.nsetree along with tests (and similar hacky tests leveraging cPython's tests). This code may be tangential to the goals of this project so I'm happy to strip that out if you would like. It's separated pretty cleanly with only a couple of hooks in elementtreemod to allow the nsetree subclasses.

It's quite a big merge request so feel free to send through any questions / suggestions to make life easier for you.

Edited by Daniel Hillier

Merge request reports