Open In App

DTD Entities

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax for DTD Entities:

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

1. Internal Entity: entity-name is the name of the entity and entity-value can be defined manually.

<!ENTITY entity-name "entity-value">

2. External Entity: That External file can hold defined information that can be used using the &Entity_name in the Tags.

<!ENTITY entity_name SYSTEM "file:///external_file.xml">

Types of entities:

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

1. Built-in Entities: These entities are predefined and cannot be redefined in the document. They are always available to use without the need to define them in a document, and they have special meaning in XML, which ensures proper parsing. There are five built-in entities to frame an XML document:

  • Ampersand (&): &amp
  • Less than (<): &lt
  • Greater than (>): &gt
  • Double quote ():
  • Single quote (): &apos

XML




<!DOCTYPE book [
  <!ELEMENT book (title, author)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
]>
<book>
  <title>Computer science & Engineering </title>
  <title>Computer science < Engineering </title>
  <title>Computer science > Engineering </title>
  <title>Computer science " Engineering </title>
  <title>Computer science ' Engineering </title>
</book>


Output:

Ampersand (&)

2. Character Entities: These entities are used to define a single character that cannot be typed easily using the characters. Character Entities are defined in the DTD and used in XML Documents. For example, there are symbols like rupee, copyright, trademark, etc.

XML




<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<!DOCTYPE character[
   <!ELEMENT symbols (#PCDATA)>
   <!ELEMENT symbol (#PCDATA)>
   <!ENTITY copyright "©">
   <!ENTITY rupee_symbol "₹">
   <!ENTITY trade_mark "®">
     
]>
<symbols>
    <symbol>copyright symbol: ©right;</symbol>
    <symbol>rupee symbol: &rupee_symbol;</symbol>
    <symbol>trade_mark symbol: &trade_mark;</symbol>
</symbols>


Output:

Character Entities

3. General Entities: These entities are used to define pieces of text or structured data in DTD and that can be used multiple times in Document. General Entities are mostly used to define abbreviations.

XML




<?xml version = "1.0"?>
  
<!DOCTYPE general_entities [
   <!ENTITY text "GeeksForGeeks">
]>
  
<note>
   &text;
</note>


Output:

General Entities

4. Parameter Entities: These entities are used to define variables that can be used in the other entities. Parameter entities are referred to other entities using the %(percent) sign.

XML




<?xml version="1.0"?>
<!DOCTYPE library [
  <!ELEMENT book (title, author, publication)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT publication (publisher, year)>
  <!ELEMENT publisher (#PCDATA)>
  <!ELEMENT year (#PCDATA)>
  
  <!ENTITY % basicElements "(title, author, publication)">
]>
  
<library>
  <book>
    <title>Introduction to XML</title>
    <author>yogesh poul</author>
    <publication>
      <publisher>GeeksForGeeks</publisher>
      <year>2023</year>
    </publication>
  </book>
</library>


Output:

Parameter Entities

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

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:

External Entity

Internal Entity Example output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads