Excellent, works great.
One follow-up question.
How would I go about making that function available so that a namespace declaration for the function in the xquery expression is unnecessary?
Ideally I'd just like to make this function available from the default namespace or define a reserved namespace prefix (yes, I realize that's a bit naughty...).

What I'm trying to avoid is simplify the job of the XQuery expression author. 80-90% of the time, the XQuery expressions will be simple one liner XPath 1.0 type statements where things all fit well within the default namespace.
Thanks again,

-----Original Message-----
From: Michael Kay [mailto:mike@saxonica.com]
Sent: March 7, 2005 13:25
To: saxon-help@lists.sourceforge.net
Subject: RE: [saxon] Extension function example

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() }

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


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,


Discover the Power of MetaSolv
Subscribe to our Company Newsletter

Discover the Power of MetaSolv
Subscribe to our Company Newsletter