What characters do I need to escape in XML documents?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In XML documents, certain characters have special meanings and must be escaped if they are to be used as literal values. This practice ensures that the XML parser can correctly interpret the data without confusion from characters that might otherwise be mistaken for markup.
Understanding the Need to Escape Characters
XML is a markup language which uses tags (elements) and attributes to structure data. The characters that are involved in defining tags or attributes have special meanings in XML syntax. When these characters need to be used in a way that doesn’t serve their syntactic purpose, they must be "escaped" to avoid being misinterpreted by the XML parser.
Special Characters in XML
Here are the specific characters that need to be escaped in XML documents:
- Ampersand (
&): Since the ampersand is used to start an entity reference (like<for the less-than character), it must be escaped if you want to use it as an ampersand in your text. - Less-than (
<): This character is used to open tags. To use it as a less-than symbol within the text, you must escape it. - Greater-than (
>): Although it's mostly safe to use this character in text content, it should be escaped when it’s part of character data that could otherwise be misinterpreted as the end of a tag. - Double quote (
"): This character is used to denote attribute values. If a double quote itself is a part of the attribute value, it must be escaped. - Apostrophe or single quote (
'): Similarly, when single quotes are used around attribute values, any single quote within the value must be escaped.
How to Escape Characters
To escape special characters in XML, you can use predefined entity references. Here’s how each of the characters mentioned above can be escaped:
- Ampersand (
&):& - Less-than (
<):< - Greater-than (
>):> - Double quote (
"):" - Apostrophe or single quote (
'):'
These entity references are predefined in XML and can be used in any XML document.
Practical Example
Consider an XML element meant to include a fragment of HTML:
This XML is incorrect because the parser will interpret <b> as the beginning of a new element. To fix this, escape the less-than and greater-than characters:
Special Cases
- CDATA Sections: For larger blocks of text that need to contain multiple special characters, CDATA sections can be useful. Everything within a
<![CDATA[and]]>section is ignored by the parser in terms of parsing XML elements and entities:
- Other entities: XML allows you to define your own entities within a document type definition (DTD), which can then be used as shortcuts within the document.
Summary
Here is a summary table of the escape characters and their entity references in XML:
| Character | Usage in XML | Entity Reference |
Ampersand (&) | Start of an entity reference | & |
Less-than (<) | Start of a tag | < |
Greater-than (>) | Could appear as end of a tag | > |
Double quote (") | Delimit attribute values | " |
Apostrophe (') | Delimit attribute values | ' |
Understanding these basic principles of character escaping in XML is crucial for creating well-formed and valid XML documents. Correctly escaping characters ensures that the data is accurately parsed and rendered by XML processors and related technologies such as XSLT and XPath.

