/** * Show the that a transformer can be reused, and show resetting * a parameter on the transformer. */ public static void ExampleTransformerReuse(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // Compile the stylesheet XsltExecutable exec = processor.NewXsltCompiler().Compile(new Uri(xsltUri)); // Create a transformer XsltTransformer transformer = exec.Load(); // Run it once transformer.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("hello to you!")); transformer.InitialContextNode = input; XdmDestination results = new XdmDestination(); transformer.Run(results); Console.WriteLine("1: " + results.XdmNode.StringValue); // Run it again transformer.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("hello to me!")); transformer.InitialContextNode = input; results.Reset(); transformer.Run(results); Console.WriteLine("2: " + results.XdmNode.StringValue); }
///<summary> ///Performs a Saxon transformation. /// </summary> /// public override Stream Transform(Stream source, Stream xsl) { MemoryStream result = new MemoryStream(); Uri uri = new Uri("file://C:/"); //set XSLT stylesheet Processor p = new Processor(); XsltCompiler c = p.NewXsltCompiler(); c.BaseUri = uri; XsltExecutable e = c.Compile(xsl); XsltTransformer t = e.Load(); //set output var destination = new Serializer(); destination.SetOutputStream(result); t.SetInputStream(source, uri); t.Run(destination); result.Position = 0; result.Flush(); return(result); }
private bool compareXML(String actual, String gold) { try { if (xmlComparer == null) { xmlComparer = processor.NewXsltCompiler().Compile(new Uri(testSuiteDir + "/SaxonResults.net/compare.xsl")); } XdmNode doc1 = processor.NewDocumentBuilder().Build(new Uri(actual)); XdmNode doc2 = processor.NewDocumentBuilder().Build(new Uri(gold)); XsltTransformer t = xmlComparer.Load(); t.InitialTemplate = new QName("", "compare"); t.SetParameter(new QName("", "actual"), doc1); t.SetParameter(new QName("", "gold"), doc2); StringWriter sw = new StringWriter(); Serializer sr = new Serializer(); sr.SetOutputWriter(sw); t.Run(sr); String result = sw.ToString(); return(result.StartsWith("true")); } catch (Exception e) { Console.WriteLine("***" + e.Message); return(false); } }
private String compareDocs(XdmNode doc1, XdmNode doc2) { try { XsltTransformer t = xmlComparer.Load(); t.InitialTemplate = new QName("", "compare"); t.SetParameter(new QName("", "actual"), doc1); t.SetParameter(new QName("", "gold"), doc2); t.SetParameter(new QName("", "debug"), new XdmAtomicValue(debug)); StringWriter sw = new StringWriter(); Serializer sr = new Serializer(); sr.SetOutputWriter(sw); t.Run(sr); String result = sw.ToString(); if (result.StartsWith("true")) { return("OK"); } else { return("XML comparison - not equal"); } } catch (Exception e) { Console.WriteLine("***" + e.Message); return("XML comparison failure: " + e.Message); } }
private string Transform(string stylesheetContent, string xmlPath, bool addErrorsPhase = false) { FileInfo xmlInfo = new FileInfo(xmlPath); XdmNode stylesheet = builder.Build(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(stylesheetContent)))); XsltCompiler compiler = processor.NewXsltCompiler(); XsltExecutable exec = compiler.Compile(stylesheet); XsltTransformer transformer = exec.Load(); DomDestination dest = new DomDestination(); using (var inputStream = xmlInfo.OpenRead()) { if (addErrorsPhase) { transformer.SetParameter(new QName("phase"), XdmValue.MakeValue("errors")); } transformer.SetInputStream(inputStream, new Uri(xmlInfo.DirectoryName)); transformer.Run(dest); } using (StringWriter sw = new StringWriter()) { if (dest.XmlDocument == null) { throw new Exception("Failed to execute Schematron validation. The SCH file selected may not be the ISO version of Schematron."); } dest.XmlDocument.Save(sw); return(sw.ToString()); } }
private string ProcessXslt(string result, string xslt) { XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(result)); string cleanXmlDocument = xmlDocumentWithoutNs.ToString(); XsltExecutable xsltExecutable = GetXsltExecutable(xslt); byte[] xmlByteArray = System.Text.Encoding.UTF8.GetBytes(cleanXmlDocument); var inputStream = new MemoryStream(xmlByteArray); XsltTransformer transformer = xsltExecutable.Load(); // Saxon requires to set an Uri for the stream; otherwise setting the input stream fails transformer.SetInputStream(inputStream, new Uri("http://localhost")); // run the transformation and save the result to string Serializer serializer = new Processor().NewSerializer(); using (var memoryStream = new MemoryStream()) { serializer.SetOutputStream(memoryStream); transformer.Run(serializer); memoryStream.Position = 0; using (var streamReader = new StreamReader(memoryStream)) { result = streamReader.ReadToEnd(); } } return(result.Contains(NoMatchString) ? null : result); }
public static string TransformSAXON(string documentPath, string xsltPath, bool schemaAware) { Processor processor = new Processor(); XsltCompiler xsltCompiler = processor.NewXsltCompiler(); xsltCompiler.Processor.SetProperty(FeatureKeys.GENERATE_BYTE_CODE, "false"); XsltExecutable xsltExecutable = xsltCompiler.Compile(new Uri("file://" + xsltPath)); XsltTransformer xsltTransformer = xsltExecutable.Load(); if (schemaAware //!string.IsNullOrEmpty(xsdPath) ) { xsltTransformer.SchemaValidationMode = SchemaValidationMode.Strict; } using (FileStream fileStream = File.OpenRead(documentPath)) { xsltTransformer.SetInputStream(fileStream, new Uri(@"file://" + documentPath)); XdmDestination destination = new XdmDestination(); xsltTransformer.Run(destination); StringBuilder outputBuilder = new StringBuilder(); outputBuilder.Append(destination.XdmNode.OuterXml); return(outputBuilder.ToString()); } }
/** * Example demonstrating call of an extension function */ public static void ExampleExtensibility(String sourceUri) { // Create a Processor instance. Processor processor = new Processor(); // Identify the Processor version Console.WriteLine(processor.ProductVersion); // Set diagnostics processor.SetProperty("http://saxon.sf.net/feature/trace-external-functions", true); // Create the stylesheet String s = @"<out xsl:version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + @" xmlns:ext='clitype:SampleExtensions.SampleExtensions?asm=SampleExtensions' " + @" addition='{ext:add(2,2)}' " + @" average='{ext:average((1,2,3,4,5,6))}'/>"; // Create a transformer for the stylesheet. XsltTransformer transformer = processor.NewXsltCompiler().Compile(new StringReader(s)).Load(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // 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); // Transform the source XML to System.out. transformer.Run(serializer); }
private Stream Transform(XsltTransformer xslt, Stream src, Uri baseuri) { MemoryStream dst = null; DomDestination dom = new DomDestination(); try { xslt.SetInputStream(src, baseuri); xslt.Run(dom); dst = new MemoryStream(); dom.XmlDocument.Save(dst); dst.Flush(); dst.Position = 0; } catch (Exception ex) { throw ex; } finally { dom.Close(); } return(dst); }
/** * Show how to transform a Saxon tree into another Saxon tree. */ public static void ExampleSaxonToSaxon(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // Create a compiler XsltCompiler compiler = processor.NewXsltCompiler(); // Compile the stylesheet XsltTransformer transformer = compiler.Compile(new Uri(xsltUri)).Load(); // Run the transformation transformer.InitialContextNode = input; XdmDestination result = new XdmDestination(); transformer.Run(result); // Serialize the result so we can see that it worked StringWriter sw = new StringWriter(); result.XdmNode.WriteTo(new XmlTextWriter(sw)); Console.WriteLine(sw.ToString()); // Note: we don't do // result.XdmNode.WriteTo(new XmlTextWriter(Console.Out)); // because that results in the Console.out stream being closed, // with subsequent attempts to write to it being rejected. }
/** * Show a transformation using a DTD-based validation and the id() function. */ public static void ExampleUsingDTD(String sourceUri, String xsltUri) { // Create a Processor instance Processor processor = new Processor(); // Load the source document DocumentBuilder db = processor.NewDocumentBuilder(); db.DtdValidation = true; XdmNode input = db.Build(new Uri(sourceUri)); //Create a transformer for the stylesheet XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltUri)).Load(); // Set the root node of the source document to be the initial context node transformer.InitialContextNode = input; //Set the destination XdmDestination results = new XdmDestination(); // Create a serializer //Serializer results = new Serializer(); //results.SetOutputWriter(Console.Out); // Transform the XML transformer.Run(results); Console.WriteLine(results.XdmNode.ToString()); }
public static void SimpleTransformation(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // Create a transformer for the stylesheet. XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltUri)).Load(); // Set the root node of the source document to be the initial context node transformer.InitialContextNode = input; // Set a parameter to the transformation transformer.SetParameter(new QName("", "", "greeting"), new XdmAtomicValue("hello")); // Create a serializer Serializer serializer = new Serializer(); serializer.SetOutputWriter(Console.Out); serializer.SetOutputProperty(Serializer.INDENT, "yes"); // Transform the source XML to Console.Out transformer.Run(serializer); }
public void TestSaxonFromFileToStream()//string sourceUri, string xsltUri) { //SaxonController saxon = new SaxonController(); //saxon.XmlToStream(@"D:\test.xml", @"D:\test.xsl"); //return View(); string sourceUri = @"D:\test.xml"; string xsltUri = @"D:\test.xsl"; //Processor属于Saxon.Api Processor processor = new Processor(); //加载源文档 XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); //为StyleSheet创建一个新的转换器 XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltUri)).Load(); transformer.InitialContextNode = input; transformer.BaseOutputUri = new Uri(xsltUri); // transformer.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("hello to you!")); // transformer.SetParameter(new QName("", "", "b-param"), new XdmAtomicValue(someVariable)); // Create a serializer. Serializer serializer = new Serializer(); serializer.SetOutputWriter(Response.Output); //for screen // serializer.SetOutputFile(Server.MapPath("test.html")); //for file // Transform the source XML to System.out. transformer.Run(serializer); }
public XmlDocument Validation(string xmlfile) { XmlDocument doc = null; DomDestination dom = new DomDestination(); Stream src = null; try { if (_schematron == null) { throw new Exception("incompleted schema stylesheet"); } FileInfo file = new FileInfo(xmlfile); Uri baseuri = new Uri(file.DirectoryName + "/"); src = file.OpenRead(); _schematron.SetInputStream(src, baseuri); _schematron.Run(dom); doc = dom.XmlDocument; } catch (Exception ex) { throw ex; } finally { dom.Close(); src?.Close(); src?.Dispose(); } return(doc); }
public override bool TransformXml(string SourceXmlPath, string OutputPath) { var input = new FileInfo(SourceXmlPath); var output = new FileInfo(OutputPath); DomDestination destination = new DomDestination(); try { // Do transformation to a destination using (var inputStream = input.OpenRead()) { CompiledXform.SetInputStream(inputStream, new Uri(input.DirectoryName)); CompiledXform.Run(destination); } } catch (Exception Ex) { Console.WriteLine(Ex.ToString() + "\n" + Ex.StackTrace); return(false); } // Save result to file destination.XmlDocument.Save(output.FullName); return(true); }
/** * Show how to transform a tree starting at a node other than the root. */ public static void ExampleSaxonToSaxonNonRoot(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // Navigate to the first grandchild XPathSelector eval = processor.NewXPathCompiler().Compile("/*/*[1]").Load(); eval.ContextItem = input; input = (XdmNode)eval.EvaluateSingle(); // Create an XSLT compiler XsltCompiler compiler = processor.NewXsltCompiler(); // Compile the stylesheet XsltTransformer transformer = compiler.Compile(new Uri(xsltUri)).Load(); // Run the transformation transformer.InitialContextNode = input; XdmDestination result = new XdmDestination(); transformer.Run(result); // Serialize the result so we can see that it worked Console.WriteLine(result.XdmNode.OuterXml); }
public override void TransformFileToFile(XMLUtilities.XSLParameter[] parameterList, string sInputPath, string sOutputName) { try { // Add any parameters AddParameters(parameterList); // apply transform var inputUri = new Uri(sInputPath); var sr = new StreamReader(sInputPath, Encoding.UTF8); XdmNode inputNode = m_processor.NewDocumentBuilder().Build(inputUri); sr.Close(); m_transformer.InitialContextNode = inputNode; var ser = new Serializer(); ser.SetOutputFile(sOutputName); m_transformer.Run(ser); ser.Close(); } catch (Exception e) { MessageBox.Show(e.InnerException.ToString()); } }
/** * Perform a transformation using a compiled stylesheet (a Templates object) */ public static void ExampleUseTemplatesObj( String sourceUri1, String sourceUri2, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Create a compiled stylesheet XsltExecutable templates = processor.NewXsltCompiler().Compile(new Uri(xsltUri)); // Note: we could actually use the same XSltTransformer in this case. // But in principle, the two transformations could be done in parallel in separate threads. // Do the first transformation Console.WriteLine("\n\n----- transform of " + sourceUri1 + " -----"); XsltTransformer transformer1 = templates.Load(); transformer1.InitialContextNode = processor.NewDocumentBuilder().Build(new Uri(sourceUri1)); transformer1.Run(new Serializer()); // default destination is Console.Out // Do the second transformation Console.WriteLine("\n\n----- transform of " + sourceUri2 + " -----"); XsltTransformer transformer2 = templates.Load(); transformer2.InitialContextNode = processor.NewDocumentBuilder().Build(new Uri(sourceUri2)); transformer2.Run(new Serializer()); // default destination is Console.Out }
/** * Show how to transform a DOM tree into another DOM tree. * This uses the System.Xml parser to parse an XML file into a * DOM, and create an output DOM. In this Example, Saxon uses a * third-party DOM as both input and output. */ public static void ExampleDOMtoDOM(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document (in practice, it would already exist as a DOM) XmlDocument doc = new XmlDocument(); doc.Load(new XmlTextReader(sourceUri)); XdmNode input = processor.NewDocumentBuilder().Wrap(doc); // Create a compiler XsltCompiler compiler = processor.NewXsltCompiler(); // Compile the stylesheet XsltTransformer transformer = compiler.Compile(new Uri(xsltUri)).Load(); // Run the transformation transformer.InitialContextNode = input; DomDestination result = new DomDestination(); transformer.Run(result); // Serialize the result so we can see that it worked Console.WriteLine(result.XmlDocument.OuterXml); }
/** * This shows how to set a parameter for use by the stylesheet. Use * two transformers to show that different parameters may be set * on different transformers. */ public static void ExampleParam(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // Compile the stylesheet XsltExecutable exec = processor.NewXsltCompiler().Compile(new Uri(xsltUri)); // Create two transformers with different parameters XsltTransformer transformer1 = exec.Load(); XsltTransformer transformer2 = exec.Load(); transformer1.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("hello to you!")); transformer2.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("goodbye to you!")); // Now run them both transformer1.InitialContextNode = input; XdmDestination results1 = new XdmDestination(); transformer1.Run(results1); transformer2.InitialContextNode = input; XdmDestination results2 = new XdmDestination(); transformer2.Run(results2); Console.WriteLine("1: " + results1.XdmNode.StringValue); Console.WriteLine("2: " + results2.XdmNode.StringValue); }
/** * Show a transformation using a user-written URI Resolver. */ public static void ExampleUsingURIResolver(String sourceUri, String xsltUri) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); // Create a transformer for the stylesheet. XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltUri)).Load(); // Set the root node of the source document to be the initial context node transformer.InitialContextNode = input; // Set the user-written XmlResolver transformer.InputXmlResolver = new UserXmlResolver(); // Create a serializer Serializer serializer = new Serializer(); serializer.SetOutputWriter(Console.Out); // Transform the source XML to System.out. transformer.Run(serializer); }
public void Transform(Stream inStream, Stream outStream) { //_logger.LogTrace($"Running transformation"); var dest = new Serializer(); dest.SetOutputStream(outStream); _transformer.SetInputStream(inStream, new Uri("http://no.where.to.go/")); _transformer.Run(dest); }
/// <summary> /// A generic XSL Transformation [v2.0] Class for use in ASP.NET pages /// </summary> public static string TransformXml2(string xmlPath, string xsltPath, Hashtable xsltParams, string output) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); try { // Create a Processor instance. Processor processor = new Processor(); // Load the source document. XdmNode input = processor.NewDocumentBuilder().Build(new Uri(xmlPath)); // Create a transformer for the stylesheet. XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltPath)).Load(); // Set the root node of the source document to be the initial context node. transformer.InitialContextNode = input; // BaseOutputUri is only necessary for xsl:result-document (might be able to use xsl:result-document for alternative file output) transformer.BaseOutputUri = new Uri(xsltPath); //Fill XsltArgumentList if necessary - taken from key/value pairs in the hashtable if (xsltParams != null) { IDictionaryEnumerator pEnumerator = xsltParams.GetEnumerator(); while (pEnumerator.MoveNext()) { transformer.SetParameter(new QName("", "", pEnumerator.Key.ToString()), new XdmAtomicValue(pEnumerator.Value.ToString())); } } // Create a serializer and run it over the result of the transform. Serializer serializer = new Serializer(); serializer.SetOutputWriter(sw); transformer.Run(serializer); // Output method (though keep in mind this can't be written to a file if using ASP.net controls because it is not yet processed) if (output == "file") { TextWriter w = new StreamWriter(@"c:\inetpub\wwwroot\output.html"); w.Write(sb.ToString()); w.Close(); } return sb.ToString(); } catch (Exception exp) { return exp.ToString(); } finally { sw.Close(); } }
private void RunSchematron() { Uri baseuri = new Uri(@"D:\VisualStudio Projects\HL7\SchematronTrySaxon.ConsoleApplication\"); Uri baseuri2 = new Uri(@"D:\VisualStudio Projects\HL7\SchematronTry.ConsoleApplication\"); // Create a Processor instance. Processor processor = new Processor(); XsltCompiler compiler = processor.NewXsltCompiler(); DocumentBuilder builder = processor.NewDocumentBuilder(); builder.XmlResolver = new UserXmlResolver(); //builder.BaseUri = baseuri; // Transform the Schematron // Create a transformer for the stylesheet. XsltTransformer transformer = compiler.Compile(new Uri(baseuri, @"iso-schematron-xslt2\iso_svrl_for_xslt2.xsl")).Load(); // Load the source document XdmNode input = builder.Build(new Uri(baseuri, @"Test2.sch")); // XdmNode input = builder.Build(new Uri(baseuri, @"Test3.sch")); // Set the root node of the source document to be the initial context node transformer.InitialContextNode = input; // Create a serializer, with output to the standard output stream Serializer serializer = new Serializer(); serializer.SetOutputFile(@"D:\VisualStudio Projects\HL7\SchematronTrySaxon.ConsoleApplication\Test2.sch.xsl"); // StringWriter schxsl = new StringWriter(); // serializer.SetOutputWriter(schxsl); // Transform the source XML and serialize the result document transformer.Run(serializer); // Execute the Schematron // Create a transformer for the stylesheet. XsltTransformer transformer2 = compiler.Compile(new Uri(@"D:\VisualStudio Projects\HL7\SchematronTrySaxon.ConsoleApplication\Test2.sch.xsl")).Load(); //XsltTransformer transformer2 = compiler.Compile(new StringReader(schxsl.GetStringBuilder().ToString())).Load(); // Load the source document // XdmNode input2 = builder.Build(new Uri(baseuri2, @"fm-max.xml")); XdmNode input2 = builder.Build(new Uri(baseuri, @"XMLFile1.xml")); // Set the root node of the source document to be the initial context node transformer2.InitialContextNode = input; // Create a serializer, with output to the standard output stream Serializer serializer2 = new Serializer(); serializer2.SetOutputFile(@"D:\VisualStudio Projects\HL7\SchematronTrySaxon.ConsoleApplication\fm-max.xml-output"); // Transform the source XML and serialize the result document transformer2.Run(serializer2); }
public ActionResult Upload(IEnumerable <HttpPostedFileBase> fileUpload) { foreach (var file in fileUpload) { if (Request.Files.Count > 0) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Files/"), fileName); var extension = Path.GetExtension(file.FileName); //dosya uzantısını aldım switch (extension) // Uzantıya göre texareaya yönlendirdim { case ".xml": ViewBag.mesaj = System.IO.File.ReadAllText(path); localPathXml = path; break; case ".xslt": ViewBag.mesaj2 = System.IO.File.ReadAllText(path); localPathXslt = path; break; } SaveExtension = extension; } } } //Dosyalar upload edildikten sonra Saxon HE ile tranform işlemi yap Processor xsltProcessor = new Processor(); DocumentBuilder documentBuilder = xsltProcessor.NewDocumentBuilder(); documentBuilder.BaseUri = new Uri("file://"); XdmNode xdmNode = documentBuilder.Build(new StringReader(ViewBag.mesaj)); XsltCompiler xsltCompiler = xsltProcessor.NewXsltCompiler(); XsltExecutable xsltExecutable = xsltCompiler.Compile(new StringReader(ViewBag.mesaj2)); XsltTransformer xsltTransformer = xsltExecutable.Load(); xsltTransformer.InitialContextNode = xdmNode; using (StringWriter stringWriter = new StringWriter()) { Serializer serializer = new Serializer(); serializer.SetOutputWriter(stringWriter); xsltTransformer.Run(serializer); ViewBag.cikti = stringWriter; } return(View("Index")); }
public string Transform(XmlNode input) { var inputNode = processor.NewDocumentBuilder().Build(input); transformer.InitialContextNode = inputNode; var dest = processor.NewSerializer(); var stream = new MemoryStream(); dest.SetOutputStream(stream); transformer.Run(dest); return(StreamUtility.MemoryStreamToString(stream)); }
public override void Close() { outputCapture.Position = 0; serializer.SetOutputStream(BaseStream); lock (transformer) { transformer.InitialContextNode = documentBuilder.Build(outputCapture); transformer.Run(serializer); } serializer.Close(); base.Close(); }
static void Main(string[] args) { string infile = @"c:\daisybooks\verysimplebook\verysimplebook.xml"; string infile_dir = @"c:\daisybooks\verysimplebook\"; string xsltfile = @"c:\devel\amis\trunk\amis\bin\xslt\dtbook\dtbook2xhtml.xsl"; string outfile = @"c:\devel\amis\sandbox\dtbooktransformer_out.xml"; // Create a Processor instance. Processor processor = new Processor(); // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(infile)); // Create a transformer for the stylesheet. XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltfile)).Load(); QName basedir = new QName("", "baseDir"); List <XdmAtomicValue> elementNames = new List <XdmAtomicValue>(); elementNames.Add(new XdmAtomicValue(infile_dir)); XdmValue basedir_value = new XdmValue(elementNames); transformer.SetParameter(basedir, basedir_value); // Set the user-written XmlResolver UserXmlResolver runTimeResolver = new UserXmlResolver(); runTimeResolver.Message = "** Calling transformation-time XmlResolver: "; transformer.InputXmlResolver = runTimeResolver; // Set the root node of the source document to be the initial context node transformer.InitialContextNode = input; /* * String outfile = "OutputFromXsltSimple2.xml"; * Serializer serializer = new Serializer(); * serializer.SetOutputStream(new FileStream(outfile, FileMode.Create, FileAccess.Write)); */ // Create a serializer, with output to the standard output stream Serializer serializer = new Serializer(); serializer.SetOutputWriter(Console.Out); // Transform the source XML and serialize the result document transformer.Run(serializer); Console.ReadLine(); }
void createHtmlFromXsl() { string resultDoc = projSettings.DestinationPath + projSettings.DocHtmlFileName; if (projSettings.StyleName == "OrteliusXml") { resultDoc = projSettings.DestinationPath + "/orteliusXml.xml"; } string xmlDoc = projSettings.DestinationPath + projSettings.DocXmlFileName; string xslDoc = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/styles/" + projSettings.StyleName + ".xsl"; try { Processor processor = new Processor(); System.IO.StreamReader reader = new System.IO.StreamReader(xmlDoc, 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(xslDoc)).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.SetOutputFile(resultDoc); // Transform the source XML to System.out. transformer.Run(serializer); } catch (Exception e) { //MessageBox.Show(e.ToString()); systemSvar += "Error in xslt rendering:\r\n" + e.ToString(); } }
//run an XSLT transform. //transformFile is the name of the .xsl to run; //path is usually tools directory (be sure to include final slash in passed string) //takes two paramaters and corresponding values; use empty strings if not needed static public XmlDocument performTransformWith2Params(XmlDocument inDOM, string path, string transformFile, string param1Name, string param1Value, string param2Name, string param2Value) { // Create a Processor instance. Processor processor = new Processor(); // Load the source document, building a tree XmlNode node = inDOM; XdmNode input = processor.NewDocumentBuilder().Build(node); // Compile the stylesheet XsltExecutable exec = processor.NewXsltCompiler().Compile(new XmlTextReader(path.Replace("Program Files", "PROGRA~1") + transformFile)); // Create a transformer XsltTransformer transformer = exec.Load(); string xdmToolsPath = "file:/" + path.Replace("\\", "/").Replace(" ", "%20"); // Run it once // Set parameters transformer.SetParameter(new QName("", "", "include-attributes"), new XdmAtomicValue(false)); //following may be needed if xslt itself needs to find other files transformer.SetParameter(new QName("", "", "localBaseUri"), new XdmAtomicValue(xdmToolsPath.Replace("Program%20Files", "PROGRA~1"))); //optionally add another parameter if (!String.IsNullOrEmpty(param1Name)) { transformer.SetParameter(new QName("", "", param1Name), new XdmAtomicValue(param1Value)); } //and another param if (!String.IsNullOrEmpty(param2Name)) { transformer.SetParameter(new QName("", "", param2Name), new XdmAtomicValue(param2Value)); } transformer.InitialContextNode = input; XdmDestination results = new XdmDestination(); transformer.Run(results); XmlDocument resultXmlDoc = new XmlDocument(); resultXmlDoc.LoadXml(results.XdmNode.OuterXml); XmlDeclaration declaration = resultXmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); resultXmlDoc.PrependChild(declaration); // return the result return(resultXmlDoc); }