What are the special reserved character entities in HTML and in XML?
The information that i have says:
HTML:
&
(replace with&
)<
(replace with<
)>
(replace with>
)"
(replace with"
)'
(replace with'
)
XML:
<
(replace with<
)>
(replace with>
)&
(replace with&
)'
(replace with'
)"
(replace with"
)
But i cannot find documentation on either of these.
The W3C does mention, in Extensible Markup Language (XML) 1.0 (Fifth Edition), certain predefined entity references. But it says that these entities are predefined (in the same way that ©
is predefined); not that they must be escaped:
4.6 Predefined Entities
[Definition: Entity and character references may both be used to escape the left angle bracket, ampersand, and other delimiters. A set of general entities (amp, lt, gt, apos, quot) is specified for this purpose. Numeric character references may also be used; they are expanded immediately when recognized and must be treated as character data, so the numeric character references " < " and " & " may be used to escape < and & when they occur in character data.]
What characters must be escaped into entity references in HTML?
What characters must be escaped into entity references in XML?
Update:
From Extensible Markup Language (XML) 1.0 (Fifth Edition):
2.4 Character Data and Markup
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.To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (
'
) may be represented as "'
", and the double-quote character ("
) as ""
".
i read the former as saying that
must be:
<
(<
) must be&
(&
) must be
may, but must when appearing as ]]>
>
(>
) must be, if appearing as]]>
And that '
and "
don't have to be escaped at all; unless you want to have quotes inside quoted attributes.
From HTML 4.01 Specification, HTML Document Representation:
5.3.2 Character entity references
Authors wishing to put the "
<
" character in text should use "<
" (ASCII decimal 60) to avoid possible confusion with the beginning of a tag (start tag open delimiter).Similarly, authors should use "
>
" (ASCII decimal 62) in text instead of ">
" to avoid problems with older user agents that incorrectly perceive this as the end of a tag (tag close delimiter) when it appears in quoted attribute values.Authors should use "
&
" (ASCII decimal 38) instead of "&
" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&
" in attribute values since character references are allowed within CDATA attribute values.Some authors use the character entity reference "
"
" to encode instances of the double quote mark ("
) since that character may be used to delimit attribute values.
HTML is much more wishy-washy on the rules, but it sounds like i should:
<
should be with<
>
should be with>
&
should be with&
"
should be with"
and if "
can be an entity reference, i should also replace '
with &
.
Update Two
From HTML5 - A vocabulary and associated APIs for HTML and XHTML:
8.3 Serializing HTML fragments
Escaping a string (for the purposes of the algorithm above) consists of running the following steps:
Replace any occurrence of the "
&
" character by the string "&
".Replace any occurrences of the U+00A0 NO-BREAK SPACE character by the string "
".If the algorithm was invoked in the attribute mode, replace any occurrences of the "
"
" character by the string ""
".If the algorithm was not invoked in the attribute mode, replace any occurrences of the "
<
" character by the string "<
", and any occurrences of the ">
" character by the string ">
".
Which i read as HTML:
&
by&
always
always"
by"
if it's inside an attribute<
by<
if it's not in an attribute (i.e. attributes can contain<
)>
by>
if it's not in an attribute (i.e. attributes can contain>
)