XQuery from a PHP application
You can perform a query using the SaxonC PHP 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.
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.