Quackit Logo

XML Creating Entities

In the previous lesson, we learned what an entity is. We also saw the 5 predefined XML entities. In this lesson, we will learn how to create our own entities.

To create your own entity, you simply define the entity in your DTD. Once you've defined the entity in your DTD, you are ready to use it within your XML document.

Note: We will be covering DTDs later so don't be too concerned if you don't know what a DTD is yet.

Defining Your Entity

You define your entity using the <!ENTITY> element using the following syntax:

<!ENTITY name definition>

Here, name is the entity's name. definition is the entity's definition.

Using Your Entity

Whenever you need to use your entity, you simply write the entity's name, surrounded by an ampersand (&) and a semi-colon (&).

&entity_name;

Example

Imagine you have some text that you want to repeat at the bottom of all your documents. Let's say the text goes something like "Thank you, please come again!".

Instead of typing all that text out every time you need to use it, you could create an entity called say, "footer". That way, you just need to include &footer; whenever you want to include that text. Now, if you ever need to change the text you only need to change it in one place - the definition.

<?xml version="1.0"?>
<!DOCTYPE tutorials [
<!ENTITY footer "Thank you, please come again!">
<]>
<tutorials>
  <tutorial>
    <name>XML Tutorial</name>
    <url>http://www.quackit.com/xml/tutorial</url>
    <footer>&footer;</footer>
  </tutorial>
  <tutorial>
    <name>HTML Tutorial</name>
    <url>http://www.quackit.com/html/tutorial</url>
    <footer>&footer;</footer>
  </tutorial>
</tutorials>

Enjoy this website?

  1. Link to this page (copy/paste into your own website or blog):
  2. Add this page to your favorite social bookmarks sites:
                     
  3. Add this page to your Favorites

Oh, and thank you for supporting Quackit!