可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
When your XML contains &
, this will result in the text &
.
When you use that in HTML, that will be rendered as &
.
回答2:
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.
回答3:
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 &
.
回答4:
CDATA tags?
<![CDATA[
This is some text with ampersands & other funny characters. >>
]]>
回答5:
&
should work just fine, Wikipedia has a List of predefined entities in XML.
回答6:
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.
回答7:
I have tried &, but it didn\'t work. Based on Wim ten Brink\'s answer I tried &amp and it worked.
One of my fellow developers suggested me to use & and that worked regardless of how many times it may be rendered.
回答8:
&
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:
&amp;
For a more thorough explanation of XML encoding, see:
What characters do I need to escape in XML documents?
回答9:
How about using the unicode \\u0026
? Works for me in my android XML files. If problems arise, someone let me know.
回答10:
<xsl:text disable-output-escaping=\"yes\">& </xsl:text>
will do the trick.
回答11:
A related issue is how to encode XML element content when such content contains an ampersand -- if the XML is part of a URI submitted with a GET action. (Leaving aside the wisdom of such an API -- it was something I had to deal with today.) I tried everything mentioned above, and the only thing that actually worked was %26amp;
The %26
was needed to URL-encode the first character in the XML-encoding for ampersand. The amp;
following the %26
was needed because the XML portion of the URI was being loaded into an XML document server-side. So in this fun scenario, it was necessary to staple both encoding methodologies together.
回答12:
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 <>
in replacement of that.
<Employees Id=\"1\" Name=\"ABC\">
<Query>
SELECT * FROM EMP WHERE ID=1 AND RES <> \'GCF\'
<Query>
</Employees>
Click here to see all the codes.