Defining General Entities and Parameter Entities in DTDs

In DTDs, an entity allows you to define a symbol for a value. In the Tree view, you can define general entities and parameter entities. The value of a general entity can be just about anything. It can be

  • A short string that represents a longer string
  • A way to include another marked-up file
  • A reference to a graphical image
  • A placeholder for some non-XML data or an expression that needs special formatting

General entities are useful for things that change often, such as the name of a product in development. An entity allows you to change the value in one place and have the corrected value appear everywhere it is needed.

See also Description of Entity and Parameter Entity Properties in DTDs.

When you define a general entity, you specify a symbol that you can use in instance documents. When the XML parser finds a reference to a general entity, it replaces the symbol with the value you specified when you defined the general entity.

When you define a parameter entity, you specify a symbol that you can use elsewhere in the DTD. Again, when the XML parser finds a reference to a parameter entity, it replaces the reference with the value you specified when you defined the parameter entity.

In a DTD, the definition of an entity must appear before a reference to that entity. Therefore, it is good practice to put all entity declarations at the beginning of a DTD.

This section discusses the following topics:

Steps for Defining Entities in DTDs

The procedures for defining general entities and parameter entities are almost the same.

To define an entity in the Tree tab:
1. Click the DTD node.
2. In the left tool bar, click New Entity or New Parameter Entity .
3. Type the name of the new entity and press Enter. Stylus Studio displays the properties for the new entity in the Properties window. If the Properties window is not visible, select View > Properties from the Stylus Studio menu bar to display it.
4. In the Properties window, check the value of the Location property.

If you want to define the value for this entity in this DTD, the value should be Internal. Otherwise, the value should be External. If you need to change the value of the Location property, double-click its current value. In the drop-down menu that appears, double-click the new value.

5. If the value of the Location property is External, skip this step. If the value of the Location property is Internal, double-click the Value field and enter the value of the entity. Definition of your entity is complete. You do not perform the remaining steps in this procedure.
6. If the value of the Location property is External, specify a value for System ID. Double-click the field to enter a value. The value of System ID is a path to a file. It can be a URL or a file system path.

Although you can also specify a value for the Public ID property, Stylus Studio ignores any value you specify. A Public ID is a string that some parsers can resolve to an address, which they then use to locate a file. Stylus Studio does not have this capability.

7. If you are defining a parameter entity, you are done. If you are defining a general entity, check the value of the Parsed property. If necessary, double-click the value of the Parsed property to change it. The value of the Parsed property indicates whether the value of the entity is parsed XML. For example, if the entity refers to an image file, you do not want Stylus Studio to try to parse it.

General Entity Example in a DTD

Suppose you define the shopname general entity as an internal entity with the value Most Excellent Book Store of Tokyo. In the Text view of the DTD, this appears as follows:

<!ENTITY shopname "Most Excellent Book Store of Tokyo">
               

            

In an instance document, when the XML parser finds &shopname;, it replaces it with Most Excellent Book Store of Tokyo.

Parameter Entity Example in a DTD

Suppose you define the invoice parameter entity as an internal entity as follows:

<!ENTITY % customer "name, street, city, state, zipcode">
               

            

The percent sign ( %) after the ENTITY keyword indicates that this is a parameter entity. Later in the DTD, you can reference this parameter entity as follows:

<!ELEMENT invoice (%customer;, item, price, date)>
               

            

When this DTD is processed, it is as if you had specified the following:

<!ELEMENT invoice (name, street, city, state, zipcode, item, price, date)>
               

            

 
Free Stylus Studio XML Training: