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 the following 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 it is a good idea to 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.

Alternatively, if you know that you want to use the Saxon implementation, you can simply instantiate the class net.sf.saxon.xpath.XPathFactoryImpl directly. If you want to take advantage of features in Saxon-PE (Professional Edition), use com.saxonica.config.ProfessionalXPathFactory, or for Saxon-EE (Enterprise Edition) use com.saxonica.config.EnterpriseXPathFactory.

It is important to note that a compiled XPath expression can only be used with a source document that was built using the same Saxon Configuration. When you create an XPathFactory, a Saxon Configuration is created automatically. You can extract this configuration and use it to build source documents. Alternatively, there is a constructor that allows you to create an XPathFactory that uses a preexisting Configuration.

Saxon's implementation of java.xml.xpath.XPath is 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 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.