Saxon.NET example

I was playing around with Saxon.NET and I like it.  Its also very well done (not suprisingly).  Benchmarking, I found that its competitive with my custom processor for XSLT 1.0 while providing 2.0 support.  I’ll be integrating this into MarcEdit, likely even this weekend, so folks can start using XSLT 2.0 in the application.  I just need to figure out exactly how I’ll implement this engine — partly because my custom processor still outperforms when using XSLT 1.0.  So I’ll likely mix the two processors — but allow users to define which that they want to use. 

Anyway, here’s a quick sample of how you utilize the XSLT processor in C#.

// Create a Processor instance.

Processor processor = new Processor();System.IO.StreamReader reader = new System.IO.StreamReader(@”c:\OREboyscouts.xml”, System.Text.Encoding.UTF8);System.IO.TextWriter stringWriter =

new System.IO.StringWriter();stringWriter.Write(reader.ReadToEnd());

stringWriter.Close();

reader.Close();

System.IO.TextReader stringReader = new System.IO.StringReader(stringWriter.ToString());System.Xml.XmlTextReader reader2 = new System.Xml.XmlTextReader(stringReader);reader2.XmlResolver =

null;// Load the source document

XdmNode input = processor.NewDocumentBuilder().Build(reader2);

 

// Create a transformer for the stylesheet.

XsltTransformer transformer = processor.NewXsltCompiler().Compile(new System.Uri(@”c:\xslt\EADlitetoMARC21slimXML.xsl”)).Load();transformer.InputXmlResolver = null; 

// Set the root node of the source document to be the initial context node

transformer.InitialContextNode = input;

// Create a serializer

Serializer serializer = new Serializer();//serializer.SetOutputWriter(Console.Out);

serializer.SetOutputFile(@”c:\xmlstream.xml”);

// Transform the source XML to System.out.

transformer.Run(serializer);

Console.ReadLine();


Posted

in

,

by

Tags: