Entities

Built-in entities

We have already seen the five built-in character entities. They are:

  • &  &
  • &lt;   <
  • &gt;   >
  • &apos; '
  • &quot; "

Character entities

We have also seen character entities. Character entities are characters specified by their decimal or hexadecimal numeric value. Most UTF-8 characters work. Examples:

  • &#x00A9; is ©   the copyrignt symbol.
  • &#246; is ö in German.
  • &#233; is é in French.
  • &#241; is ñ in Spanish.
  • &#x13E3;&#x13B3;&#x13A9; These characters show up as: ᏣᎳᎩ rather than the characters tsa la gi in Cherokee.

Most western european letters, and many other UTF-8 symbols work in XML 1.0. Others do not. The UTF-8 Cherokee characters are not in the XML 1.0 standard, and are not in the character set in my computer, so they do not show up.

General entities

single character

You can build your own entity, which you can then use like a built-in entity or a character entity.
You declare your own entity by using the <!ENTITY> element in your DTD.

You can build your own entity, containing any character you wish.

In the source of the example to the left, you can see:
<!ENTITY first "I" >
This means that you can code &first; in your XML document, and get the letter I.

multiple characters

You can build your own entity, containing a string of multiple characters.

In the source of the example to the left, you can see:
<!ENTITY fred "Frederick I, Duke of Swabia, King of Germany, King of Italy, Holy Roman Emperor, and King of Burgundy" >
This means that you can code &fred; in your XML document, and get all this text.

single element

You can build your own entity, containing an element.
Note:   Be sure to include the complete element, including its opening AND closing tags.

In the source of the example to the left, you can see:
<!ENTITY fred "<first>Fredrick</first>" >
This means that you can code &fred; in your XML document, and get a compete XML   first   element, inculding its enclosed text.

multiple elements

You can build your own entity containing more than one element.
Note:   Be sure to include complete elements, including their opening AND closing tags.
You can include elements that contain other elements. You can mix elements and text.

In the source of the example to the left, you can see:
<!ENTITY jones "<first>Fredrick</first><last>Jones</last>" >
This means that you can code &jones; in your XML document, and get all this stuff.

external

If you have a very large amount of text, or a large complex set of element, you may wish to put it in an external file. On the other hand, having the data spread over multiple files slows down the whole process of interpretation, and makes the interpretation more complex.

In the source of the example to the left, you can see:
<!ENTITY jones "<fred SYSTEM "fred.txt" >
SYSTEM means to look for the file locally. In this case the external file is fred.txt within the current directory. (You could use PUBLIC data.)

I had some difficulty getting this example to work on my machine. My security settings would not allow it in Firefox. I was able to bypass some of the security, by responding to the message in the information bar in Internet Explorer.

where you can use entities

text

A built-in entity provides one character of text.
A character entity also provides one character of text.
Some general entities provide only a string of text.

These entities can be used anywhere in the XML document, where text is allowed within an element or attribute value.
They can only be used in a DTD when XML text is explicitly given. This inculdes default and fixed values for an attribute value.
They cannot be used elsewhere, to specify part of the XML or DTD structure.

elements

Some general entities provide XML elements.
Some general entities provide XML elements, and also text.

The entities containing elements can be used anywhere in the XML document, where the elements are allowed.
The entities containing elements and text can be used anywhere in the XML document, where the elements and text is allowed.
They can not be used in a DTD.
They cannot be used elsewhere, to specify part of the XML or DTD structure.

entities within entities

You can use entities within the text in an entity, because this is text that will be put into the XML document. This can easily become complex.

parameter entities

In the next section, we will look at parameter entities.

Unlike the other kinds of entities, parameter entities must be used only within the DTD.
They can NOT be used within the XML document.

parameter

parameter

parameter entities are different than other entities.

  • They use a percent sign (%) rether than an ampersand (&) to start the parameter.
  • They are used within a DTD, not in an XML document.

The link to the left provides a discussion and examples.