Beispiel #1
0
        /// <summary>
        /// Create a XSLT transformation. See: https://github.com/FrendsPlatform/Frends.Xml
        /// </summary>
        /// <returns>string</returns>
        public static string Transform(TransformInput input)
        {
            var processor = new Processor();
            var compiler  = processor.NewXsltCompiler();

            using (var stringReader = new StringReader(input.Xslt))
            {
                var executable  = compiler.Compile(stringReader);
                var transformer = executable.Load();
                using (var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(input.Xml)))
                {
                    transformer.SetInputStream(inputStream, new Uri("file://"));
                    input.XsltParameters?.ToList().ForEach(x => transformer.SetParameter(new QName(x.Name), new XdmAtomicValue(x.Value)));

                    using (var stringWriter = new StringWriter())
                    {
                        var serializer = new Serializer();
                        serializer.SetOutputWriter(stringWriter);
                        transformer.Run(serializer);
                        var output = stringWriter.GetStringBuilder().ToString();
                        output = output.Replace("\n", Environment.NewLine);
                        return(output);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create a XSLT transformation. See: https://github.com/FrendsPlatform/Frends.Xml
        /// </summary>
        /// <returns>string</returns>
        public static string Transform(TransformInput input)
        {
            var processor = new Processor();
            var compiler  = processor.NewXsltCompiler();

            using (var stringReader = new StringReader(input.Xslt))
            {
                var executable  = compiler.Compile(stringReader);
                var transformer = executable.Load();


                using (var inputStream = new MemoryStream())
                {
                    //XmlDocument always produces MemoryStream where its encoding matches the input XML's declaration
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.PreserveWhitespace = true;
                    xmldoc.LoadXml(input.Xml);
                    xmldoc.Save(inputStream);
                    xmldoc = null;
                    inputStream.Position = 0;
                    transformer.SetInputStream(inputStream, new Uri("file://"));

                    input.XsltParameters?.ToList().ForEach(x => transformer.SetParameter(new QName(x.Name), new XdmAtomicValue(x.Value)));

                    using (var stringWriter = new StringWriter())
                    {
                        var serializer = processor.NewSerializer();
                        serializer.SetOutputWriter(stringWriter);
                        transformer.Run(serializer);
                        var output = stringWriter.GetStringBuilder().ToString();
                        output = output.Replace("\n", Environment.NewLine);
                        return(output);
                    }
                }
            }
        }