[lxml-dev] Python values in xpath functions
Stefan or others, Using XPath Python functions, right now I instantiate a global object that functions can work on. That works fine, however for multi-threading applications in the future that won't work. What do you thinks is the best approach: locking the lxml process while running an XSLT transformation or searching/finding (or waiting for) a way to pass Python variables through to XPath functions? Kind regards, Petr ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------
Hi Petr, Petr van Blokland wrote:
Using XPath Python functions, right now I instantiate a global object that functions can work on. That works fine, however for multi-threading applications in the future that won't work. What do you thinks is the best approach: locking the lxml process while running an XSLT transformation or searching/finding (or waiting for) a way to pass Python variables through to XPath functions?
I'm not quite sure what you're trying to do, but if it's about stateful functions, I'd implement them as callable objects. You can always use normal Python locking to serialise access to the object state. Stefan
On Jun 20, 2006, at 10:36 PM, Stefan Behnel wrote:
Hi Petr,
Petr van Blokland wrote:
Using XPath Python functions, right now I instantiate a global object that functions can work on. That works fine, however for multi- threading applications in the future that won't work. What do you thinks is the best approach: locking the lxml process while running an XSLT transformation or searching/finding (or waiting for) a way to pass Python variables through to XPath functions?
I'm not quite sure what you're trying to do, but if it's about stateful functions, I'd implement them as callable objects. You can always use normal Python locking to serialise access to the object state.
Stefan
Stefan, something like the following: rq = None ... global rq rq = httprequest # e.g. coming from Twisted Matrix xsltree.transform(xmltree) ... Where an external XPath function might be implemented as: externalfunction(dummy, value): from xxxx import rq rq.value = value The question is about: is this the way to do it, or is there/will there be a way to pass the rq through XSLT. Petr ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------
Hi Petr, Petr van Blokland wrote:
rq = None
... global rq rq = httprequest # e.g. coming from Twisted Matrix xsltree.transform(xmltree) ...
Where an external XPath function might be implemented as:
externalfunction(dummy, value): from xxxx import rq rq.value = value
This may or may not work, depending on Python's way of handling local imports. You should really store the value in the function (either closure or class).
The question is about: is this the way to do it, or is there/will there be a way to pass the rq through XSLT.
As I said, I'd register a stateful object as function in this case. We do not currently have a way of accessing things like transform parameters or local XSLT variables from external functions. That would not work anyway for non-XPath object types. We could have something like a local context dictionary for each transformation, but I haven't seen any use cases that make the required API semantics clear enough. Stefan
Hi, may be someone can get me out. I am returning an etree from a Python function in XPath. But it does not seem to work stepping through the result as in <xsl:for-each select="myfunction()/*">...</xsl:for-each> where <xsl:for-each select="*">...</xsl:for-each>works fine for the current node. What do I do wrong. Should the function answer something different from an etree, as in: def myfunction(dummy, *args): ... # create etree from args return etree Kind regards, Petr van Blokland ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------
Hi Petr, Petr van Blokland wrote:
I am returning an etree from a Python function in XPath.
"etree" is the name of the module. I guess you mean an ElementTree object?
But it does not seem to work stepping through the result as in <xsl:for-each select="myfunction()/*">...</xsl:for-each> where <xsl:for-each select="*">...</xsl:for-each>works fine for the current node. What do I do wrong.
Don't return an ElementTree (don't you get an exception for that anyway?). Return an Element or a list of Elements. Stefan
On Jul 16, 2006, at 9:47 AM, Stefan Behnel wrote:
Hi Petr,
Petr van Blokland wrote:
I am returning an etree from a Python function in XPath.
"etree" is the name of the module. I guess you mean an ElementTree object?
Yes.
But it does not seem to work stepping through the result as in <xsl:for-each select="myfunction()/*">...</xsl:for-each> where <xsl:for-each select="*">...</xsl:for-each>works fine for the current node. What do I do wrong.
Don't return an ElementTree (don't you get an exception for that anyway?).
I do.
Return an Element or a list of Elements.
Ok, I'll try. Thanks. Petr ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------
participants (2)
-
Petr van Blokland
-
Stefan Behnel