XQuery from a Python application
You can perform a query using the SaxonC Python interface as follows:
- Create a PySaxonProcessor
and set any global configuration options on the
PySaxonProcessor
object. If your query uses any features that require saxoncpe or saxoncee, be sure to use the constructorproc = PySaxonProcessor(license=True)
. -
Optionally, build the source document by calling
new_document_builder()
to create a document builder, setting appropriate options, and then calling theparse_xml()
method. This returns a PyXdmNode which can be supplied as input to the query either as the context item, or as the value of an external variable. -
Call
new_xquery_processor()
to create an PyXQueryProcessor. 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 theset_query_content()
method, or from a file using theset_query_file()
method. -
Call one of the methods
run_query_to_string()
,run_query_to_value()
orrun_query_to_file()
to compile and execute the query. As the names suggest these methods return the output of the query as a serialized string (str),PyXdmValue
object or file on disk, respectively.
Note that the PyXQueryProcessor
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 a PyXQueryProcessor
is
not really suitable for reuse.