问题
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: &
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 "
&
" and "<
" respectively. The right angle bracket (>) may be represented using the string ">
", and MUST, for compatibility, be escaped using either ">
" 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) - < or <
> (greater-than) - > or >
& (ampersand) - &
' (apostrophe or single quote) - '
" (double-quote) - "
来源:https://stackoverflow.com/questions/14500800/how-can-i-include-an-ampersand-character-in-an-xml-document