Examples of Internal and External Entity

1. Internal Entity:

XML




<!DOCTYPE library [
  <!ENTITY bookTitle "Introduction to XML">
]>
<library>
  <book>&bookTitle;</book>
</library>


Output:

Internal Entity Example output

2. External Entity:

XML




<!DOCTYPE book [
  <!ELEMENT book (title, author)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
]>
<book>
  <title>Advanced XML Parsing</title>
  <author>John Doe</author>
</book>


XML




//main_DTD.xml
<!DOCTYPE library [
  <!ENTITY book SYSTEM "file:///book.xml">
]>
<library>
  &book;
</library>


Output:

Internal Entity Example output



DTD Entities

When you are writing an XML document, pieces of information need to be used several times. At that time, you can use XML entities to store that piece of information to reuse it again and again by referring to it, avoiding writing that information multiple times. For example, if you have a default introduction, name, address, text, data, etc. that is commonly used, you should use an entity while writing a script of XML documents. In a nutshell, XML entities are used to define reusable content or pieces of data. Using entities in XML documents makes them more organized and maintainable.

Variables in programming languages and entities in XML represent values. However, both variables and entities work very differently in programming languages and serve different purposes.

Similar Reads

Syntax for DTD Entities:

There are two methods to define the syntax for DTD Entities:...

Types of entities:

There are various types of DTD entities used to define special characters in documents....

Examples of Internal and External Entity:

...