/// <summary> /// Transforms an XML using the XSL. /// </summary> /// <param name="xml">The XML to transform.</param> /// <param name="xsl">The XSL to use to transform.</param> /// <returns></returns> public static Tuple <string, XslTimings> Transform(string xsl, string xml, XslOptions options) { Contract.Requires(xsl != null); Contract.Requires(xml != null); Contract.Requires(options != null); using (TextReader xmlReader = new StringReader(xml)) { using (TextReader xslReader = new StringReader(xsl)) { using (Stream output = new MemoryStream()) { XslTimings timing = Transform(xslReader, xmlReader, output, options); output.Position = 0; using (StreamReader sr = new StreamReader(output, options.OutputEncoding)) { StringBuilder sb = new StringBuilder(); using (StringWriterWithEncoding xmlWriter = new StringWriterWithEncoding(sb, options.OutputEncoding)) { xmlWriter.Write(sr.ReadToEnd()); return(new Tuple <string, XslTimings>(xmlWriter.ToString(), timing)); } } } } } }
/// <summary> /// Transforms an XML using the XSL. /// </summary> /// <param name="xml">The XML to transform, without BOM.</param> /// <param name="xsl">The XSL to use to transform, without BOM.</param> /// <returns></returns> public static XslTimings Transform(TextReader xsl, TextReader xml, Stream output, XslOptions options) { Contract.Requires(xsl != null); Contract.Requires(xml != null); Contract.Requires(output != null); Contract.Requires(options != null); XslTimings timings = new XslTimings(); Stopwatch timer; XsltArgumentList xslArgs = options.GetArgumentList(); XslProcedures myProcs = new XslProcedures(); xslArgs.AddExtensionObject("urn:XslProcs", myProcs); XmlReaderSettings xmlSettings = new XmlReaderSettings(); xmlSettings.CloseInput = false; xmlSettings.IgnoreWhitespace = options.RemoveWhitespace; xmlSettings.DtdProcessing = DtdProcessing.Parse; if (!options.ExternalDefinitions) { xmlSettings.XmlResolver = null; } XmlReaderSettings xslSettings = new XmlReaderSettings(); xslSettings.CloseInput = false; // Create the XmlReader object. using (XmlReader xmlReader = XmlReader.Create(xml, xmlSettings)) { XslCompiledTransform trans = new XslCompiledTransform(false); using (XmlReader xslReader = CreateXmlReaderWithMode(xsl, xslSettings, options.StartMode)) { try { timer = Stopwatch.StartNew(); trans.Load(xslReader); timer.Stop(); timings.Compile = timer.Elapsed; } catch (XsltException) { throw; } } XmlWriterSettings xmlWriteSettings = trans.OutputSettings.Clone(); if (options.OutputEncoding != null) { // override the output encoding in the XSL if specified xmlWriteSettings.Encoding = options.OutputEncoding; } else if (xmlWriteSettings.Encoding == XslOptions.DefaultEncoding) { // If UTF8 set it to have no BOM by default xmlWriteSettings.Encoding = new UTF8Encoding(false); } if (options.RemoveWhitespace) { // xmlWriteSettings.NewLineChars = string.Empty; xmlWriteSettings.NewLineHandling = NewLineHandling.Replace; xmlWriteSettings.NewLineOnAttributes = false; xmlWriteSettings.IndentChars = string.Empty; xmlWriteSettings.Indent = true; } using (XmlWriter xmlWriter = XmlWriter.Create(output, xmlWriteSettings)) { timer.Restart(); trans.Transform(xmlReader, xslArgs, xmlWriter); timer.Stop(); timings.Execute = timer.Elapsed; } } return(timings); }