XPath from a PHP application

The SaxonC PHP interface is custom-designed API for XPath processing, allowing integrated access to all Saxon's XML processing capabilities in a uniform way. The PHP API is directly built upon the PHP API for XPath.

You can evaluate an XPath expression using the PHP interface in a number of ways, for instance as follows:

  1. Create a Saxon\SaxonProcessor and set any global configuration options on the SaxonProcessor.

  2. Build the source document by calling newDocumentBuilder() to create a DocumentBuilder, setting appropriate options, and then calling the parseXmlFromFile() or parseXmlFromString() methods. This returns a Saxon\XdmNode which can be supplied as the context item to the XPath expression.

  3. Call newXPathProcessor() to create a Saxon\XPathProcessor, and set any options required for the specific XPath execution (for example, declaring namespace prefixes that are used in the XPath expression, the initial context node, and the values of any variables referenced in the expression).

  4. To compile and evaluate the expression, call one of the methods evaluate(), evaluateSingle() or effectiveBooleanValue() to execute a specified XPath expression.

  5. The result of an XPath expression is in general an XdmValue, representing a value as defined in the XDM data model (that is, a sequence of nodes and/or atomic values). Subclasses of XdmValue include XdmItem, XdmNode, and XdmAtomicValue, and these relate directly to the corresponding concepts in XDM. Various methods are available to translate between this model and native PHP data types.

Note that the XPathProcessor only provides compile-and-go methods to execute an expression directly without going through an explicit compilation process. This provides a simpler approach if the expression is only evaluated once; but means that an XPathProcessor is not really suitable for reuse.

Examples of the use of the PHP API to evaluate XPath expressions are included in the SaxonC download file in the directory samples/php, see file xpathExamples.php.