What Are Predefined Entity References in XML?

Predefined entity references in XML are special shorthand codes used to represent reserved characters that would otherwise disrupt an XML parser. Because XML uses specific characters like angle brackets and ampersands to define markup syntax, these characters cannot be used directly as regular text within element content or attribute values. This article explains what the five predefined XML entity references are, why they are essential for creating well-formed documents, and how to use them properly.

The 5 Predefined Entity References

XML specifies five built-in entity references that XML parsers automatically recognize without requiring a Document Type Definition (DTD):

  1. &lt; represents the less-than sign (<)
  2. &gt; represents the greater-than sign (>)
  3. &amp; represents the ampersand (&)
  4. &apos; represents the single quote or apostrophe (')
  5. &quot; represents the double quote (")

Each reference begins with an ampersand (&) and ends with a semicolon (;).

Why Entity References Are Necessary

1. Preventing Parsing Errors

The characters < and & are strictly reserved in XML syntax: * The < character signals the start of a tag. If an XML parser encounters a raw < inside element text, it attempts to parse the following text as a tag name, which causes a fatal parsing error. * The & character signals the start of an entity reference. If it is not followed by a valid entity name and semicolon, the parser fails.

Replacing these characters with &lt; and &amp; ensures the parser treats them as standard character data rather than structural markup.

2. Handling Attribute Delimiters

In XML, attribute values are enclosed in single or double quotes. If the text inside an attribute contains the same type of quote used to enclose it, the XML parser interprets the inner quote as the end of the attribute value.

Using &quot; inside double-quoted attributes or &apos; inside single-quoted attributes prevents attribute truncation and syntax violations.

3. Maintaining Well-Formed XML

For an XML document to be processed by any standard XML engine, it must be “well-formed.” If reserved characters are used without escaping, the document becomes malformed, and XML parsers will halt processing immediately.

Example Usage

Invalid XML:

<message>if x < 10 & y > 20 then alert("Success")</message>

This fails because < and & break the XML structure.

Valid XML:

<message>if x &lt; 10 &amp; y &gt; 20 then alert(&quot;Success&quot;)</message>

This parses successfully, and the receiving application receives the original text correctly decoded.