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.


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

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


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.


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 ' & '.

链接地址: http://www.djcxy.com/p/42158.html

上一篇: 我怎样才能从图像标签使用PHP单独src?

下一篇: 我如何在XML中跳过&符号,以便它们在HTML中呈现为实体?