[lxml-dev] Some thoughts re XPath extension functions
OK, I have a first (barely) functional implementation of registerXPathExtensionFunc. (I sent it to Martijn, but it's not ready for checkin until some collective thinking happens.) For one thing, I currently collect functions globally, and register them with the new XPathContext that is created on demand every time the xpath function is called. That operation is unfortunately slow. I think that there are reasons that XPathContext are created on demand: If your extension function calls the xpath method, I think it is necessary to use a new context. (Needs checking.) But then that means re-registering extensions, of course... I first thought of keeping a XPathContext with the xmlDoc. It would save creation time, for one thing. But it means that I have to guard against the recursion problems above. (Yes, I have done such things, it is a real situation.) Also, more care to guard against leaks. Do people here think it worth the trouble? And I also thought that the extension functions should be registered with the document, and not globally. Do people agree this is a good thing? More complicated in some ways, but it would allow different documents to have different extension functions registered. Is this useful in real life? I cannot think of a use case. I would definitely like feedback on this issue. Another alternative would be to make people manipulate XPathContext explicitely, and provide it (as an optional argument?) when calling XPaths functions. I think that is ugly, and again I cannot see use cases for using two distinct sets of functions on a single document. Anybody disagrees? On that note, extension functions receive a XPathParserContext, which indirectly gives access to a lot of relevant information that an extension function might need. For example, it gives access to the XPathContext, which can give access to the xsltTransformContext, which gives you access to the node being constructed as a result of a XSL transformation... I have built extension functions that use the latter; and of course the XPathContext tells you the node where the xpath is being evaluated. Is there anything else that people here know they might need from any of those contexts in XPath extension functions? (Not to mention XSL extension elements, when I get there...) Because we would have to put methods/accesors on appropriate Python wrappers around all those. And worry about wrapping and hence releasing these. Sigh. (I am thinking of putting a lot of accessors on the XPathContext only, so we do not have to wrap the other ones in Python and worry about memory management. Objections?) OK, I am through bombarding people for a while, my next step when I look at this next will be to build a test suite... MAP
participants (3)
-
Kapil Thangavelu
-
Marc-Antoine Parent
-
Martijn Faassen