Esempio n. 1
0
        /// <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));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private static Tuple <string, XslTimings> RunTransformString(string xml, string xsl, bool removeWhitespace, bool externalDefinitions, string mode, params string[] parameters)
        {
            XslOptions options = new XslOptions();

            options.RemoveWhitespace    = removeWhitespace;
            options.StartMode           = mode;
            options.ExternalDefinitions = externalDefinitions;

            foreach (string s in parameters)
            {
                string name  = s.Substring(0, s.IndexOf('='));
                string value = s.Substring(s.IndexOf('=') + 1);

                options.AddParameter(name, value);
            }

            Tuple <string, XslTimings> response;

            if (string.IsNullOrEmpty(xsl))
            {
                // Use Processing Instruction
                response = Useful.Xml.XslTransformation.Transform(xml, options);
            }
            else
            {
                response = Useful.Xml.XslTransformation.Transform(xsl, xml, options);
            }

            return(response);
        }
Esempio n. 3
0
        /// <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 XslTimings Transform(Stream xsl, Stream xml, Stream output, XslOptions options)
        {
            Contract.Requires(xsl != null);
            Contract.Requires(xml != null);
            Contract.Requires(output != null);
            Contract.Requires(options != null);

            using (TextReader xslReader = new StreamReader(xsl, true))
            {
                using (TextReader xmlReader = new StreamReader(xml, true))
                {
                    return(Transform(xslReader, xmlReader, output, options));
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Transforms the XML using the Processing Instruction
        /// </summary>
        /// <param name="xsl"></param>
        /// <param name="xml"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Tuple <string, XslTimings> Transform(string xml, XslOptions options)
        {
            Contract.Requires(xml != null);
            Contract.Requires(options != null);

            string xslFile = GetPi(xml);

            if (string.IsNullOrEmpty(xslFile))
            {
                throw new XsltException("Invalid Processing Instruction.");
            }

            string xsl = File.ReadAllText(xslFile);

            return(Transform(xsl, xml, options));
        }
Esempio n. 5
0
        /// <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);
        }