4

I use XSL to transform a XML document into HTML in .NET.

One of the nodes in the XML has a URL that should be output as the href parameter of the a HTML tag of the HTML. When the input URL has an ampersand character (e.g. http://servers/path?par1=val1&par2=val2) the ampersand appears in the output HTML as &.

Is there any way to solve this issue? Is disable-output-escaping the solution? Would not that create a whole bunch of other problems?

Here's a code sample that reproduces the issue and its output:

Output:

<html>
  <body>
    <a href="http://servers/path?par1=val1&amp;par2=val2#section1" />
  </body>
</html>

C# Code:

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;

        }
    }
}
1
  • 2
    +1 for ready-to-run sample code Commented Oct 22, 2009 at 23:57

2 Answers 2

5

The output that you get is acceptable HTML.
As I just learned from here, that's actualy THE correct way to write an URL inside an HTML page!
So I think there should be a way of generating the character by itself but you probably don't need to (shouldn't).

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah makes a lot of sense now. It even seems obvious :-)
3

When you try to write XML as an attribute value, it's always encoded. But, for text nodes, you can disable that encoding by using disable-output-escaping attribute:

<a href="{root/url}">
    <xsl:value-of select="root/url" disable-output-escaping="yes" />
</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.