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: &
.
&
should work just fine, Wikipedia has a List of predefined entities in XML.
In my case I had to change it to %26
.
I needed to escape &
in a URL. So &
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.
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.
&
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 &
) and then use that to create properly encoded HTML (which involves again changing &
to &
). That results in:
&
For a more thorough explanation of XML encoding, see:
What characters do I need to escape in XML documents?
<xsl:text disable-output-escaping="yes">& </xsl:text>
will do the trick.