<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html> <head> <this-is section="xpath-api" page="jaxp-xpath" subpage="factory"/> <!-- Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: Selecting the XPath implementation</title> <meta name="coverage" content="Worldwide"/> <meta name="copyright" content="Copyright Saxonica Ltd"/> <meta name="title" content="Saxonica: XSLT and XQuery Processing: Selecting the XPath implementation"/> <meta name="robots" content="noindex,nofollow"/> <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/> </head> <body class="main"> <h1>Selecting the XPath implementation</h1> <p>An application using the JAXP 1.3 XPath API starts by instantiating a factory class. This is done by calling:</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code> XPathFactory xpathFactory = XPathFactory.newInstance(objectModel); XPath xpath = xpathFactory.newXPath(); </code> </pre> </div> <p>Here <code>objectModel</code> is a URI that identifies the object model you are using. Saxon recognizes the following values for the object model:</p> <table> <thead> <tr> <td content="para"> <p> <b>Symbolic name</b> </p> </td> <td content="para"> <p> <b>Meaning</b> </p> </td> </tr> </thead> <tbody> <tr> <td content="para"> <p>XPathConstants.DOM_OBJECT_MODEL</p> </td> <td content="para"> <p>The DOM object model</p> </td> </tr> <tr> <td content="para"> <p>NamespaceConstant.OBJECT_MODEL_SAXON</p> </td> <td content="para"> <p>Saxon's native object model. This means anything that implements the <a class="bodylink" href="../../javadoc/net/sf/saxon/om/NodeInfo.html"><code>NodeInfo</code></a> interface, including the standard tree, the tiny tree, and third-party implementations of <code>NodeInfo</code>.</p> </td> </tr> <tr> <td content="para"> <p>NamespaceConstant.OBJECT_MODEL_JDOM</p> </td> <td content="para"> <p>The JDOM object model</p> </td> </tr> <tr> <td content="para"> <p>NamespaceConstant.OBJECT_MODEL_XOM</p> </td> <td content="para"> <p>The XOM object model</p> </td> </tr> <tr> <td content="para"> <p>NamespaceConstant.OBJECT_MODEL_DOM4J</p> </td> <td content="para"> <p>The DOM4J object model</p> </td> </tr> </tbody> </table> <p>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 <code>javax.xml.xpath.XPathFactory</code> is set to the value <code>net.sf.saxon.xpath.XPathFactoryImpl</code>. 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.</p> <p>Alternatively, if you know that you want to use the Saxon implementation, you can simply instantiate the class <a class="bodylink" href="../../javadoc/net/sf/saxon/xpath/XPathFactoryImpl.html"><code>net.sf.saxon.xpath.XPathFactoryImpl</code></a> directly. If you want to take advantage of features in Saxon-PE (Professional Edition), use <a class="bodylink" href="../../javadoc/com/saxonica/config/ProfessionalXPathFactory.html"><code>com.saxonica.config.ProfessionalXPathFactory</code></a>, or for Saxon-EE (Enterprise Edition) use <a class="bodylink" href="../../javadoc/com/saxonica/config/EnterpriseXPathFactory.html"><code>com.saxonica.config.EnterpriseXPathFactory</code></a>.</p> <p>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 <a class="bodylink" href="../../javadoc/net/sf/saxon/Configuration.html"><code>Configuration</code></a>. When you create an <code>XPathFactory</code>, a Saxon <code>Configuration</code> 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 <code>XPathFactory</code> that uses a preexisting <code>Configuration</code>.</p> <p>Saxon's implementation of <code>java.xml.xpath.XPath</code> is the class <a class="bodylink" href="../../javadoc/net/sf/saxon/xpath/XPathEvaluator.html"><code>net.sf.saxon.xpath.XPathEvaluator</code></a>. 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.</p> <p>The <code>XPath</code> 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 <code>XPathExpression</code>) 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.</p> <table width="100%"> <tr> <td> <p align="right"><a class="nav" href="context-node.xml">Next</a></p> </td> </tr> </table> </body> </html>