Java Reference
In-Depth Information
private
static
final
QName
name
=
new
QName("http://www.tutortutor.ca/",
"date",
"tt");
@Override
public XPathFunction resolveFunction(QName name, int ar-
ity)
{
if (name.equals(this.name) && arity == 1)
return new Date();
return null;
}
}
XPathFunctionResolver declares a single XPathFunction re-
solveFunction(QName functionName, int arity) method that XPath
callstoidentifythenameoftheextensionfunctionandobtainaninstanceofaJavaob-
ject whose evaluate() method implements the function.
The functionName parameteridentifiesthefunction'squalifiednamebecauseall
extension functions must live in a namespace, and must be referenced via a prefix
(which doesn't have to match the prefix in the document). As a result, you must also
bind a namespace to the prefix via a namespace context (as demonstrated previously).
The arity parameter identifies the number of arguments that the extension function
accepts, and is useful when overloading extension functions. If the functionName
and arity valuesareacceptable,theextensionfunction'sJavaclassisinstantiatedand
returned; otherwise, null is returned.
Finally, the function resolver class is instantiated and registered with the XPath
instance by calling XPath 's void setXPathFunctionResolv-
er(XPathFunctionResolver resolver) method.
Thefollowingexampledemonstratesallthesetaskstouse date() inXPathexpres-
sion //tt:contact[tt:date(tt:birth)>tt:date('1960-01-01')]/
tt:name/text() , which returns only those contacts whose date of birth is greater
than 1960-01-01 ( Jane Doe followed by Sandra Smith ):
DocumentBuilderFactory
dbf
=
DocumentBuilderFact-
ory.newInstance();
dbf.setNamespaceAware(true);
Search WWH ::




Custom Search