Open In App

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.



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:




<!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:

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 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: &copyright;</symbol>
    <symbol>rupee symbol: &rupee_symbol;</symbol>
    <symbol>trade_mark symbol: &trade_mark;</symbol>
</symbols>

Output:

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 version = "1.0"?>
  
<!DOCTYPE general_entities [
   <!ENTITY text "GeeksForGeeks">
]>
  
<note>
   &text;
</note>

Output:

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 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:

Examples of Internal and External Entity:

1. Internal Entity:




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

Output:

Internal Entity Example output

2. External Entity:




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




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

Output:

Internal Entity Example output


Article Tags :