XML Entities

XML entities allow you to use text to refer to a data item, instead of using the data item itself.

You can use entities to represent:

  • Characters that would otherwise cause problems for the XML processor
  • Large blocks of data that need to be repeated throughout the document
  • Characters that you can't type on your keyboard (eg, ©)

Problematic Characters

Some characters have a special meaning in XML. For example, the less than sign (<) marks the beginning of a tag. And, of course, the greater than sign (>) marks the end of the tag. When the XML processor parses the document, it looks for these characters (and others) to determine how to interpret the document.

This is fine, as long as your data doesn't contain any of these characters. But what if it does? What if your data contains say, a less than sign?

Imagine you had the following text within an element: 10 < 5. When the XML processor encounters the <, it will assume it's the start of an opening tag. Problem is, it's not.

In order to include characters such as < and & etc, you need to use their entity reference instead of the character itself.

Entity Syntax

You start an entity reference using the & character and close it using the ; character.

Example:

Predefined Entity References

There are 5 predefined entity references in XML. These are:

Entity ReferenceCharacter
&lt;<
&gt;>
&amp;&
&quot;"
&apos;'