public static void Main(string[] args) { //GDNDynamicTests t = new GDNDynamicTests(); //t.EvaluateTest(); if (args.Length != 3) { PrintUsage(); return; } try { XPathDocument doc = new XPathDocument(args[0]); ExsltTransform xslt = new ExsltTransform(); xslt.Load(args[1]); xslt.MultiOutput = false; using (FileStream fs = File.Create(args[2])) { xslt.Transform(doc, null, fs); } } catch (Exception e) { Console.Error.WriteLine("An exception occured: "); Console.Error.WriteLine(e); } }
public ExsltTransform PrepareTransformationObject(String rootPath, string xsltText) { ExsltTransform xslt = new ExsltTransform(); MemoryStream ms = new MemoryStream((new UTF8Encoding(true)).GetBytes(xsltText)); try { XmlTextReader xmltr = new XmlTextReader(ms); xmltr.WhitespaceHandling = WhitespaceHandling.Significant; xslt.Load(xmltr, new MyResolver(rootPath, this)); } finally { ms.Close(); } return(xslt); }
protected void RunAndCompare(string source, string stylesheet, string result) { XPathDocument doc = new XPathDocument(TestDir + source); ExsltTransform exslt = new ExsltTransform(); exslt.Load(TestDir + stylesheet); StringWriter res = new StringWriter(); exslt.Transform(doc, null, res); StreamReader sr = new StreamReader(ResultsDir + result); string expectedResult = sr.ReadToEnd(); sr.Close(); if (res.ToString() != expectedResult) { Console.WriteLine("Actual Result was {0}", res.ToString()); Console.WriteLine("Expected Result was {0}", expectedResult); } Assert.IsTrue(res.ToString() == expectedResult); }
private string ProcessXslt(String rootPath) { var xslt = this.Xslt; var xml = this.Xml; if (String.IsNullOrEmpty(this.Xslt)) { throw new Exception("Must load xslt before calling Execute()"); } else if (String.IsNullOrEmpty(this.Xml)) { throw new Exception("No xml loaded for xslt to process"); } else { String fileSet = String.Empty; Environment.CurrentDirectory = rootPath; ExsltTransform t = PrepareTransformationObject(rootPath, this.Xslt); XsltArgumentList al = new XsltArgumentList(); al.AddParam("dish-root", String.Empty, rootPath); al.AddParam("codee-root", String.Empty, rootPath); al.AddParam("xml-root", String.Empty, rootPath); al.AddParam("xslt-root", String.Empty, rootPath); foreach (var dict in this.Parameters) { al.AddParam(dict.Key, String.Empty, dict.Value); } String inputXml = this.Xml; String newFileContents = String.Empty; XmlDocument doc = new XmlDocument(); inputXml = inputXml.Trim(); doc.LoadXml(inputXml); MemoryStream ms = new MemoryStream(); try { t.Transform(doc.CreateNavigator(), al, ms); ms.Position = 0; String currentDoc = (new UTF8Encoding(true)).GetString(ms.GetBuffer(), 0, (int)ms.Length); if (currentDoc.StartsWith("<?xml")) { currentDoc = currentDoc.Substring(currentDoc.IndexOf("?>") + 2); } currentDoc = currentDoc.Trim((char)65279); if (currentDoc.Contains("<")) { currentDoc = currentDoc.Substring(currentDoc.IndexOf("<")); } if (!String.IsNullOrEmpty(currentDoc)) { try { doc = new XmlDocument(); doc.LoadXml(currentDoc); MemoryStream wms = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(wms, (new UTF8Encoding(true))); writer.Formatting = Formatting.Indented; doc.WriteContentTo(writer); writer.Flush(); newFileContents = (new UTF8Encoding(true)).GetString(wms.GetBuffer(), 0, (int)writer.BaseStream.Length).Trim(); wms.Close(); } catch (Exception ex) { throw ex; // Shoudl really call kitchen service below //KitchenService.ReportError(ex); // Ignore formatting errors newFileContents = currentDoc; } } } finally { ms.Close(); } return(newFileContents); /* * String outputFileName = String.Format("{0}{1}", this.FileName, ".Output.xml"); * * newFileContents = newFileContents.Replace(Environment.NewLine, "\n").Replace("\n", Environment.NewLine).Trim(); * * this.PreviousFileSetZipped = newFileContents.Zip(); * * this.Save(); * * //File.WriteAllText(outputFileName, newFileContents); * Dish.SplitContents(outputFileName, newFileContents, false, this.FileName); * * * * return fileSet; */ } }
public FileSet ProcessXslt() { var xslt = this.Xslt; var xml = this.Xml; var rootPath = this.RootDirInfo.FullName; if (String.IsNullOrEmpty(this.Xslt)) { throw new Exception("Must load xslt before calling Execute()"); } else if (String.IsNullOrEmpty(this.Xml)) { throw new Exception("No xml loaded for xslt to process"); } else { Environment.CurrentDirectory = rootPath; ExsltTransform t = PrepareTransformationObject(rootPath, this.Xslt); XsltArgumentList al = new XsltArgumentList(); al.AddParam("dish-root", String.Empty, rootPath); al.AddParam("codee-root", String.Empty, rootPath); al.AddParam("xml-root", String.Empty, rootPath); al.AddParam("xslt-root", String.Empty, rootPath); foreach (var dict in this.Parameters) { al.AddParam(dict.Key, String.Empty, dict.Value.SafeToString()); } String inputXml = this.Xml; String newFileContents = String.Empty; XmlDocument doc = new XmlDocument(); inputXml = inputXml.Trim(); doc.LoadXml(inputXml); MemoryStream ms = new MemoryStream(); try { t.Transform(doc.CreateNavigator(), al, ms); ms.Position = 0; String currentDoc = (new UTF8Encoding(false)).GetString(ms.GetBuffer(), 0, (int)ms.Length); if (currentDoc.StartsWith("<?xml")) { currentDoc = currentDoc.Substring(currentDoc.IndexOf("?>") + 2); } currentDoc = currentDoc.Trim((char)65279); if (currentDoc.Contains("<")) { currentDoc = currentDoc.Substring(currentDoc.IndexOf("<")); } if (!String.IsNullOrEmpty(currentDoc)) { try { doc = new XmlDocument(); doc.LoadXml(currentDoc); MemoryStream wms = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(wms, (new UTF8Encoding(false))); writer.Formatting = Formatting.Indented; doc.WriteContentTo(writer); writer.Flush(); newFileContents = (new UTF8Encoding(false)).GetString(wms.GetBuffer(), 0, (int)writer.BaseStream.Length).Trim(); wms.Close(); } catch (Exception ex) { throw ex; } } } finally { ms.Close(); } var fileSet = newFileContents.ToFileSet(); this.AddFileSetToOutput(fileSet); this.Xslt = String.Empty; this.Xml = String.Empty; return(fileSet); } }