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

前端 未结 11 1808
野趣味
野趣味 2020-11-22 15:59

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

11条回答
  •  醉梦人生
    2020-11-22 17:01

    & 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?

提交回复
热议问题