How do I escape ampersands in XML so they are rendered as entities in HTML?
When your XML contains &
, this will result in the text &
.
When you use that in HTML, that will be rendered as &
.
How can I escape & in XML?
Use &
in place of &
.
Change it to:
<string name="magazine">Newspaper & Magazines</string>
escaping ampersands in XML
An ampersand may also be part of a numeric character reference, but that particular character () is not allowed in XML:
Character Range
[2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
Minor note: you mean well-formed, not valid. See Well-formed vs Valid XML.
What characters do I need to escape in XML documents?
If you use an appropriate class or library, they will do the escaping for you. Many XML issues are caused by string concatenation.
XML escape characters
There are only five:
" "
' '
< <
> >
& &
Escaping characters depends on where the special character is used.
The examples can be validated at the W3C Markup Validation Service.
Text
The safe way is to escape all five characters in text. However, the three characters "
, '
and >
needn't be escaped in text:
<?xml version="1.0"?>
<valid>"'></valid>
Attributes
The safe way is to escape all five characters in attributes. However, the >
character needn't be escaped in attributes:
<?xml version="1.0"?>
<valid attribute=">"/>
The '
character needn't be escaped in attributes if the quotes are "
:
<?xml version="1.0"?>
<valid attribute="'"/>
Likewise, the "
needn't be escaped in attributes if the quotes are '
:
<?xml version="1.0"?>
<valid attribute='"'/>
Comments
All five special characters must not be escaped in comments:
<?xml version="1.0"?>
<valid>
<!-- "'<>& -->
</valid>
CDATA
All five special characters must not be escaped in CDATA sections:
<?xml version="1.0"?>
<valid>
<![CDATA["'<>&]]>
</valid>
Processing instructions
All five special characters must not be escaped in XML processing instructions:
<?xml version="1.0"?>
<?process <"'&> ?>
<valid/>
XML vs. HTML
HTML has its own set of escape codes which cover a lot more characters.
What is the use of & in XML?
&
is simply the encoded version of the "&" (ampersand) character.
So yes, the entry you see represents the "&" character.
From W3C XML 1.0 Spec, section 2.4:
The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters... If they are needed elsewhere, they must be escaped using either numeric character references or the strings " & " and " < " respectively.
Prevent re-encoding ampersands using Node's setTextContent method
Change the pre-processing to replace straight quotes with Unicode characters, not with invalid XML entities. Those entities are defined by HTML, and is not valid XML.
“
should be“
or\u201C
if written as Java literal”
should be”
or\u201D
if written as Java literal‘
should be‘
or\u2018
if written as Java literal’
should be’
or\u2019
if written as Java literal'
should be'
Related Topics
How to Modify the Fill Color of an Svg Image When Being Served as Background Image
Why Do Browsers Insert Tbody Element into Table Elements
What's the Difference Between Disabled="Disabled" and Readonly="Readonly" for HTML Form Input Fields
Transparent Half Circle Cut Out of a Div
Can an HTML Element Have Multiple Ids
How to Create a Div With a Curved Bottom
Should I Put Input Elements Inside a Label Element
Nesting Block Level Elements Inside the ≪P≫ Tag... Right or Wrong
Is It Wrong to Change a Block Element to Inline With CSS If It Contains Another Block Element
Absolutely Positioned Flex Item Is Not Removed from the Normal Flow in Ie11
An Invalid Form Control With Name='' Is Not Focusable
How to Scale the Content of an Iframe
What's the Proper Value For a Checked Attribute of an HTML Checkbox