Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > XML applications > Use the XML API to perform operations > Use external variables and functions > Use external functions


Use external functions with XPath

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.


Procedure

  1. When preparing an XPath expression that uses external functions, declare the function signatures using an XStaticContext 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.

    XStaticContext declareFunction methods.

    This table explains when to use each form of the declareFunction method.

    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

    The following example shows how to prepare an XPath expression that uses an external function.

    // 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);
    

  2. To execute an XPath expression that uses external functions, supply (or bind) the Java methods that implement the functions using an XDynamicContext instance.

    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 used when executing the XPath expression.

    The XDynamicContext has two bindFunction methods. Each requires a QName object corresponding to the name of the function and a Method object identifying the Java method that will provide the implementation for the function.

    XDynamicContext bindFunction methods.

    This table explains when to use each form of the XDynamicContext bindFunction methods.

    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

    The following example executes the XPath expression prepared in the first example, first binding a method for the function it uses. In this example, the static pow(double a, double b) method of the java.lang.Math class is used to provide the implementation for the external function.

    // 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
    result.exportItem(new StreamResult());
    


Use static and dynamic contexts
The XFactory class
Use sequence types
Map XML types to Java types

+

Search Tips   |   Advanced Search