XPath from a C++ application
The SaxonC C++ interface is custom-designed API for XPath processing, allowing integrated access to all Saxon's XML processing capabilities in a uniform way. The C++ interface also provides the internal API on which the Python and PHP APIs are built.
You can evaluate an XPath expression using the C++ interface in a number of ways, for instance as follows:
-
Create a SaxonProcessor and set any global configuration options on the
SaxonProcessor
. -
Build the source document by calling
newDocumentBuilder()
to create aDocumentBuilder
, setting appropriate options, and then calling theparseXmlFromFile()
orparseXmlFromString()
methods. This returns an XdmNode which can be supplied as the context item to the XPath expression. -
Call
newXPathProcessor()
to create an 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). -
To compile and evaluate the expression, call one of the methods
evaluate()
,evaluateSingle()
oreffectiveBooleanValue()
to execute a specified XPath expression. -
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 C++ 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 C++ API to evaluate XPath expressions are included in the
SaxonC download file in the directory samples/cppTests
, see file
testXPath.cpp
.