5. User defined extension functions

When XML Calabash is used with Saxon EE (and only EE, other editions do not support separate compilation), you can load user defined functions written in XSLT or XQuery with the cx:import extension.

<cx:import namespace="namespace-uri“
           href="module-file"
           type="content-type"/>
namespace-uri

The module namespace declared in the library module. (This attribute is only necessary for loading XQuery functions.)

module-file

The URI of the module file or stylesheet.

content-type

The type of extensions you are trying to load, application/xquery or application/xml+xslt.

These extension functions are then available in XPath expressions.

For example, if f.xqy contains:

xquery version "1.0";

module namespace f="http://xmlcalabash.com/ns/functions";

declare default function namespace "http://www.w3.org/2005/xpath-functions";

declare function f:count(
  $seq as item()*
) as xs:integer
{
  count($seq)
};

Then this pipeline will run:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:cx="http://xmlcalabash.com/ns/extensions"
                xmlns:f="http://xmlcalabash.com/ns/functions"
                xmlns:c="http://www.w3.org/ns/xproc-step"
                version="1.0" exclude-inline-prefixes="c">
   <p:output port="result">
     <p:pipe step="params" port="result"/>
   </p:output>

   <cx:import namespace="http://xmlcalabash.com/ns/functions" href="f.xqy"/>

   <p:parameters name="params">
     <p:input port="parameters">
       <p:empty/>
     </p:input>
     <p:with-param name="three" port="parameters" select="f:count((1,2,3))"/>
   </p:parameters>
</p:declare-step>

and produce output something like this:

<c:param-set xmlns:c="http://www.w3.org/ns/xproc-step">
  <c:param name="three" namespace="" value="3"/>
</c:param-set>

At present, the dynamically loaded functions are not scoped in any way. They're available globally after they've been loaded.