XQuery from a C++ application
You can perform a query using the SaxonC C++ interface as follows:
- Create a SaxonProcessor
and set any global configuration options on the
SaxonProcessor
object. If your query uses any features that require SaxonC-PE or SaxonC-EE, be sure to use the constructornew SaxonProcessor(true)
. -
Optionally, build the source document by calling
newDocumentBuilder()
to create a document builder, setting appropriate options, and then calling theparseXmlFromFile()
orparseXmlFromString()
methods. This returns an XdmNode which can be supplied as input to the query either as the context item, or as the value of an external variable. -
Call
newXQueryProcessor()
to create an XQueryProcessor. Then set any options required for the specific query execution (for example, the initial context item, the base URI, the values of external variables and the parameters of the query). The query can be supplied as a string using thesetQueryContent()
method, or from a file using thesetQueryFile()
method. -
Call one of the methods
runQueryToString()
,runQueryToValue()
orrunQueryToFile()
to compile and execute the query. As the names suggest these methods return the output of the query as a serialized string (as a char pointer array), or a pointer to anXdmValue
object or file on disk, respectively.
Alternatively, it is also possible to run basic one shot queries using the methods
executeQueryToString()
, executeQueryToValue()
and
executeQueryToFile()
directly on the XQueryProcessor
, when other options
are not needed. With these methods the query is supplied as an argument, so it is not necessary to
first supply the query to the XQueryProcessor
using setQueryContent()
or
setQueryFile()
.
Note that the XQueryProcessor
only provides compile-and-go methods to execute a
query directly without going through an explicit compilation process. This provides a simpler
approach if the query is only evaluated once; but means that an XQueryProcessor
is
not really suitable for reuse.