<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="using-xquery" page="callingfunctions" subpage=""/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: Calling XQuery Functions from Java</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title"
            content="Saxonica: XSLT and XQuery Processing: Calling XQuery Functions from Java"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>Calling XQuery Functions from Java</h1>
      <p>Although the usual way to invoke XQuery from a Java application is to compile and execute a query
as described above, it is also possible to invoke individual XQuery functions directly from Java if required.
This interface is very efficient but performs less validation of parameters than the standard interface described above.
To achieve this, first compile the query as described above, specifying a query that includes one or more
<code>declare function</code> declarations in the query prolog. It is then possible to retrieve the 
<code>UserFunction</code> objects representing the compiled code of these functions, by calling the
<code>getUserDefinedFunction</code> method on the <code>StaticQueryContext</code> object.
As discussed above, the information is not held in the original <code>StaticQueryContext</code> object
(which Saxon does not alter), but in a modified copy which is obtainable from the <code>XQueryExpression</code>
object. Once found,
such a function can be called using its <code>call</code> method. The first argument to this is an array 
containing the values of the arguments. These must be supplied using Saxon's native classes, for example
an integer argument is supplied as an instance of <code>net.sf.saxon.value.IntegerValue</code>. These
values must be of the type expected by the function; no conversion or type checking takes place (which
means that a ClassCastException will probably occur if the wrong type of value is supplied). The second
argument to the <code>call</code> method is a <code>Controller</code>. A Controller can be obtained
by calling the <code>getController()</code> method on the <code>XQueryExpression</code> object. The same Controller
can be used for a series of separate function calls.</p>
      <p>For example:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
Configuration config = new Configuration();
StaticQueryContext sqc = config.newStaticQueryContext();

XQueryExpression exp1 = sqc.compileQuery(
        "declare namespace f='f.ns';" +
        "declare function f:t1($p as xs:integer) { $p * $p };" +
        "declare function f:t2($p as xs:integer) { $p + $p };" +
        "1"
);

StaticQueryContext sqc2 = exp1.getStaticContext();
UserFunction fn1 = sqc2.getUserDefinedFunction("f.ns", "t1", 1);
UserFunction fn2 = sqc2.getUserDefinedFunction("f.ns", "t2", 1);
Controller controller = exp1.getController();

IntegerValue[] arglist = new IntegerValue[1];
for (int x=1; x&lt;1000000; x++) {
    arglist[0] = new IntegerValue(x);
    Value v1 = fn1.call(arglist, controller);
    Value v2 = fn2.call(arglist, controller);
    System.err.println("Returned product " + v1 + "; sum =" + v2);
}</code>
         </pre>
      </div>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="resultformat.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>