本文介绍了如何制作XslCompiledTransform变换& amp;正如XslTransform所做的那样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我们已经使用过XslTransform。 .NET 1.1,用于转换XML文档,数据集,xsl到HTML。其中一些html内容包含javascript和链接。例如: // javascript if(a& gt; b) ..... // xsl内容 abc.aspx?p1 = v1& amp; p2 =< xsl:value-of select =" $ v2" /> 但是,由于我们切换到用XslCompiledTransform替换XslTransform,所有这些实体类都需要包含在xsl:text中,并带有DOE < xsl:text disable-output-escaping =" yes"> 来自XslTransform的结果 if(ab) .... abc.aspx?p1 = v1& p2 = ... 来自XslCompiledTransform的结果,不包含在xsl:text中而不包含doe if(a& gt; b) .... abc.aspx?p1 = v1& amp; p2 = .. 。在我们的实现中,所有XML内容都被转换为XpathDocument,而xsl文件的输出方法被设置为html。我尝试使用1- StringWriter,2- XmlWritter,3- MemoryStream(见下文),我们从这三种方法得到了相同的结果 我们有办法吗?可以使XslCompiledTransform处理那些实体类,例如& amp;,& gt;,& lt; ,作为XslTransform //使用StringWriter版本 public static string Transform(System.Xml.XPath.XPathNavigator nav,string xslPath,Hashtable htArguments,XmlResolver resolver) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); XslCompiledTransform xt = PrepareCompiledTransform(xslPath,resolver); xt.Transform(nav,PrepareXsltArgumentList(htArguments),sw); sw.Close(); xt = null; 返回sb.ToString(); } //使用xml编写器 public static string Transform(System.Xml.XPath.XPathNavigator nav,string xslPath,Hashtable htArguments,XmlResolver resolver) { StringBuilder sb = new StringBuilder( );; StringWriter sw = new StringWriter(sb); XslCompiledTransform xt = PrepareCompiledTransform(xslPath,resolver); XmlWriterSettings settings = xt.OutputSett ings.Clone(); settings.CheckCharacters = false; settings.CloseOutput = true; XmlWriter xw = XmlWriter.Create(sb ,设置); xt.Transform(nav,PrepareXsltArgumentList(htArguments),xw); xw.Close(); sw。关闭(); 返回sb.ToString(); } //使用内存流 public static string Transform(System.Xml.XPath.XPathNavigator nav,string xslPath,Hashtable htArguments,XmlResolver resolver) { //使用memroystream MemoryStream mry = new MemoryStream(); StreamReader sr = new StreamReader(mry,System.Text.UTF8Encoding.UTF8); XslCompiledTransform xt = PrepareCompiledTransform (xslPath,resolver); xt.Transform(nav,PrepareXsltArgumentList(htArguments),mry); mry.Position = 0; string s = sr.ReadToEnd(); sr.Close(); 返回s; } 私有静态X. slCompiledTransform PrepareCompiledTransform(字符串xslPath,XmlResolver stylesheetResolver) { bool bEnableDebug = false; XslCompiledTransform xt = new XslCompiledTransform(bEnableDebug); XsltSettings settings = new XsltSettings(); settings.EnableDocumentFunction = true; XmlResolver theResolver =(stylesheetResolver == null? new XmlUrlResolver():stylesheetResolver); xt.Load(GetXmlFilePath(xslPath),settings,theResolver); 返回xt; } We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascriptif (a > b)..... // xsl contentsabc.aspx?p1=v1&p2=<xsl:value-of select="$v2" /> However, since we switched to replace XslTransform with XslCompiledTransform, all these entity classes need to be wrapped within xsl:text with DOE<xsl:text disable-output-escaping="yes"> Results from XslTransformif (a b)....abc.aspx?p1=v1&p2=... Results from XslCompiledTransform without be wrapped within xsl:text without doeif (a > b)....abc.aspx?p1=v1&p2=... In our implementations, all the XML contents were "converted into XpathDocument, and the output method of xsl file is set to "html" and I have tried to use 1- StringWriter, 2- XmlWritter, 3- MemoryStream (see below), and we got the same results from all three approaches Is there a way that we can make the XslCompiledTransform handle those entity classes, e.g. &, >, < , as XslTransform // using StringWriter versionpublic static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver ){StringBuilder sb = new StringBuilder();StringWriter sw = new StringWriter(sb); XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);xt.Transform(nav, PrepareXsltArgumentList(htArguments), sw);sw.Close();xt = null;return sb.ToString();}// using xml writer public static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver ){StringBuilder sb = new StringBuilder();StringWriter sw = new StringWriter(sb); XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);XmlWriterSettings settings = xt.OutputSettings.Clone();settings.CheckCharacters = false;settings.CloseOutput = true;XmlWriter xw = XmlWriter.Create(sb, settings);xt.Transform(nav, PrepareXsltArgumentList(htArguments), xw);xw.Close();sw.Close();return sb.ToString();}// using memory streampublic static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver ){// use memroystreamMemoryStream mry = new MemoryStream();StreamReader sr = new StreamReader(mry, System.Text.UTF8Encoding.UTF8); XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);xt.Transform(nav, PrepareXsltArgumentList(htArguments), mry); mry.Position = 0;string s = sr.ReadToEnd();sr.Close();return s; }private static XslCompiledTransform PrepareCompiledTransform(string xslPath, XmlResolver stylesheetResolver ){bool bEnableDebug = false;XslCompiledTransform xt = new XslCompiledTransform(bEnableDebug); XsltSettings settings = new XsltSettings();settings.EnableDocumentFunction = true;XmlResolver theResolver = (stylesheetResolver == null ? new XmlUrlResolver() : stylesheetResolver);xt.Load(GetXmlFilePath(xslPath), settings, theResolver);return xt;} 推荐答案 脚本应该没有逃脱。只需确保输出HTML - set< xsl:output method =" html"并输出到Stream或TextWriter。 Script should go out with no escaping. Just make sure you output HTML -set <xsl:output method="html"and output into Stream or TextWriter. 这篇关于如何制作XslCompiledTransform变换& amp;正如XslTransform所做的那样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 19:40