# 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 `samples/cs` directory.

All the classes referred to below are in the namespace `Saxon.Api`, and can be loaded from the assembly `saxonapi.dll`.

The first thing the application needs to do is to create a `Processor`. The `Processor` 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 `Processor`.

XSLT, XQuery, and XPath processing all follow the same pattern:

- From the `Processor`, create a Compiler for the appropriate language, using one of the methods `NewXsltCompiler`, `NewXQueryCompiler`, or `NewXPathCompiler`.

- Set any required properties or configuration options on the resulting Compiler object (these establish the static evaluation context), and then call its `Compile` method to create an Executable. The `Compile` methods 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 `Load` method on the Executable. The resulting loaded object is an `XsltTransformer`, `XQueryEvaluator`, or `XPathSelector` depending 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 `Run` in the case of XSLT or XQuery where the output is a newly constructed XML document, or `Evaluate`, `EvaluateSingle`, or `GetEnumerator` in 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:

1. `XdmValue`: an XPath value. This is in general a sequence, whose items are nodes or atomic values. You can supply an `XdmValue` as the value of a stylesheet or query parameter, and receive an `XdmValue` as the result of evaluating a query or XPath expression.

2. `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 `GetEnumeration` on an `XdmValue` object to iterate over the items in the sequence.

3. `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 `EnumerateAxis` which allows you to find related nodes using any of the 13 XPath axes. For convenience, the `OuterXml` property provides a simple way to serialize the node.

4. `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.

The `Processor` provides a method `NewDocumentBuilder` which, as the name implies, returns a `DocumentBuilder`. This may be used to construct a document (specifically, an `XdmNode`) from a variety of sources. The input can come from raw lexical XML by specifying a `Stream` or a `Uri`, or it may come from a DOM document built using the Microsoft XML parser by specifying an `XmlNode`, 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`.

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`. The `SchemaValidator`, in turn, is used to validate a source document against the set of schema definitions held in the `SchemaManager`'s cache.

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 `XdmDestination` allow such results to be serialized as XML (using either the Saxon serializer or an `XmlTextWriter`), or to be materialized as a Saxon `XdmNode` or as a DOM `XmlNode`.

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:

Interface class | Property | Implementation class
Saxon.Api.Processor | Implementation | net.sf.saxon.Configuration
Saxon.Api.XdmNode | Implementation | net.sf.saxon.om.NodeInfo
Saxon.Api.XsltTransformer | Implementation | net.sf.saxon.Controller
Saxon.Api.XQueryCompiler | Implementation | net.sf.saxon.query.StaticQueryContext

Next