This is indeed possible; it's mentioned in the documentation but there isn't an example, so I've just added one. Excuse the formatting
 

<p>A Java method called as an extension function may have an extra first argument of class <code>net.sf.saxon.expr.XPathContext</code>. This argument is not supplied by the calling XPath or XQuery code, but by Saxon itself.

The <code>XPathContext</code> object provides methods to access many internal Saxon resources, the most useful being <code>getContextItem()</code> which returns the context item from the dynamic context. The XPathContext object is available with static or instance-level methods, but not with constructors.</p>

<p>The following example shows a function that obtains the line number of the context node (this is actually a built-in Saxon extension):</p>

/**
* Return the line number of the context node.
*/

public static int lineNumber(XPathContext c) {
  Item item = c.getCurrentIterator().current();
  if (item instanceof NodeInfo) {
    return ((NodeInfo)item).getLineNumber();
  } else {
    return -1;
  }
}

<p>If this method appears in class <code>com.example.code.NodeData</code>, then it can be accessed using the following code in XSLT:</p>

<xsl:value-of select="nd:line-number()" xmlns:nd="java:com.example.code.NodeData"/>

<p>or the following in XQuery:</p>

<line xmlns:nd="java:com.example.code.NodeData">
   { nd:line-number() }
</line>

Michael Kay

Saxonica Limited



From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of Dueck, Brian
Sent: 07 March 2005 17:38
To: saxon-help@lists.sourceforge.net
Subject: [saxon] Extension function example

Hi;

Wondering if there is an example somewhere of how to define an extension function prior to compiling and invoking the XQuery.

The key requirement of this function is that when it is executed it must be able to transparently access the context node of the XQuery expression being executed (i.e. context node doesn't have to be explicitly passed by the user).

Hence, I don't think I can just use the standard Java extensibility provided by Saxon, but suspect I have to create something similar to the VendorFunctionLibrary provided by Saxon.

Any/all help would be greatly appreciated,

Brian.

Discover the Power of MetaSolv
Subscribe to our Company Newsletter