saxonica.com

Selecting the XPath implementation

An application using the JAXP 1.3 XPath API starts by instantiating a factory class. This is done by calling:


XPathFactory xpathFactory = XPathFactory.newInstance(objectModel);
XPath xpath = xpathFactory.newXPath();

Here objectModel is a URI that identifies the object model you are using. Saxon recognizes four values for the object model:

Symbolic name

Meaning

XPathConstants.DOM_OBJECT_MODEL

The DOM object model

NamespaceConstant.OBJECT_MODEL_SAXON

Saxon's native object model. This means anything that implements the NodeInfo interface, including the standard tree, the tiny tree, and third-party implementations of NodeInfo.

NamespaceConstant.OBJECT_MODEL_JDOM

The JDOM object model

NamespaceConstant.OBJECT_MODEL_XOM

The XOM object model

NamespaceConstant.OBJECT_MODEL_DOM4J

The DOM4J object model

To ensure that Saxon is selected as your XPath implementation, you must specify one of these constants as your chosen object model, and you must ensure that the Java system property javax.xml.xpath.XPathFactory is set to the value net.sf.saxon.xpath.XPathFactoryImpl. Normally, if Saxon is on your classpath then the Saxon XPath implementation will be picked up automatically, but if there are other implementations on the classpath as well then it is best to set the system property explicitly to be sure.

This API is based on the class net.sf.saxon.xpath.XPathEvaluator. This class provides a few simple configuration interfaces to set the source document, the static context, and the context node, plus a number of methods for evaluating XPath expressions.

The XPath object returned from the above calls allows you to set the static context for evaluating XPath expressions (you can pre-declare namespaces, variables, and functions), and to compile XPath expressions in this context. A compiled XPath expression (an object of class XPathExpression) can then be evaluated, with a supplied node (represented by a class in the selected object model) supplied as the context node. For further details, see the Javadoc specifications and the supplied example applications.

Next