XQuery from a PHP application

You can perform a query using the SaxonC PHP interface as follows:

  1. 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 constructor new SaxonProcessor(true).
  2. Optionally, build the source document by calling newDocumentBuilder() to create a document builder, setting appropriate options, and then calling the parseXmlFromFile() or parseXmlFromString() 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.

  3. 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 the setQueryContent() method, or from a file using the setQueryFile() method.

  4. Call one of the methods runQueryToString(), runQueryToValue() or runQueryToFile() 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 an XdmValue 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.