Home > Enterprise >  Saxon .NET with C CLI
Saxon .NET with C CLI

Time:03-10

I had an idea to use C CLI to interact with the Saxon .NET interface . The problem is that every single example on Saxonica is with C# , and not C . can you give me an example that caches an XML file , and using Xslt filepaths to transform it using C CLI to use the .NET interface ???

Also pls dont give me workarounds that dont use C CLI

CodePudding user response:

A minimal sample using Saxon .NET HE 10 (tested with 10.6 initially, now updated to 10.7) run on Windows against .NET framework 4.8 is e.g.

#include "pch.h"

using namespace System;
using namespace Saxon::Api;

int main(array<System::String ^> ^args)
{
    Processor^ processor = gcnew Processor();

    Console::WriteLine(processor->ProductVersion);

    DocumentBuilder^ docBuilder = processor->NewDocumentBuilder();

    Uri^ baseUri = gcnew Uri(System::Environment::CurrentDirectory   "\\");

    XdmNode^ inputDoc = docBuilder->Build(gcnew Uri(baseUri, "input-sample1.xml"));

    XsltCompiler^ xsltCompiler = processor->NewXsltCompiler();

    xsltCompiler->BaseUri = baseUri;

    XsltExecutable^ xsltExecutable = xsltCompiler->Compile(gcnew Uri(baseUri, "sheet1.xsl"));

    Xslt30Transformer^ xslt30Transformer = xsltExecutable->Load30();

    xslt30Transformer->ApplyTemplates(inputDoc, processor->NewSerializer(Console::Out));

    return 0;
}

Example project at https://github.com/martin-honnen/SaxonHECLIExample1.

To write to a file instead, use e.g.

FileStream^ resultStream = File::OpenWrite("result1.xml");

xslt30Transformer->ApplyTemplates(inputDoc, processor->NewSerializer(resultStream));

resultStream->Close();

instead of xslt30Transformer->ApplyTemplates(inputDoc, processor->NewSerializer(Console::Out));; example adaption is at https://github.com/martin-honnen/SaxonHECLIExample1/tree/WriteToFileInsteadOfConsole

  • Related