The entities which can be read by XML parser are called parsed entities. The entities can be of two types: internal or external.

Internal entities

If an entity is defined and used within a file it is internal. Both the general and parameter entities can be defined as internal entity.

General internal entity

General internal entities are referenced in the DTD and can be used anywhere and anytime within the document. 

Syntax for general internal entity

<!ENTITY entityName “Entity Value”>

The rules to use general entity are:

  • One general entity cannot be used within other general entity as circular reference.

Example that doesn't work:

<!ENTITY author “Ruskin Bond &book;”>

<!ENTITY book “Lamp is Lit by &author;”>

  • They cannot have any of the three characters as values in them:  & ; %

Example of general internal entity

<?xml version="1.0" standalone=”yes”?>

<!DOCTYPE root[

    <!ELEMENT root (line,lang)>

    <!ELEMENT line ANY>

    <!ENTITY film "Harry Potter series">

]>

 

<root>         

      <!—Entity reference -->

    <line>Favourite cinema is &film;</line>    

</root>

Output for the example above will be:

<root>

<line>Favourite cinema is Harry Potter series</line>

</root>

Parameter internal entity

The parameter entities are useful for having entity references in DTD. They cannot be referenced in the content of the document. They begin with % instead of & as in general entity, and can be used only in DTD.

Syntax for parameter internal entity

<!ENTITY % entityName “Entity Value”>

Example of parameter internal entity

<?xml version='1.0'?>

<!DOCTYPE root [

    <!ELEMENT root (data)>

    <!ENTITY % text "<!ELEMENT entry (author, book, award)>">

    %text;

    <!ELEMENT author (#PCDATA)>

    <!ELEMENT book (#PCDATA)>

    <!ELEMENT award (#PCDATA)>   

    <!ELEMENT data (text)*>

]>

 <root>

    <data>

        <text>

        <author>P C Tejaswi</author>

        <book>Karvaalo</book>

        <award>State Academy</award>

        </text>  

 

            <text>

        <author>Kuvempu</author>

        <book>Ramayana Darshanam</book>

        <award>Gnanapeeta</award>

        </text> 

    </data>

</root>

Output for the example above will be:

<?xml version="1.0" standalone="no" ?>

<!DOCTYPE document (View Source for full doctype...)>

<root>

    <data>

        <text>

        <author>P C Tejaswi</author>

        <book>Karvaalo</book>

        <award>State Academy</award>

        </text>  

 

            <text>

        <author>Kuvempu</author>

        <book>Ramayana Darshanam</book>

        <award>Gnanapeeta</award>

        </text> 

    </data>

</root>

External entities

Entities defined in DTD, outside the XML document are called external entities. The external document can have a single or a group of owners and references using the SYSTEM keyword. This is called private external entity; else it can have access to larger public and can be accessed by using the PUBLIC keyword. The general and parameter entities both can be defined externally.

General external entity

XML is a flexible language and the data can be added from multiple sources and document by using the external entity. A well-formed one or more XML documents can be added together. The names of the external documents to be added are declared in DTD.

Syntax for general external entity

<!ENTITY  %entityName SYSTEM “uri”>

<!ENTITY  %entityName PUBLIC “uri”>

Example of general external entity

Sign.xml file:

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

<footer>

<author>P C Tejaswi</author>

<book>Karvaalo</book>

</footer>

Main.xml

<?xml version="1.0" standalone="no" ?>

<!DOCTYPE document [

<!ELEMENT document (genre, footer)>

<!ELEMENT genre (#PCDATA)>

<!ELEMENT author (#PCDATA)>

<!ELEMENT book (#PCDATA)>

<!ELEMENT footer (author, book)>

<!ENTITY foot SYSTEM "sign.xml">

]>

<document>

<genre>Non-Fiction</genre>

&foot;

</document>

Output for the example above will be:

<?xml version="1.0" standalone="no" ?>

<!DOCTYPE document (View Source for full doctype...)>

<document>

        <genre>Non-Fiction</genre>

        <author>P C Tejaswi</author>

<book>Karvaalo</book>

</document>

Parameter external entity

The parameter external entities are used to refer to external DTD’s in current document. It helps to build large DTD from multiple smaller DTD files. This reduces the retyping of code and change at one place is reflected in many places. It also helps in mixing and reuse of DTDs. Parameter external entity supports both private and public DTD files and hence SYSTEM or PUBLIC keywords can be used respectively to link DTD files.

Syntax for parameter external entity

The syntax below links file1.dtd to file2.dtd. Similarly any number of dtd’s can be linked and used in the xml document.

File1.dtd

<!ENTITY root1 (element1, element2)>

<!ELEMENT element1 (#PCDATA)>

<!ELEMENT element2 (#PCDATA)>

File2.dtd

<!ELEMENT root2 (element3, element4)>

<!ELEMENT element1 (#PCDATA)>

<!ELEMENT element2 (#PCDATA)>

<!ENTITY % addDTD SYSTEM “file1.dtd”>

%addDTD;

Example of parameter external entity

externalPE.dtd

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

<!ELEMENT footer (film, director)>

<!ELEMENT film (#PCDATA)>

<!ELEMENT director (#PCDATA)>

autograph.xml

<?xml version="1.0" standalone="no" ?>

<!DOCTYPE document [

    <!ELEMENT document (genre, footer*)>

     <!ELEMENT genre (#PCDATA)>

    <!ENTITY % foot SYSTEM "externalPE.dtd">

    %foot;

]>

 

<document>

    <genre>Fiction</genre>

    <footer>

        <film>Fellowship of the Ring</film>

        <director>Peter Jackson</director>

    </footer>

</document>

Output for the example above will be:

<?xml version="1.0" standalone="no" ?>

<!DOCTYPE document (View Source for full doctype...)>

<document>

  <genre>Fiction</genre>

  <footer>

  <film>Fellowship of the Ring</film>

  <director>Peter Jackson</director>

  </footer>

</document>

 

›› go to examples ››