Using s9api for XQuery
You can perform a query using the s9api interface as follows:
Create a Processor (
net.sf.saxon.s9api.Processor) and set any global
configuration options on the Processor.
Optionally, build the source document by calling
newDocumentBuilder() to create a document builder,
setting appropriate options, and then calling the
build() method. This returns an
which can be supplied as input to the query either as the context item, or as the value of an external variable.
newXQueryCompiler() to create an XQuery Compiler, and set any options that are local to a specific
compilation (for example, the destination of error messages).
compile() method to compile a query. The result is an
which can be used as often as you like in the same thread or in different threads.
To run a query, call the
load() method on the
XQueryExecutable. This creates
XQueryEvaluator can be serially reused, but it must not be shared across
multiple threads. Set any options required for the specific query execution (for example, the initial context node, the
valus of external variables, and the destination for the query result), and then call either the
run() method to execute the query.
XQueryEvaluator is an
Iterable, it is possible to iterate over the results
directly using the Java 5 "for-each" construct.
The output of the query may be retrieved as an iterator over a sequence of items, or it may is specified as a
Destination object, which allows a wide range of possibilities:
you can send the output to a serializer, or to a SAX ContentHandler. You can build a tree either in Saxon's native format
by the s9api class
XdmNode) or as a DOM. You can send the output to be validated against a schema by nominating a
SchemaValidator as the destination, or you can pipe it through an XSLT transformation,
XsltTransformer also implements the
Examples of s9api queries are included in the Saxon resources file, see module S9APIExamples.java.
This sample application was accidentally omitted from the 188.8.131.52 build. It can be obtained at