Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape ampersands in XML so they are rendered as entities in HTML?

I have some XML text that I wish to render in an HTML page. This text contains an ampersand, which I want to render in its entity representation: &.

How do I escape this ampersand in the source XML? I tried &, but this is decoded as the actual ampersand character (&), which is invalid in HTML.

So I want to escape it in such a way that it will be rendered as & in the web page that uses the XML output.

like image 532
AJM Avatar asked Aug 25 '09 14:08

AJM


People also ask

Does ampersand need to be escaped in XML?

xml Escaping Ampersand The & character appears first in entity references and must be escaped in element content or in attribute content.

How do you escape ampersand in HTML?

If you want one to appear in text on a web page you should use the encoded named entity “ & ”—more technical mumbo-jumbo at w3c.org. While most web browsers will let you get away without encoding them, stuff can get dicey in weird edge cases and fail completely in XML.

Is ampersand valid in XML?

Note that the ampersand (&) and less-than (<) characters are not permitted in XML attribute values. Since XFDL computes appear in a compute attribute, these must be escaped with character or entity references (e.g. the entity references &amp; for the ampersand and &lt; for the less-than character).


1 Answers

When your XML contains &amp;amp;, this will result in the text &amp;.

When you use that in HTML, that will be rendered as &.

like image 181
Wim ten Brink Avatar answered Sep 26 '22 19:09

Wim ten Brink