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—&amp;, &lt;, &gt;, &quot;, and &apos;—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):

  1. &lt; (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 &lt;.

  2. &amp; (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 &amp;.

  3. &gt; (Greater Than - >)
    The greater-than sign closes an XML tag. While strictly legal in general text, it must be escaped as &gt; if it appears in the sequence ]]> to avoid accidentally terminating a CDATA section. Using &gt; consistently is considered best practice.

  4. &quot; (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 &quot; to prevent prematurely closing the attribute.

  5. &apos; (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 &apos;.

How XML Parsers Process These Entities

When an XML processor reads a document, it follows a strict sequence:

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.