XML Predefined Entities: amp, lt, gt, quot, apos
XML uses predefined entities to safely represent characters that
would otherwise conflict with its core markup syntax. Characters like
< and & signal the beginning of tags
and entity references, making it impossible to use them as raw text
without confusing a parser. To solve this, the XML specification
includes five built-in entity references—&,
<, >, ",
and '—which allow authors to include reserved
characters seamlessly in text content and attribute values.
The Five Predefined Entities
XML processors natively recognize five specific entity references without requiring a Document Type Definition (DTD):
<(Less Than -<)
The less-than sign is the opening delimiter for XML tags. If an unescaped<appears within character data or attributes, the parser attempts to read it as a new tag, causing a fatal syntax error. It must always be escaped using<.&(Ampersand -&)
The ampersand signals the start of an entity reference. If used alone as text, the parser expects an entity name to follow. To prevent parsing errors, raw ampersands must always be written as&.>(Greater Than ->)
The greater-than sign closes an XML tag. While strictly legal in general text, it must be escaped as>if it appears in the sequence]]>to avoid accidentally terminating a CDATA section. Using>consistently is considered best practice."(Quotation Mark -")
Double quotation marks delimit attribute values. If an attribute value is enclosed in double quotes and needs to contain a double quote, that inner character must be escaped as"to prevent prematurely closing the attribute.'(Apostrophe -')
Single quotation marks can also delimit attribute values. If single quotes are used around an attribute value that contains an internal single quote or apostrophe, it must be escaped as'.
How XML Parsers Process These Entities
When an XML processor reads a document, it follows a strict sequence:
- Tokenization and Parsing: The parser scans the
text. When it encounters an ampersand followed by a predefined entity
name and a semicolon (e.g.,
&), it recognizes it as a single token. - Replacement: The parser substitutes the entity
reference with its corresponding literal character
(
&becomes&,<becomes<, etc.). - Data Delivery: The decoded literal character is passed to the application consuming the XML. The application receives clean, intended text without the entity syntax.
Because these five entities are defined directly in the W3C XML standard, every compliant XML parser handles them automatically, ensuring consistent document rendering and structural validity across all systems.