When using an XPath expression that uses external functions, declare the function signatures using an XStaticContext instance and supply (or bind) a Java implementation for each function using an XDynamicContext instance.
The XStaticContext interface has two declareFunction methods that each have three parameters—one for the name, one for the return type of the function, and an array for the types of the arguments. The name is always provided as a QName object, but the types can be QNames or XSequenceTypes. The function name, return type, and argument types must uniquely identify the function.
Method Signature | Purpose |
---|---|
declareFunction(QName name, QName type, QName[] argTypes) | Use when the return value and arguments of the
function are all single atomic values The type QNames must refer to built-in types or global types declared in a schema that has been registered on the XFactory instance used to create the XStaticContext instance. If a QName refers to a non-atomic type, the processor will treat it as the type element(*, ns:type), where ns:type is the given QName. The XTypeConstants interface has convenient constants available that provide a QName object for each built-in type. |
declareFunction(QName name, XSequenceType type, XSequenceType[] argTypes) | Use when any of the arguments or the return value of the function is a node or a sequence of atomic values or nodes |
// Create the factory XFactory factory = XFactory.newInstance(); // Create a new static context XStaticContext staticContext = factory.newStaticContext(); // Declare a namespace for the function staticContext.declareNamespace("my", "http://myfunc"); // Create a QName for the name of the function QName methodQName = new QName("http://myfunc", "pow"); // Declare the function on the static context staticContext.declareFunction(methodQName, XTypeConstants.DOUBLE_QNAME, new QName[]{XTypeConstants.DOUBLE_QNAME, XTypeConstants.DOUBLE_QNAME}); // Create an XPath executable for the expression XPathExecutable executable = factory.prepareXPath("sum(/polynomial/term/(my:pow(2, @power) * @coefficient))", staticContext);
Use Java reflection to obtain a java.lang.reflect.Method object for the function. If the method is an instance method, an instance object is required when binding this function.
An error is raised if you do not supply a Java method for a function that is used when executing the XPath expression.
Method Name | Purpose |
---|---|
bindFunction(QName qname, Method method) | Use when binding a static method |
bindFunction(QName qname, Method method, Object instanceObject) | Use when binding an instance method |
// Create a new dynamic context XDynamicContext dynamicContext = factory.newDynamicContext(); // Retrieve the java.lang.reflect.Method object for this function Method method = Math.class.getMethod("pow", Double.TYPE, Double.TYPE); // Bind the function to the dynamic context dynamicContext.bindFunction(methodQName, method); // Create an XML input document String xml = "<polynomial>" + "<term power='2' coefficient='3'/>" + "<term power='1' coefficient='-2'/>" + "<term power='0' coefficient='1'/>" + "</polynomial>"; StreamSource source = new StreamSource(new StringReader(xml)); // Execute the expression XSequenceCursor result = executable.execute(source, dynamicContext); // Serialize the result to System.out result.exportItem(new StreamResult(System.out));
In this information ... | IBM Redbooks, demos, education, and more(Index) Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience. This feature requires Internet access. Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof. |