Project

Profile

Help

Programmatically adding external information to an XPath custom function

Added by Andrea Desole over 9 years ago

I have some XPath extensions functions to implement. The problem is that these functions use information that is not provided by the function parameters, and it changes, possibly before each function call. The most obvious solution would be to add this information to my extension function call. Every time I set the information accordingly. This can cause problems with multithreading, as separate threads can try to set the information in the same function. I wanted to ask if there is a better way, that possibly frees me of multithreading related headaches.

Thanks


Replies (2)

RE: Programmatically adding external information to an XPath custom function - Added by Michael Kay over 9 years ago

It's hard to give advice without knowing where this extra information comes from.

Generally (as you've recognized) you need to be very careful about implementing anything that isn't a "pure function" - that is, one whose result depends solely and exclusively on the values supplied in the function parameters. Otherwise, optimization can change the effect in a way you didn't expect, for example by moving function calls out of loops, or by lazy evaluation strategies that mean functions aren't evaluated in the order you expect.

The way the spec handles this with system-defined functions is to permit functions to depend on information in the static and dynamic context. The context is also available to user-written extension functions. You can put your own information in the context by use of the getUserData() and setUserData() methods on the Controller - you can get the Controller using context.getController(), and there is always precisely one instance of the Controller for the duration of execution of a single query or stylesheet. But this doesn't solve the order of execution problem: if one function A writes data in the context, and another function B reads it, this doesn't guarantee that A will be executed before B.

RE: Programmatically adding external information to an XPath custom function - Added by Andrea Desole over 9 years ago

I think this is what I need. To give more information: it's about XBRL formulas, and specifically value assertions, although I expect this problem to show up is more situations later. In my case the value assertions need some information, like the matched variables, that are set from the code before the XPath expression is evaluated. I don't expect dependencies between functions, and one expression evaluation is performed in one thread, although I can have more threads evaluating the same expression. So the way to go would be to access the controller from the selector before the expression is evaluated, via getUnderlyingXPathContext().getXPathContextObject().getController(), and during the evaluation directly from the XPathContext, using setUserData and getUserData in the controller to exchange the external information. Yes, this looks like the solution. Thanks for the answer

    (1-2/2)

    Please register to reply