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 in the .NET API.
An example C# program illustrating various use cases for the API is available in the
directory, see API examples for .NET for further information
All the classes referred to below are in the namespace
The first thing the application needs to do is to create a 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
XSLT, XQuery, and XPath processing all follow the same pattern:
From the Processor, create a
Compilerfor the appropriate language, using one of the methods
Set any required properties or configuration options on the resulting
Compilerobject (these establish the static evaluation context), and then call its
Compile()method to create a corresponding
Compile()methods are overloaded to accept input from a variety of sources.
Executableobject 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
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:
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
EnumerateAxis()which 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.
The Processor provides a method
NewDocumentBuilder() which, as the name implies, returns
a 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.
The SchemaManager exists to compile schema documents
and to maintain a cache containing the compiled schemas. It thus contains methods 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 XdmDestination 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.
Underlying Java implementation classes
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 internal classes converted from the original Java code.
We do not publish API documentation for the C# version of these implementation classes. However, the Java API documentation for the original Java classes will generally provide a useful guide. To find the relevant class, you need to be aware of the mapping from C# type names to Java class names, specifically:
A C# type named
Saxon.Hej.x.y.FooBarcorresponds to the Java class
A C# type named
Saxon.Eej.x.y.FooBarcorresponds to the Java class
These underlying implementation classes are documented in Java terms and use Java naming conventions, but this does not stop them being used from any .NET language.
The places where such escape hatches are provided are shown below: