Skip to content

Instantly share code, notes, and snippets.

@giansalex
Created November 19, 2017 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giansalex/32d3c5105a76728fff1ab8601c8603d6 to your computer and use it in GitHub Desktop.
Save giansalex/32d3c5105a76728fff1ab8601c8603d6 to your computer and use it in GitHub Desktop.
Saxon c# xslt2 transform
using Saxon.Api;

var xslt = new FileInfo(@"C:\path\to\stylesheet.xslt");
var input = new FileInfo(@"C:\path\to\data.xml");
var output = new FileInfo(@"C:\path\to\result.xml");

// Compile stylesheet
var processor = new Processor();
var compiler = processor.NewXsltCompiler();
var executable = compiler.Compile(new Uri(xslt.FullName));

// Do transformation to a destination
var destination = new DomDestination();
using(var inputStream = input.OpenRead())
{
    var transformer = executable.Load();
    transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
    transformer.Run(destination);
}

// Save result to a file (or whatever else you wanna do)
destination.XmlDocument.Save(output.FullName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment