Hi all, this is my first post to this mailing list and I apologize for it being rather lengthy. First of all congrats for producing a great piece of software. I am currently evaluating the usage of lxml trees as the program data model for messaging middleware interfacing applications. These applications (adapters) usually receive data as messages from some middleware component, perform certain operations on the data and then e.g. put out the transformed data as a file, write it to a database or publish it on the middleware, again. To abstract from the proprietary middleware message formats I plan to transport xml blobs in the messages and use lxml to represent the xml data in the adapter programs. However, 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. I've been playing around a bit with lxml, renaming the _Element class to _ElementBase and defining a new class _Element that inherits from _ElementBase and implements the necessary __setattr__/__getattr__ magic, which works quite nicely. 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
tree.foo Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'etree._ElementTree' object has no attribute 'foo' tree.getroot().party [ http://www.fpml.org/2005/FpML-4-2}party at 401660>, http://www.fpml.org/2005/FpML-4-2}party at 401690>] tree.getroot().party[0] http://www.fpml.org/2005/FpML-4-2}party at 401660> tree.getroot().party[0].partyId http://www.fpml.org/2005/FpML-4-2}partyId at 401630> tree.getroot().party[0].partyId.foo = 187873 tree.getroot().party[0].partyId.foo http://www.fpml.org/2005/FpML-4-2}foo 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 am now wondering how to implement such an API-layer a bit more non-intrusively, without changing the lxml base classes themselves. Given the structure of lxml with the usage of _elementFactory, I'm unsure how to do so. Any hints? Anyone else interested in such an additional API layer? Btw. please ignore the stupid disclaimer my employer adds to my emails..otoh they let me use Python :-) Best regards, Holger Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde, verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte den Inhalt der E-Mail als Hardcopy an. The contents of this e-mail are confidential. If you are not the named addressee or if this transmission has been addressed to you in error, please notify the sender immediately and then delete this e-mail. Any unauthorized copying and transmission is forbidden. E-Mail transmission cannot be guaranteed to be secure. If verification is required, please request a hard copy version.