how to add support for custom Xquery function

Help
2012-09-07
2012-10-08
  • Taurus Soft
    Taurus Soft
    2012-09-07

    Hi

    I am planning to use Saxon in a java app to process XQuery commands. I want to
    add support for some custom XQuery functions available at Xqueryfunctions.com-
    to my app.

    How do I do this? Do I have to manually run all those functions each time an
    Xquery command is to be executed?
    I want to add support for those XQuery functions so that any user who accesses
    my java app and tries to run his XQuery command, is able to use those
    functions.

    Regards,
    Arvind.

     
  • Michael Kay
    Michael Kay
    2012-09-07

    The simplest approach is to explicitly import the external function library
    using an "import module" declaration in the query prolog.

    Saxon-EE allows you to compile the query library once using the s9api method
    XQueryCompiler.compileLibrary(); the module still needs to be imported into
    any query that uses it, but it will not be recompiled each time it is
    imported.

    You can of course provide a wrapper method that adds the "import module"
    declaration automatically, though you have to be a bit careful about order of
    declarations. (There might be a way of avoiding this by manipulating the
    function library list in the XQuery static context, but only if you are
    feeling adventurous...)

     
  • Taurus Soft
    Taurus Soft
    2012-09-15

    Hello Michael,

    I tried import module declaration, however this is the error I am getting--

    Error on line 1 column 0 
      XPST0003: XQuery syntax error in #module namespace  functx#:
        Module declaration must not be used in a main module
    

    I am trying to use the functions at
    http://xqueryfunctions.com-- in the following
    manner--

    module namespace  functx = "[url]http://www.functx.com[/url]" at "[url]http://anyaservices.net/xquery/functx-1.0.xq[/url]";
                declare variable $doc as node() external;
                let $page_title:= $doc//title[contains(.,'Error 404')]
                           ..........................................................................................
                           ..........................................................................................
    

    Kindly let me know how to rectify this error.

    Thanks,
    Arvind.

     
  • Michael Kay
    Michael Kay
    2012-09-15

    The correct syntax is

    import module namespace functx =
    "http://www.functx.com" at "http://anyaservices.net/
    xquery/functx-1.0.xq
    ";

    I'm not entirely sure what you got wrong but your code fragment seems to omit
    the "import" keyword, and to have various stray semicolons.