XSLT3 Options for .NET Core in 2019
Asked Answered
O

3

5

Has anyone got XSLT3 transforms working in .NET Core 2.x+ in 2019?

Seems that the request to MS for XSLT2/3 support hasn't moved forwards, and the Saxon people have other priorities, especially given the IKVM closedown.

Are there any other alternatives for in-process XSLT transformation? At the moment, it seems my only choice is to wrap something up via an external service or some undesirable (for us) COM-style approach that would involve lots of marshalling of data, hurting performance.

Orle answered 21/5, 2019 at 7:39 Comment(0)
D
4

Unfortunately IKVM has never supported .NET Core, so the .NET version of Saxon cannot be made to work in that environment. In Saxonica we've been exploring alternative avenues for .NET support, but we haven't found anything remotely promising. (Anyone fancy doing a Kotlin implementation for .NET?)

I don't know what's possible using XMLPrime or Exselt, both of which target .NET.

2021 Update

Saxonica now ships SaxonCS on .NET 5, this product is built by converting the Java code of SaxonJ to C# source code using a custom transpiler.

Doxia answered 21/5, 2019 at 10:32 Comment(0)
C
2

There is one way how to use Saxon on .NET Core: via Transform.exe running as a process.

You can use code similar to this:

/// <summary>Transform XML inputFile using xsltFile and parameters. Save the result to the outputFile.</summary>

public void Transform(string inputFile, string outputFile, string xsltFile, NameValueCollection parameters)
{
//Search for the instalation path on the system
string path = GetInstalPath(@"Software\Saxonica\SaxonHE-N\Settings", "InstallPath");
string exePath = Path.Combine(path, "bin", "Transform.exe");

string parametersCmd = null;

//Set indicidual parameters
foreach (string parameter in parameters)
{
    parametersCmd += String.Format("{0}={1} ", parameter, parameters[parameter]);
}

//set arguments for Transform.exe
string arguments = string.Format("-s:\"{1}\" -xsl:\"{0}\" -o:\"{3}\" {2}", xsltFile, inputFile, parametersCmd, outputFile);

//https://mcmap.net/q/173324/-hide-console-window-from-process-start-c
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = exePath;
startInfo.Arguments = arguments;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;

int waitingTime = 5 * 60 * 1000; //5 minutes; time in milliseconds

Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;

try
{
    processTemp.Start();
    processTemp.WaitForExit(waitingTime);
}
catch (Exception e)
{
    throw;
}

}

static string GetInstalPath(string comName, string key)
{
RegistryKey comKey = Registry.CurrentUser.OpenSubKey(comName);
if (comKey == null)
    return null;
string clsid = (string)comKey.GetValue(key);
return clsid;
}
Command answered 1/10, 2019 at 19:28 Comment(0)
P
2

SaxonCS EE has been released and works with .NET 5 and .NET 6 (RC/preview) and that way allows using XSLT 3, XPath 3.1 and XQuery 3.1 with .NET Core. It is only available under a commercial license however, but you can test it with a trial license, download from Saxonica is at https://www.saxonica.com/download/dotnet.xml, also on NuGet as https://www.nuget.org/packages/SaxonCS/.

In the meantime IKVM has been updated (https://www.nuget.org/packages/IKVM.Maven.Sdk) and is capable of producing .NET 3.1, .NET 5 and .NET 6 (aka .NET core) compatible cross-compilations. Using that I have managed to cross-compile Saxon HE 11.4 Java to .NET 6 and have published two command line apps/dotnet tools on NuGet to run XSLT 3.0 or XQuery 3.1:

I have furthermore created an extension library to ease the use of the Java s9api from .NET code, it is on NuGet at https://www.nuget.org/packages/SaxonHE11s9apiExtensions/, the GitHub repository is at https://github.com/martin-honnen/SaxonHE11s9apiExtensions.

A simple example to run some XSLT 3.0 code with .NET 6, using the IKVM cross compiled Saxon HE 11, would be:

using net.sf.saxon.s9api;
using net.liberty_development.SaxonHE11s9apiExtensions;
//using System.Reflection;

// force loading of updated xmlresolver (no longer necessary with Saxon HE 11.5)
//ikvm.runtime.Startup.addBootClassPathAssembly(Assembly.Load("org.xmlresolver.xmlresolver"));
//ikvm.runtime.Startup.addBootClassPathAssembly(Assembly.Load("org.xmlresolver.xmlresolver_data"));

var processor = new Processor(false);

Console.WriteLine($"{processor.getSaxonEdition()} {processor.getSaxonProductVersion()}");

var xslt30Transformer = processor.newXsltCompiler().Compile(new Uri("https://github.com/martin-honnen/martin-honnen.github.io/raw/master/xslt/processorTestHTML5Xslt3InitialTempl.xsl")).load30();

xslt30Transformer.callTemplate(null, processor.NewSerializer(Console.Out));

A samples project showing various examples of XPath 3.1, XQuery 3.1 and XSLT 3.0 usage is at https://github.com/martin-honnen/SaxonHE11IKVMNet6SaxonCSSamplesAdapted.

Saxon 12.1 HE Java is now compatible with Java 8 and that way with IKVM, thus it, like Saxon HE 11 or 10, allows cross-compilation to .NET Core, current versions of .NET are 6 and 7. The easiest way to use IKVM to cross-compile Saxon HE Java to .NET is to use IKVM.Maven.Sdk and to ease the use of Saxon 12.1 I have developed some extension functions packaged as https://www.nuget.org/packages/SaxonHE12s9apiExtensions/. Using that you can make use of XSLT 3.0 and/or XQuery 3.1 and/or XPath 3.1 in your .NET 6 or later code with a few lines e.g.

using net.sf.saxon.s9api;
using net.liberty_development.SaxonHE12s9apiExtensions;

Console.WriteLine($"{Environment.Version} {Environment.OSVersion}");

var processor = new Processor(false);

Console.WriteLine($"{processor.getSaxonEdition()} {processor.getSaxonProductVersion()}");

var xslt30Transformer = processor.newXsltCompiler().Compile(new Uri("https://github.com/martin-honnen/martin-honnen.github.io/raw/master/xslt/processorTestHTML5Xslt3InitialTempl.xsl")).load30();

xslt30Transformer.callTemplate(null, processor.NewSerializer(Console.Out));

A larger sample project showing various XSLT and XQuery and XPath uses is at https://github.com/martin-honnen/SaxonHE12IKVMNet6SaxonCSSamplesAdapted.

Puckett answered 31/10, 2021 at 8:28 Comment(1)
Using SaxonHE12s9apiExtensions for a small hobby project of mine in .NET 7, so far so good I had a lot issues with the default MS implementation The extension library is available trough nugetKashmir

© 2022 - 2024 — McMap. All rights reserved.