Paul Everitt wrote:
Stefan Behnel wrote:
Anyway, I don't know if it's a good idea to use XSLT here, since there may be too many 'ifs' and 'maybes'.
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?
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.
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.
XSLT supports arguments in functions, just like XPath does.
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. :^)
But using element classes, you could do something like this (untested):
class MyDataFiller(ElementBase): def _init(self): child = self if child.tag == 'sqlquery': query = child.text del self # 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 # will call _init() for data_child in data:
# do something with data
In the example above, is the _init called when the document is parsed, or when the element is traversed?
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.