Using XQuery from C#
Note that the current version of SaxonCS offers "enterprise edition" capability only.
You can perform a query using the Saxon.Api interface as follows:
-
Create a 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 one of theBuild()
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
NewXQueryCompiler()
to create an XQueryCompiler. Then set any options that are local to a specific compilation (for example, the destination of error messages, the base URI, or the character encoding of the query text). -
Call one of the
Compile()
methods to compile a query. The result is anXQueryExecutable
, which can be used as often as you like in the same thread or in different threads. -
To run a query, first call the
Load()
method on theXQueryExecutable
. This creates an XQueryEvaluator. TheXQueryEvaluator
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 values of external variables, and the destination for the query result), and then call any of the methodsEvaluate()
,EvaluateSingle()
,Run()
,RunStreamed()
, orGetEnumerator()
methods to execute the query. -
Because the
foreach(XdmItem item in compiler.Compile("1 to 10").Load()) {...}XQueryEvaluator
is anIEnumerable
, it is possible to iterate over the results directly using the C# "foreach" construct: The result of the
if (section.Select(Descendant("para").Where(Eq(Attribute("class"), "normal")).Exists()) {...}XQueryEvaluator.Evaluate()
method is anXdmValue
. Saxon provides powerful facilities (modeled on LINQ) to process anXdmValue
using steps and predicates. For example, if the query returns a single nodesection
, (XdmNode
is a subclass ofXdmValue
), then you could test whether it has any descendantpara
elements with@class="normal"
by writing
The output of the query may be retrieved as an iterator over a sequence of items, or it
may be specified as an IDestination object, which allows a wide range of possibilities. The most
common IDestination
is a Serializer, which can format the result as XML, HTML, or JSON. Alternatively, you can build a tree
either in Saxon's native format (represented by the class Saxon.Api.XdmNode
) or
as a DOM.
Examples of C# queries are included in the saxon-resources
file, see
module APIExamples.cs.
Separate compilation of library modules
Under Saxon-EE, it is possible to compile library modules separately from the main
module. This reduces the compilation time and memory usage when the same library module
is imported by many main modules for different queries. A method
CompileLibrary()
(with a number of overloaded variants supplying the
input in different ways) is provided in the XQueryCompiler
class; any
library module compiled using this method will be available to all subsequent
compilations using the same XQueryCompiler
. To import the module, simply
use import module
specifying the module URI in the normal way. It is not
necessary to supply a module location hint (at "URI"
), and if any is
supplied, it will be ignored.