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.
xml Escaping Ampersand The & character appears first in entity references and must be escaped in element content or in attribute content.
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.
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 & for the ampersand and < for the less-than character).
When your XML contains &amp;
, this will result in the text &
.
When you use that in HTML, that will be rendered as &
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With