views:

226

answers:

5

I am trying to take the string "<BR>" in VB.NET and convert it to HTML through XSLT. When the HTML comes out, though, it looks like this:

&lt;BR&gt;

I can only assume it goes ahead and tries to render it. Is there any way I can convert those &lt;/&gt; back into the brackets so I get the line break I'm trying for?

+5  A: 

Check the XSLT has:

<xsl:output method="html"/>


edit: explanation from comments

By default XSLT outputs as XML(1) which means it will escape any significant characters. You can override this in specific instances with the attribute disable-output-escaping="yes" (intro here) but much more powerful is to change the output to the explicit value of HTML which confides same benefit globally, as the following:

  • For script and style elements, replace any escaped characters (such as & and >) with their actual values (& and >, respectively).
  • For attributes, replace any occurrences of > with >.
  • Write empty elements such as <br>, <img>, and <input> without closing tags or slashes.
  • Write attributes that convey information by their presence as opposed to their value, such as checked and selected, in minimized form.

from a solid IBM article covering the subject, more recent coverage from stylusstudio here

If HTML output is what you desire HTML output is what you should specify.

(1) There is actually corner case where output defaults to HTML, but I don't think it's universal and it's kind of obtuse to depend on it.

annakata
How does that help?
AnthonyWJones
where in the xsl document does this line need to go?
Joe Morgan
joe: this should be your first tag within the <xsl:stylesheet>
Pierre Spring
A: 

The string "<br>" is already HTML so you can just Response.Write("<br>").

But you meantion XSLT so I imagine there some transform going on. In that case surely the transform should be inserting it at the correct place as a node. A better question will likely get a better answer

AnthonyWJones
A: 

Try wraping it with <xsl:text disable-output-escaping="yes">&lt;br&gt;</xsl:text>

Shawn Simon
I've tried this, but it still converts it into HTML itself. (something to the effect of <xsl:text disable-...
Joe Morgan
A: 

Don't know about XSLT but..

One workaround might be using HttpUtility.HtmlDecode from System.Web namespace.

using System;
using System.Web;

class Program
{
    static void Main()
    {
        Console.WriteLine(HttpUtility.HtmlDecode("&lt;br&gt;"));
        Console.ReadKey();
    }
}

...

chakrit
A: 

Got it! On top of the selected answer, I also did something similar to this on my string:

htmlString = htmlString.Replace("&lt;","<")
htmlString = htmlString.Replace("&gt;",">")

I think, though, that in the end, I may just end up using <pre> tags to preserve everything.

Joe Morgan