XSLT输出中的&符号问题

Dav*_*eis 4 .net c# xslt

我使用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&amp;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)

pau*_*oya 5

您获得的输出是可接受的HTML.
正如我刚从这里学到的那样,这是在HTML页面中编写URL的正确方法!
所以我认为应该有一种单独生成角色的方法,但你可能不需要(不应该).