Saxonica.com

The Standalone XPath API

Because JAXP 1.3 is not available as a standard component of JDK 1.4, Saxon also provides a standalone XPath API of its own, with no dependencies on the JAXP 1.3 classes. This is available in the package net.sf.saxon.sxpath, which is included in the standard JAR file: saxon9.jar or saxon9sa.jar. It is a simpler API with less capability than the JAXP API, for example it does not allow XPath expressions to contain references to external variables or user-defined functions.

This interface consists essentially of two classes. The XPathEvaluator class can be used to establish a static context for compiling an XPath expression, and provides a method compileExpression that takes an XPath expression as a string, and returns an object of class XPathExpression. The XPathExpression object can then be evaluated using its evaluate() method. This method takes as an argument an object of class Source to represent the document against which the expression is evaluated. This can be a document that has already been parsed and built as a tree structure (represented, for example, by a DOMSource object or by a Saxon NodeInfo, or it can be an unparsed XML document supplied as a StreamSource or a SAXSource. The result of the expression is returned as a Java List, which may contain a mixture of nodes and atomic values. Nodes are represented using the class NodeInfo, unless the source used a different object model in which case they are returned using the node classes of that object model. Atomic values are represented using the appropriate Java class, for example Double, Boolean, or String.

Further details of these classes are available in the JavaDoc documentation.

Next