Examples of Internal and External Entity
1. Internal Entity:
XML
<!DOCTYPE library [ <!ENTITY bookTitle "Introduction to XML"> ]> < library > < book >&bookTitle;</ book > </ library > |
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:
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.