Personally I'd advise against this: if your function namespace is accessible without a prefix, then the user is going to have to provide a prefix every time they access a function in the standard library, e.g. count(), position(), contains(), and I think most users will find this very confusing.
 
However, if you're determined then it can be done: simply call StaticQueryContext#setDefaultFunctionNamespace() to define the namespace URI to be used for unprefixed function calls.
 
Michael Kay
http://www.saxonica.com/


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

StaticQueryContext#declarePassiveNamespace() is doing it's job fine, except if I use an empty string "" as a namespace prefix.  No error or anything, but the function is not found unless I provide an actual value.
 
If at all possible, I'd like to allow my function to be accessed without any namespace prefix. How would I go about doing this?
 
Brian.
-----Original Message-----
From: Michael Kay [mailto:mike@saxonica.com]
Sent: March 7, 2005 17:33
To: saxon-help@lists.sourceforge.net
Subject: RE: RE: [saxon] Extension function example

The XML Way is that the user has to include the namespace declarations, never mind how verbose they are.
 
But XQuery doesn't always follow the XML Way, it allows predeclaration of namespaces outside the query context. So you can provide an API wrapper around the Saxon query API in which you predeclare a namespace by calling StaticQueryContext#declarePassiveNamespace(). For example, you could do this in a subclass of StaticQueryContext.
 
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 22:13
To: saxon-help@lists.sourceforge.net
Subject: RE: RE: [saxon] Extension function example

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,

Brian.
-----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() }
</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

Discover the Power of MetaSolv
Subscribe to our Company Newsletter

Discover the Power of MetaSolv
Subscribe to our Company Newsletter