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

HtmlXmlEscapingAmpersand

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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

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

Solution 2 - Html

As per §2.4 of the XML 1.0 spec, you should be able to use &.

> I tried & but this isn't allowed.

Are you sure it isn't a different issue? XML explicitly defines this as the way to escape ampersands.

Solution 3 - Html

The & character is itself an escape character in XML so the solution is to concatenate it and a Unicode decimal equivalent for & thus ensuring that there are no XML parsing errors. That is, replace the character & with &.

Solution 4 - Html

Use CDATA tags:

 <![CDATA[
   This is some text with ampersands & other funny characters. >>
 ]]>

Solution 5 - Html

Solution 6 - Html

In my case I had to change it to %26.

I needed to escape & in a URL. So &amp; did not work out for me. The urlencode function changes & to %26. This way neither XML nor the browser URL mechanism complained about the URL.

Solution 7 - Html

I have tried &amp, but it didn't work. Based on Wim ten Brink's answer I tried &amp;amp and it worked.

One of my fellow developers suggested me to use &#x26; and that worked regardless of how many times it may be rendered.

Solution 8 - Html

&amp; is the way to represent an ampersand in most sections of an XML document.

If you want to have XML displayed within HTML, you need to first create properly encoded XML (which involves changing & to &amp;) and then use that to create properly encoded HTML (which involves again changing & to &amp;). That results in:

&amp;amp;

For a more thorough explanation of XML encoding, see:

https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents

Solution 9 - Html

<xsl:text disable-output-escaping="yes">&amp;&nbsp;</xsl:text> will do the trick.

Solution 10 - Html

Consider if your XML looks like below.

<Employees Id="1" Name="ABC">
  <Query>
    SELECT * FROM EMP WHERE ID=1 AND RES<>'GCF'
  <Query>
</Employees>

You cannot use the <> directly as it throws an error. In that case, you can use &#60;&#62; in replacement of that.

<Employees Id="1" Name="ABC">
  <Query>
    SELECT * FROM EMP WHERE ID=1 AND RES &#60;&#62; 'GCF'
  <Query>
</Employees>

14.1 How to use special characters in XML has all the codes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAJMView Question on Stackoverflow
Solution 1 - HtmlWim ten BrinkView Answer on Stackoverflow
Solution 2 - HtmlJohn FeminellaView Answer on Stackoverflow
Solution 3 - HtmltroubleView Answer on Stackoverflow
Solution 4 - HtmlscragarView Answer on Stackoverflow
Solution 5 - Htmlnikc.orgView Answer on Stackoverflow
Solution 6 - HtmlSerhat AkayView Answer on Stackoverflow
Solution 7 - HtmlmcamposView Answer on Stackoverflow
Solution 8 - HtmlRiley MajorView Answer on Stackoverflow
Solution 9 - HtmlRickView Answer on Stackoverflow
Solution 10 - HtmlSarath KSView Answer on Stackoverflow