Exemple #1
0
        /// <summary>
        /// Repairs an html fragment (makes it Xhtml) and executes a transformation on it.
        /// </summary>
        /// <param name="html">The html to repair</param>
        /// <param name="xsltPath">The path to the XSLT to use for transformation</param>
        /// <param name="xsltParameters"></param>
        /// <param name="errorSummary">out value - warnings generated while repairing the html</param>
        /// <returns></returns>
        public static XDocument RepairXhtmlAndTransform(string html, string xsltPath, Dictionary <string, string> xsltParameters, out string errorSummary)
        {
            TidyHtmlResult tidyHtmlResult = MarkupTransformationServices.TidyHtml(html);

            errorSummary = tidyHtmlResult.ErrorSummary;
            XNode     tidiedXhtml    = tidyHtmlResult.Output;
            XDocument outputDocument = new XDocument();

            XslCompiledTransform xslt = XsltServices.GetCompiledXsltTransform(xsltPath);

            using (XmlWriter writer = outputDocument.CreateXhtmlWriter())
            {
                using (XmlReader reader = tidiedXhtml.CreateReader())
                {
                    if (xsltParameters != null && xsltParameters.Count > 0)
                    {
                        XsltArgumentList xsltArgumentList = new XsltArgumentList();
                        foreach (var xsltParameter in xsltParameters)
                        {
                            xsltArgumentList.AddParam(xsltParameter.Key, "", xsltParameter.Value);
                        }
                        xslt.Transform(reader, xsltArgumentList, writer);
                    }
                    else
                    {
                        xslt.Transform(reader, writer);
                    }
                }
            }

            return(outputDocument);
        }
Exemple #2
0
            private static XslCompiledTransform GetCachedTransformation(string xsltFilePath)
            {
                string transformationCacheKey = "Compiled" + xsltFilePath;
                var    cache = HostingEnvironment.Cache;

                XslCompiledTransform transformer = cache[transformationCacheKey] as XslCompiledTransform;

                if (transformer == null)
                {
                    lock (typeof(XslTransformationStream))
                    {
                        transformer = cache[transformationCacheKey] as XslCompiledTransform;
                        if (transformer == null)
                        {
                            transformer = XsltServices.GetCompiledXsltTransform(xsltFilePath);
                            cache.Add(transformationCacheKey,
                                      transformer,
                                      new CacheDependency(xsltFilePath),
                                      DateTime.MaxValue,
                                      TimeSpan.FromDays(1.0),
                                      CacheItemPriority.Default,
                                      null);
                        }
                    }
                }
                return(transformer);
            }
Exemple #3
0
        /// <summary>
        /// Repairs an html fragment (makes it Xhtml) and executes a transformation on it.
        /// </summary>
        /// <param name="xml">The xml to repair</param>
        /// <param name="xsltPath">The path to the XSLT to use for transformation</param>
        /// <returns></returns>
        public static XDocument RepairXmlAndTransform(string xml, string xsltPath)
        {
            XDocument tidiedXml = MarkupTransformationServices.TidyXml(xml);

            XDocument outputDocument = new XDocument();

            XslCompiledTransform xslt = XsltServices.GetCompiledXsltTransform(xsltPath);

            using (XmlWriter writer = outputDocument.CreateWriter())
            {
                using (XmlReader reader = tidiedXml.CreateReader())
                {
                    xslt.Transform(reader, writer);
                }
            }

            return(outputDocument);
        }