XPathExecutor

public class XPathExecutor

XPathExecutor is an executor which evaluates a given xpath and yields the result.

Example:

XPathExecutor exec = new XPathExecutor(getDocument("doc.xml"));

exec.registerFunction("http://example.com", "sum", new MySum());

System.out.println(exec.eval( "3 + number('3') + eg:sum(3, 4, '5.1') + /values/one  - 2"));

// The result will be '17.1'
Author:Hugo Y. K. Lam

Constructors

XPathExecutor

public XPathExecutor()

Creates a new instance of XPathExecutor.

XPathExecutor

public XPathExecutor(Node document)

Creates a new instance of XPathExecutor.

Parameters:
  • document – the document containing the context being queried and the namespaces being referenced.

XPathExecutor

public XPathExecutor(Node context, Node namespaces)

Creates a new instance of XPathExecutor.

Parameters:
  • context – the document containing the context being queried.
  • namespaces – the document containing the namespaces being referenced.

Methods

eval

public Object eval(String expression)

Evaluates an XPath expression.

Parameters:
  • expression – the XPath expression.
Throws:
  • TransformerException – if unable to transform the expression.
Returns:

the evaluated result.

eval

public Object eval(String expression, Node context)

Evaluates an XPath expression.

Parameters:
  • expression – the XPath expression.
  • context – the document containing the context being queried.
Throws:
  • TransformerException – if unable to transform the expression.
Returns:

the evaluated result.

registerFunction

public void registerFunction(String ns, String funcName, XPathFunction func)

Registers a function to be used in an XPath.

Parameters:
  • ns – the namespace of the function.
  • funcName – the function name.
  • func – the function implementation.

visit

public void visit(String expression, XPathVisitor visitor)

Evaluates an XPath expression.

Parameters:
  • expression – the XPath expression.
  • visitor – the XPath visitor.
Throws:
  • TransformerException – if unable to transform the expression.