XML Internal Entities

Internal Entities

An internal entity is one that is defined locally within a DTD. The basic purpose of an internal entity is to get rid of typing the same content (like the name of the organization) again and again. And instead, we can define an internal entity to contain the text and then only you need to use the entity where you want to insert the text. Because the entity is expanded by the parser, you can be assured that you'll get the same text in every location. The parser will also catch if you misspell an entity name.

To use an entity you insert an "entity reference" into your document. You're probably already familiar with some entity references because you need to use them for special characters that cannot be typed directly in an XML document, like "<" and "&". An entity reference is an ampersand (&), followed by the name of the entity, followed by a semicolon (;).

If you've defined the entity "compname" to contain the name of your company, then you can use it with the following entity reference "&compname;".

The text that is inserted by an entity reference is called the "replacement text". The replacement text of an internal entity can contain markup (elements, attributes, processing instructions, other entity references, etc.), but the content must be balanced (any element that you start in an entity must end in the same entity) and circular entity references are not allowed.

Entity declaration

Entities must be declared before they can be used. They may be declared in the DTD, if your XML parser processes the DTD also known as the external subset), or the internal subset.Note: if the same entity is declared more than once, only the first declaration applies and the internal subset is processed before the external subset.

All entities are declared with the "ENTITY" declaration. The exact format of the declaration distinguishes between internal, external, and parameter entities.

Declaring Internal Entities

An internal entity declaration has the following form:

<!ENTITY entityname "replacement text">

You can use either double or single quotes to delimit the replacement text. The declaration of the yoyo, mentioned earlier,would be:

<!ENTITY compname  'w3resource.com.'>

Predefined Internal Entities

There are five internal entities which are predefined in internal entities. All xml processors are required to support references to those entities, even if they are not declared.

Here is a table containing predefined entities and their replacement text:

Entity Name Replacement Text
lt The less than sign (<)
gt The greater than sign (>)
>amp The ampersand (&)
apos The single quote or apostrophe (')
quot The double quote (")

Previous: XML Entity
Next: XML External Entities

Follow us on Facebook and Twitter for latest update.