An XML document can have 4 types of entities as given below:

Built-In entity

The 5 types of built in entities supported by all XML parsers are given below. They can be used in DTD and XML content, but they cannot be part of markup.

Built-in entities
Character References
& &
< &lt;
> &gt;
&quot;
‘  &apos;

Example with built-in entities

DTD:

<!DOCTYPE Base[

    <!ELEMENT Base (Salary)>  

    <!ELEMENT Salary (#PCDATA)>

    <!ENTITY less "&lt;">

]>

XML:

<Salary>Salary is &less; than 10K</Salary>

Output:

<second>Salary is < than 10K</second>

Character entity

The character entities are the special symbols required in XML document. They are represented by Unicode character code as decimal or hexadecimal numbers. The complete number chart is available at http://www.unicode.org/charts/

Example with character entities

The (c) is represented as &#169; decimal value and &#x00A9; hexadecimal value in XML.

<?xml version="1.0" encoding="UTF-8"?>

DTD

<!DOCTYPE Base[

    <!ELEMENT Base (second)>  

    <!ELEMENT second (#PCDATA)>

    <!ENTITY less "&lt;">

    <!ENTITY copy "&#169;">

]>

XML

<Base>

    <second>Salary is is &less; than 10K. The &copy; of the book is usually held with author.</second>

</Base>

Output

            The © of the book is usually held with author.

General entity

The general entities are used to create reusable sections. A reusable section can specify the value of the entity or can refer to external file. When the replacement text is long it can be stored in text file and can be referred in DTD.

Example with general entities

<!ENTITY foot “Copy write of the article belongs to author”>

  • &foot; - This replaces the word foot to “Copy write of the article belongs to author” in the XML document, by the XMl parser.

The entities referring to external file.

<!ENTITY foot SYSTEM “MathMeasurement.txt”>

<!ENTITY foot PUBLIC “-//Maths Measurement//Length Section//EN” “Measure.txt”>

Parameter Entity

Like general entities, the parameter entites give replacement to a text body. However, they can be used as replacement only in DTD’s. The parameter entities help to give modular DTD’s and are useful with conditional sections.

Example with parameter entities

<!ENTITY % formOffice "INCLUDE">

<![ %formOffice;[

    <!ELEMENT form (name, officeName, area)>

]]>

Here %formOffice is the internal parameter entity. Using it with INCLUDE statement it defines the form element in DTD.

We can have an external parameter entity which refers to external ‘.dtd’ files:

Example of external parameter entity

<!ENTITY % foot SYSTEM “MathMeasurement.dtd”>

<!ENTITY % foot PUBLIC “-//Maths Measurement//Length Section//EN” “Measure.dtd”>

 

›› go to examples ››