Selecting the XPath implementation
An application using the JAXP 1.3 XPath API starts by instantiating a factory class. To use Saxon as your XPath engine, call:
XPathFactory xpathFactory = new net.sf.saxon.xpath.XPathFactoryImpl(); XPath xpath = xpathFactory.newXPath();If you want to use Saxon as your XPath implementation, you must instantiate the class net.sf.saxon.xpath.XPathFactoryImpl directly, rather than using the method
XPathFactory.newInstance()
which relies on a classpath search.
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
(as described in JAXP XPath
API).