DTD - Entities

In DTD (Document Type Definition), entities are used to define and reference reusable pieces of text or character data within an XML document. They provide a way to define and manage entities, making it easier to handle repetitive or commonly used content.

Internal Entities:

Internal entities are defined within the DTD itself using the <!ENTITY> declaration. They are typically used for defining simple text or character data. Here's an example:

<!ENTITY companyName "ABC Corporation">
<!ENTITY emailSuffix "@example.com">

In this example, two internal entities are defined. The companyName entity represents the text "ABC Corporation", and the emailSuffix entity represents the text "@example.com". These entities can be referenced within the XML document using the entity reference syntax &entityName;.

External Entities:

External entities are defined in a separate file and referenced within the DTD. They are commonly used for referencing large or complex pieces of content that are stored externally. 

DTD file:

<!ENTITY footer SYSTEM "footer.txt">

External file (footer.txt):

This is the footer text.

In this example, the external entity footer is defined in the DTD file, and it references an external file called "footer.txt". The content of the external file, "This is the footer text.", will be inserted into the XML document wherever the footer entity is referenced.

Entities can be referenced within the XML document using the entity reference syntax &entityName;. For example:

<!DOCTYPE rootElement [
    <!ENTITY companyName "ABC Corporation">
]>
<rootElement>
    <company>&companyName;</company>
</rootElement>

In this example, the entity companyName is referenced within the XML document using &companyName;. When the document is parsed, the entity reference will be replaced with the corresponding entity value, resulting in <company>ABC Corporation</company>.