Saxon API for .NET
A new API has been developed providing access to XSLT, XQuery, XPath, and XML Schema processing on the .NET platform. This is available from any .NET-supported language, although the examples are all expressed in C# terms.
This section provides a brief introduction to the structure and concepts of the API. Full specifications are available here.
A set of example C# programs illustrating various use cases for the API is available in the
All the classes referred to below are in the namespace
Saxon.Api, and can be loaded from the
The first thing the application needs to do is to create a
holds configuration information for Saxon, and shared resources such as the name pool and schema pool. It is possible
to run multiple processors concurrently if required, but it is usually more economical for all Saxon processes within
a single application to use the same
XSLT, XQuery, and XPath processing all follow the same pattern:
Processor, create a Compiler for the appropriate language, using one of the methods
Set any required properties or configuration options on the resulting Compiler object (these establish the static evaluation context), and then call its
Compilemethod to create an Executable. The
Compilemethods are overloaded to accept input from a variety of sources.
The Executable object represents the compiled stylesheet, query, or XPath expression. It can be evaluated as often as required, in the same thread or in different threads. The first stage in this evaluation is to call the
Loadmethod on the Executable. The resulting loaded object is an
XPathSelectordepending on the language in use.
Properties and configuration methods can then be set on the loaded object to establish the dynamic evaluation context, and the real processing is then finally invoked using another method: this may be
Runin the case of XSLT or XQuery where the output is a newly constructed XML document, or
GetEnumeratorin the case of XQuery and XPath where the output is an arbitrary sequence.
The API includes a number of classes that reflect the XSLT/XQuery/XPath data model (XDM). These are as follows:
XdmValue: an XPath value. This is in general a sequence, whose items are nodes or atomic values. You can supply an
XdmValueas the value of a stylesheet or query parameter, and receive an
XdmValueas the result of evaluating a query or XPath expression.
XdmItem: an XPath item. This is a subtype of
XdmValue, since any item can be treated as a sequence of length one. You can call
XdmValueobject to iterate over the items in the sequence.
XdmNode: a node. This object provides access to most of the properties of nodes defined in the XDM model: the node kind, the string value, the name, the typed value, the base URI. It also provides a method
EnumerateAxiswhich allows you to find related nodes using any of the 13 XPath axes. For convenience, the
OuterXmlproperty provides a simple way to serialize the node.
XdmAtomicValue: an atomic value, as defined in the XDM model. You can construct an atomic value directly from common objects such as an integer, a string, a double, or a Uri; or you can construct one by specifying a string containing the lexical representation, and a QName identifying the required type.
Processor provides a method
NewDocumentBuilder which, as the name implies, returns
DocumentBuilder. This may be used to construct a document (specifically, an
from a variety of sources. The input can come from raw lexical XML by specifying a
Stream or a
or it may come from a DOM document built using the Microsoft XML parser by specifying an
or it may be supplied programmatically by nominating an
XmlReader. Various processing options
can be set as properties of the
DocumentBuilder: these determine, for example, how whitespace is handled
and whether schema validation is performed. The resulting document can be
used as the input to a transformation, a query, or an XPath expression. It might also contain a stylesheet or a schema
which can then be used as input to the
XsltCompiler or the
SchemaManager exists only in the
Saxon-EE product, and its job is to compile schema documents
and to maintain a cache containing the compiled schemas. It thus contains method to compile schemas from a variety
of document sources. It also contains a factory method
NewSchemaValidator, which returns a
SchemaValidator, in turn, is used to validate a source document
against the set of schema definitions held in the
Finally, the API offers a class
XmlDestination to define the possible ways of handling a document
constructed as the output of a transformation, query, or validation episode. Various subtypes of
allow such results to be serialized as XML (using either the Saxon serializer or an
or to be materialized as a Saxon
XdmNode or as a DOM
These classes are designed to be combined in arbitrary ways. For example, you might run an XQuery whose result is a sequence of newly-constructed document nodes. You could then iterate over these nodes, and for each one, apply an XSLT transformation whose result is then serialized.
There are several places where the classes in the
Saxon.Api package provide an "escape hatch" into
the underlying implementation classes. These are provided for the benefit of applications that for some reason
need to mix use of the .NET API with the established Java API. The underlying implementation classes are documented
in Java terms and use Java calling conventions, but this does not stop them being used from any .NET language:
you may need to consult the IKVM documentation for details of the mappings.
The places where such escape hatches are provided are shown below: