How can I include an ampersand (&) character in an XML document?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-04 12:51:10

问题


How can I put an & symbol in an attribute of an XML tag while keeping the XML valid?

<?xml version="1.0" ?> 
<a text="b&c"/>

When I use W3School's validator, I get the error:

EntityRef: expecting ';'


回答1:


Use a character reference to represent it: &amp;

See the specification:

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; ", and MUST, for compatibility, be escaped using either " &gt; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.




回答2:


You can use these escape sequences :

< (less-than)                   -   &#60; or &lt;

> (greater-than)                -   &#62; or  &gt;

& (ampersand)                   -   &#38;

' (apostrophe or single quote)  -   &#39;

" (double-quote)                -   &#34;


来源:https://stackoverflow.com/questions/14500800/how-can-i-include-an-ampersand-character-in-an-xml-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!