Home > Articles > Web Services > XML

Using XML Entities

  • Print
  • + Share This
XML entities are a primitive macro facility allowing commonly repeated sequences of document content and even DTD declarations to be defined and referenced by name. This excerpt from Strategic XML describes how to make good use of the power of entities.
This article is excerpted from Strategic XML by W. Scott Means (Sams, 2001, ISBN 0-672-32175-0).
From the author of

From the author of

Introduction

When you're authoring complex documents or creating multiple related documents, certain content fragments tend to be repeated over and over again. For example, a series of XHTML web pages might share a set of common footer tags. Entities are provided as a primitive macro facility to allow commonly repeated sequences of document content and even DTD declarations to be defined and referenced by name.

Listing 1 shows a simple entity declaration and reference.

Listing 1—A Simple Entity Example

<!DOCTYPE message [
 <!ELEMENT message (#PCDATA)>
 <!ENTITY my_message "Hello, world." >
]>
<message>&my_message;</message>

First, the entity is declared using the <!ENTITY> markup, and then the associated text ("Hello, world.") is included in the target document, using a normal entity reference (&my_message;). The actual syntax of entity declarations and references changes based on the following:

  • Where the replacement text comes from (an inline string or an external file)

  • Where the entity will be referenced (within the DTD or the document)

  • Whether the resulting replacement text will be read by the parser

  • + Share This
  • 🔖 Save To Your Account