Web Services, Part VIII: Reading DTDs with JavaScript: Defining Entity References - Doc JavaScript
Web Services, Part VIII: Reading DTDs with JavaScript
Defining Entity References
The second reason to have a DTD is that it defines entities that you can reference in the XML file. It's like having parameters in the XML file that you want to replace with real values, only during the loading of the XML file. Sometimes, you don't want to commit to a value ahead of time. Entity references allows you to defer the value substitution until you load the XML file. It also allows you to easily change values without modifying the XML file; you just need to change the DTD file.
An entity reference is a group of characters used in text as a substitute for another group of character. There are several types of entity references. The most common is the single character entity reference, where a group of characters is used as a substitute for a single specific character that is also a markup delimiter in XML. For example, if an attribute must contain the ampersand sign (&
), you can substitute the entity reference &
. Entity references always begin with an ampersand (&
) and end with a semicolon (;
). The five characters are:
&
is referenced by&
<
is referenced by<
>
is referenced by>
"
is referenced by"
'
is referenced by'
The second type of entity reference is that of user-defined unlimited-length references. Peek again at our mydvd
XML file in Page 2. The description
element is free text, showing the months of the year this sales report is for:
<description>Sales Report for January, February, and <&month;> of 2001</description>
Two months, January
and February
, are written explicitly. The third month is written as an entity reference, &month;
. You need to get the value of this entity reference from the DTD file. If you look at the DTD listing in Page 2, you can see the relevant ENTITY
statement:
<!ENTITY month "April">
Notice that we put <
to the left of &month;
and >
to its right. These additions are not related to &month;
. We'll use them for another example, later in this column.
Another example for entity reference is shown in the author
element of mydvd
:
<author>author: &preparedby;</author>
The entity reference &preparedby;
needs to be substituted by a value from the DTD. Here is how we define it:
<!ENTITY preparedby "John Smith">
Next: How to load DTDs with a Browser
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: February 11, 2002
Revised: February 11, 2002
URL: https://www.webreference.com/js/column103/4.html