
Hi Holger,
Holger Joukl wrote:
I'd like to add an (arguably :-) "even-more-pythonic" API layer on top of lxml, enabling the dot (.) operator syntax to navigate through the tree, similar to amara or gnosis.xml.objectify, plus the possibility to assign simple Python builtin types transparently.
For my purposes, element.text is regarded as the element data, and ns-unqualified subelement access is allowed by simply using the parent ns-prefix, if no qualified name was given, i.e. getattr(elt, 'foo') ---> returns children of elt with tagname {<ns-qualification of elt>}foo getattr(elt, '{myURI}foo') --> returns children of elt with tagname {myURI}foo
E.g.:
tree
<etree._ElementTree object at 0x403170>
tree.foo
Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'etree._ElementTree' object has no attribute 'foo'
tree.getroot().party
[<Element {http://www.fpml.org/2005/FpML-4-2%7Dparty at 401660>, <Element {http://www.fpml.org/2005/FpML-4-2%7Dparty at 401690>]
tree.getroot().party[0]
<Element {http://www.fpml.org/2005/FpML-4-2%7Dparty at 401660>
tree.getroot().party[0].partyId
<Element {http://www.fpml.org/2005/FpML-4-2%7DpartyId at 401630>
tree.getroot().party[0].partyId.foo = 187873 tree.getroot().party[0].partyId.foo
<Element {http://www.fpml.org/2005/FpML-4-2%7Dfoo at 401690>
tree.getroot().party[0].partyId.foo()
187873
etree.tostring(tree.getroot().party[0])
'<party id="PartyA">\n\t\t<partyId>Party A<foo>187873</foo></partyId>\n\t</party>\n\t'
I think that's an interesting API to have, especially since a lot of Python XML libraries support this. I could imagine having a package "lxml.elementlib" as a collection of generic Element classes that implement certain extended APIs.
Before I start writing something like this myself, could you contribute your implementation for this purpose? It shouldn't be very complex anyway. If you do, please provide it in pure Python. And, if you want be be really helpful, you could come up with some test cases similar to what you find in src/lxml/tests/test_*.py or even some doctests to proof that it works as expected.
Stefan