XSLT transformations from a C application
The SaxonC C API can be used to invoke transformations from your own C application. Functions are provided to perform XSLT transformations in SaxonCProcessor.h. Note that this API is more basic than the C++ version, in particular, with the C API the compile and run phases happen together within the same function call.
For these XSLT transformation functions, the XML document source and XSLT stylesheet are supplied
as arguments, and stylesheet parameters and other properties are supplied using the structs
sxnc_parameter
and sxnc_property
, respectively. The transformation
results can be output to file using the xsltSaveResultToFile()
function, or string
representation using the xsltApplyStylesheet()
function.
You can perform a transformation using the SaxonC C interface as follows:
-
The first step is the memory allocation for the processor and variables required by SaxonC. To simplify the process, the utility function
initSaxonc()
can be used to alloc the following variables:- environ - SaxonC environment given as the struct
sxnc_environment
. - processor - The SaxonC processor data structure given as the struct
sxnc_processor
which is used as a reference to the SaxonProcessor
object in Java. - parameters - The
sxnc_parameter
struct (an array) is used to represent the stylesheet parameters supplied to the XSLT transform. For further details see the Configuration section in the SaxonC documentation. - properties - The
sxnc_property
struct (an array) is used to represent the required configuration properties and options for the processor. For a full list of the available properties see the Configuration section in the SaxonC documentation.
For example:
int cap = 10; sxnc_parameter *parameters; int parLen = 0, parCap; parCap = cap; sxnc_property *properties; int propLen = 0; parCap = cap; sxnc_environment *environ; sxnc_processor *processor; initSaxonc(&environ, &processor, ¶meters, &properties, parCap, parCap); - environ - SaxonC environment given as the struct
-
Next set up the GraalVM environment to create and initialize a new independent VM instance, using the
sxnc_environment *environ; create_graalvm_isolate(environi);create_graalvm_isolate()
function. This is required by the SaxonC processors. -
Use the
int checkProc = c_createSaxonProcessor(environ, processor, 0);c_createSaxonProcessor()
function to create a SaxonC processor, which is used for XSLT and XQuery processing. The last argument is used to specify whether the processor is to be run with license enabled (i.e. for Saxon-EE and Saxon-PE) or off - set using values '1' and '0' respectively. -
You can now execute XSLT stylesheets, using the
const char *result = xsltApplyStylesheet(environ, processor, cwd, "../data/sample.xml", "../data/test.xsl", 0, 0, 0, 0);]xsltSaveResultToFile()
orxsltApplyStylesheet()
functions. For example, you could execute a stylesheet and return the results as a string representation, using:Notice here we have not supplied any parameters or properties.
-
At the end of the program it is necessary to deallocate the memory created for the Saxon processor, parameters and properties; and cleanly release resources created by GraalVM.
graal_tear_down(environi->thread); freeSaxonc(&environi, &processor, ¶meters, &properties);