Re: [lxml-dev] Re: SimpleXMLWriter vs. lxml performance

Paul Everitt wrote:
Only if they are available in libxslt by default, but I'm not sure. Anyway, I don't know if it's a good idea to use XSLT here, since there may be too many 'ifs' and 'maybes'. But using element classes, you could do something like this (untested): --------------------------- class MyDataFiller(ElementBase): def _init(self): child = self[0] if child.tag == 'sqlquery': query = child.text del self[0] # remove sqlquery element to prevent running this twice # run SQL query, generate result child nodes for it Namespace('ns')['mydata'] = MyDataFiller xml = XML(""" <myroot xmlns='ns'> <mydata> <sqlquery>SELECT from ...</sqlquery> </mydata> </myroot> """) data = xml[0] # will call _init() for data_child in data: # do something with data --------------------------- I said there was no way to have a constructor, but there actually is an _init() method that can be overridden and that will be called after instance initialization (which is a bit after instance creation, so this is different from __init__). But you have to assure that it is either harmless to call it multiple times or that it does something that is reflected in the underlying XML to assure it is only executed once (like I do above). Stefan

Stefan Behnel wrote:
Ahh, too bad. I was hoping to avoid an approach that only worked with lxml. But, if that's the way it is, that's the way it is. Out of curiosity, is it not a good idea based on the current state, or the long-term plans as well? Essentially, I'm looking for a way to bring new nodes into a document. Similar to how XInclude does, but under programmatic control, and with the ability to do arguments. I'd prefer to keep the integration point in a declarative document-oriented style, instead of a script-oriented style. But it sounds like I might not be able to get there from here and I have to take what I can get. :^)
In the example above, is the _init called when the document is parsed, or when the element is traversed? Stated differently, if I want to evaluate the query and get the new nodes, do I have to write some script to grab each node and "evaluate" it? --Paul

Paul Everitt wrote:
As I said, if you want to test it, feel free. That way, we know what works and what may have to be fixed. It may enable us to decide if it's worth implementing.
XSLT supports arguments in functions, just like XPath does.
I updated the documentation regarding this point, but for a short answer:
Stated differently, if I want to evaluate the query and get the new nodes, do I have to write some script to grab each node and "evaluate" it?
Yes. This is only done when a Python object is instantiated. Which means that you may have to do something like this: tree.xpath('//XPath expression to find all complex elements') to instantiate them first. Stefan

Stefan Behnel wrote:
Ahh, too bad. I was hoping to avoid an approach that only worked with lxml. But, if that's the way it is, that's the way it is. Out of curiosity, is it not a good idea based on the current state, or the long-term plans as well? Essentially, I'm looking for a way to bring new nodes into a document. Similar to how XInclude does, but under programmatic control, and with the ability to do arguments. I'd prefer to keep the integration point in a declarative document-oriented style, instead of a script-oriented style. But it sounds like I might not be able to get there from here and I have to take what I can get. :^)
In the example above, is the _init called when the document is parsed, or when the element is traversed? Stated differently, if I want to evaluate the query and get the new nodes, do I have to write some script to grab each node and "evaluate" it? --Paul

Paul Everitt wrote:
As I said, if you want to test it, feel free. That way, we know what works and what may have to be fixed. It may enable us to decide if it's worth implementing.
XSLT supports arguments in functions, just like XPath does.
I updated the documentation regarding this point, but for a short answer:
Stated differently, if I want to evaluate the query and get the new nodes, do I have to write some script to grab each node and "evaluate" it?
Yes. This is only done when a Python object is instantiated. Which means that you may have to do something like this: tree.xpath('//XPath expression to find all complex elements') to instantiate them first. Stefan
participants (2)
-
Paul Everitt
-
Stefan Behnel