[lxml-dev] XPath & XSLT Extension functions and node sets

So, I've been experimenting with custom python extension functions and have come across a surprising limitation. Is there any way to get an extension function to be able to return a list of text nodes, instead of only a list of XML nodes? It's easy enough to pass in a list of text nodes (i.e. '//text()' ), but I can't find a way to return that same list from an extension function. I tried returning the result directly from an xpath call, thinking that maybe the smart strings would work, but no dice. -- John Krukoff <jkrukoff@ltgc.com> Land Title Guarantee Company

John Krukoff wrote:
So, I've been experimenting with custom python extension functions and have come across a surprising limitation. Is there any way to get an extension function to be able to return a list of text nodes, instead of only a list of XML nodes?
It's easy enough to pass in a list of text nodes (i.e. '//text()' ), but I can't find a way to return that same list from an extension function. I tried returning the result directly from an xpath call, thinking that maybe the smart strings would work, but no dice.
Interesting. The code in extensions.pxi actually goes elif python.PySequence_Check(obj): resultSet = xpath.xmlXPathNodeSetCreate(NULL) for element in obj: if isinstance(element, _Element): node = <_Element>element xpath.xmlXPathNodeSetAdd(resultSet, node._c_node) else: xpath.xmlXPathFreeNodeSet(resultSet) raise XPathResultError, u"This is not a node: %r" % element so that's clearly not supported. I wonder if it can be. The problem is that the content of a node-set isn't freed when the node-set is, as the nodes are expected to be referenced in a document. So if we create new text nodes here, we'll most likely end up leaking their memory, simply because it will be impossible to tell if they originated from a document or from user provided strings. Supporting this for smart strings (that know their Element parent) might be relatively easy, but it's more tricky in the general case. It might work if we instead create a new Element and append the nodes to it. That would be a bit of an overhead, but it would enable garbage collection for us. Could you file a feature request for now? Stefan

On Thu, 2009-10-08 at 09:51 +0200, Stefan Behnel wrote: [ snipped ]
Could you file a feature request for now?
Stefan
Okay, added bug to launchpad: https://bugs.launchpad.net/lxml/+bug/446654 It doesn't look like launchpad will let me assign it to the wishlist, though. -- John Krukoff <jkrukoff@ltgc.com> Land Title Guarantee Company
participants (2)
-
John Krukoff
-
Stefan Behnel