我使用XSL将XML文档转换为.NET中的HTML.
XML中的一个节点有一个URL,应该作为HTML的HTML标记的href参数输出.当输入URL具有&符号(例如http://servers/path?par1=val1&par2=val2
)时,&符号在输出HTML中显示为&
.
有什么方法可以解决这个问题吗?是disable-output-escaping
解决方案吗?难道不会产生一大堆其他问题吗?
这是一个重现问题及其输出的代码示例:
输出:
<html>
<body>
<a href="http://servers/path?par1=val1&par2=val2#section1" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
C#代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Xml.Xsl;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = ComposeXml();
XmlDocument styleSheet = new XmlDocument();
styleSheet.LoadXml(XslStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter(Console.Out);
myWriter.Formatting = Formatting.Indented;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(styleSheet);
myXslTrans.Transform(xmlDoc, null, myWriter);
Console.ReadKey();
}
private const string XslStyleSheet =
@"<xsl:stylesheet version=""1.0""
xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
<xsl:template match=""/"">
<html>
<body>
<a>
<xsl:attribute name=""href"">
<xsl:value-of select=""root/url"" />
</xsl:attribute>
</a>
</body>
</html>
</xsl:template>
</xsl:stylesheet>";
static private XmlDocument ComposeXml()
{
XmlDocument doc = new XmlDocument();
XmlElement rootNode = doc.CreateElement("root");
doc.AppendChild(rootNode);
XmlElement urlNode = doc.CreateElement("url");
urlNode.InnerText = "http://servers/path?par1=val1&par2=val2#section1";
rootNode.AppendChild(urlNode);
return doc;
}
}
}
Run Code Online (Sandbox Code Playgroud)