Custom User-Defined Functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom User-Defined Functions

Jeremy Nix
I've been trying to determine the best way to define a function that I
want to be available so that I can call it within XForms context (not
XSLT context).  In the past when I have needed this functionality, I
have created custom Java classes that contained static methods that can
be called directly.  In this case, I'm wanting to do a few complex XPath
expressions and return back a boolean result.  I came across the below
article and thought that I may be able to define a custom function in
XForms itself.

https://www.w3.org/MarkUp/Forms/wiki/Custom_XPath_functions

Is this possible?  If so, what namespace is function defined within?  
When I tried this, I receive an error that includes "Unknown control:
xf:function", which leads me to believe this functionality does not
exist.  If that is true, are there plans on incorporating this type of
functionality?

Thanks for the help.

--
_____________________________________________
Jeremy Nix
Senior Application Developer
Biomedical Informatics
Cincinnati Children's Hospital Medical Center
513-803-2764 / [hidden email]

--
You received this message because you are subscribed to the Google Groups "Orbeon Forms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Reply | Threaded
Open this post in threaded view
|

Re: Custom User-Defined Functions

Alessandro  Vernet
Administrator
Hi Jeremy,

Indeed, this custom XPath functions aren't supported. We have an RFE to support XPath 2.0 <xf:function>.

https://github.com/orbeon/orbeon-forms/issues/764

If you want to add your own function implemented in Java or Scala, you can add it to XFormsFunctionLibrary.scala, ideally adding it in your own namespace.

Alex
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet
Reply | Threaded
Open this post in threaded view
|

Re: Custom User-Defined Functions

jaygo
In reply to this post by Jeremy Nix
I'm not sure if it is a solution for your case but there is a way to create custom XPath functions in Java and use them in XForms directly without registering them in any external file. You have to create your own class with static methods, for example:
package com.example.xforms;
import org.orbeon.saxon.expr.XPathContext;

class Utils {
 
  /** Without context */
  public static boolean check( String param1, int param2 ) {
    [...]
  }

  /** With context */
  public static boolean checkByContext( XPathContext context, int param ) {
    [...]
  }
}

Then you have to declare namespace based on package name and class name inside your XForms file:
xmlns:utils="java:com.example.xforms.Utils"

and now you can use methods in XPath expression e.g.:
  <xf:bind ref="node1" required="utils:check('text', 2)" />
  <xf:bind ref="node2" required="utils:check-by-context(2)" />

Jar with this class can be placed in WEB-INF/lib folder inside orbeon war file.