<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="extensibility" page="integratedfunctions" subpage="ext-simple-J"/>
      <!--
           Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: Java extension functions: simple interface</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title"
            content="Saxonica: XSLT and XQuery Processing: Java extension functions: simple interface"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>Java extension functions: simple interface</h1>
      <p>The simple API for integrated Java extension functions is 
         available via the s9api class <a class="bodylink" href="../../javadoc/net/sf/saxon/s9api/ExtensionFunction.html"><code>ExtensionFunction</code></a>. Here
      is an example that defines an extension function to calculate square roots, registers this extension function with the s9api
      <code>Processor</code>, and then invokes it from an XPath expression:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
            Processor proc = new Processor(false);
            ExtensionFunction sqrt = new ExtensionFunction() {
                public QName getName() {
                    return new QName("http://math.com/", "sqrt");
                }

                public SequenceType getResultType() {
                    return SequenceType.makeSequenceType(
                        ItemType.DOUBLE, OccurrenceIndicator.ONE
                    );
                }

                public net.sf.saxon.s9api.SequenceType[] getArgumentTypes() {
                    return new SequenceType[]{
                        SequenceType.makeSequenceType(
                            ItemType.DOUBLE, OccurrenceIndicator.ONE)};
                }

                public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
                    double arg = ((XdmAtomicValue)arguments[0].itemAt(0)).getDoubleValue();
                    double result = Math.sqrt(arg);
                    return new XdmAtomicValue(result);
                }
            };

            proc.registerExtensionFunction(sqrt);
            XPathCompiler comp = proc.newXPathCompiler();
            comp.declareNamespace("mf", "http://math.com/");
            comp.declareVariable(new QName("arg"));
            XPathExecutable exp = comp.compile("mf:sqrt($arg)");
            XPathSelector ev = exp.load();
            ev.setVariable(new QName("arg"), new XdmAtomicValue(2.0));
            XdmValue val = ev.evaluate();
            String result = val.toString();         
            </code>
         </pre>
      </div>
      <p>Full details of the interface are defined in the Javadoc for class 
         <a class="bodylink" href="../../javadoc/net/sf/saxon/s9api/ExtensionFunction.html"><code>ExtensionFunction</code></a>.</p>
      <p>The main restrictions of the simple interface are (a) that the extension function has no access to static or dynamic context information,
      and (b) that it does not support pipelined evaluation of the arguments or result. To avoid these restrictions, use the full interface
      described on the next page.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="ext-full-J.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>