[lxml-dev] Context of custom XPath functions

Frederik Elwert felwert at uni-bremen.de
Thu Aug 23 13:15:54 CEST 2007


Hi,

Am Donnerstag, den 23.08.2007, 12:42 +0200 schrieb Stefan Behnel:
> Frederik Elwert wrote:
> > Currently, I'm trying to implement some of the XForms functions with
> > lxml. The most urgent one is instance(), which gets the instance data
> > from an XForms model. But if you're not familiar with XForms, don't
> > bother, just think of id() or something similar:
> > 
> > With id(), it is necessary to access the context of the function call,
> > i.e., the tree in which it is called. id() is supposed to return an
> > element node (or node-set) matching the given argument. So id() has to
> > know about the tree, and not only about it's given argument.
> 
> I assume that instance() does not get any nodes as parameter. Otherwise you
> could call their getroottree() to retrieve the ElementTree of the current
> document.

Right, instance() get's a string as parameter, if any. So this doesn't
work.

> 
> > So how would one implement id() or any similar function with lxml? As
> > far as I got with custom functions, they only can handle the arguments
> > they get passed direcly, but they don't know about the broader context.
> > Any suggestions how I could solve this?
> 
> One way would be to define the functions local to each XPath call and provide
> them with the necessary context yourself.

I'm not sure I really understand how one would do this. But it sounds
interesting, so could you give an example or further reference? I'm
interested in a solution that I could get to work with current lxml, if
possible.

> If you want a more global solution, you may have noticed that functions
> receive a (currently empty) context object as first parameter. Maybe we should
> make that a real context object or a dictionary that includes the reference to
> the current document or its root node?

I already thought about that, too. This would be great! Maybe a dict is
a good idea, since one could add further context information in the
future.

I'm not sure, if the document itself or the root node would be the best
choice. Another XForms function, current(), "Returns the context node
used to initialize the evaluation of the containing XPath expression."
<http://www.w3.org/TR/xforms11/#fn-current>. So maybe this information
is most useful in general. One knows which element is the context for
the xpath-function, and from this, one can get the doc and root by
getroottree() etc.

And the context node seems to be introduces quite well in XPath, as I
just read in the XPath spec <http://www.w3.org/TR/xpath>. The
introduction defines the context of XPath expressions as:

      * a node (the context node)
      * a pair of non-zero positive integers (the context position and
        the context size)
      * a set of variable bindings
      * a function library
      * the set of namespace declarations in scope for the expression

So maybe this could be used as a reference for what to pass in a context
dict. Function library and namespaces are present, anyway, so they man
not be needed. Is this the case with XSLT-variables? I would guess so.
So a context dict might contain the context node, and, if useful for
anyone, context position and size.

But whatever you find most practical, I really like the idea of context
information for XPath functions!

Regards,
Frederik



-- 
Frederik Elwert, M.A.

Feldstr. 79A
28203 Bremen
0421.277 85 30

ICQ# 255-031-612
JabberID freedo at jabber.bettercom.de



More information about the lxml-dev mailing list