Attributes Entitites   «Prev  Next»

Lesson 5Parameter entities
Objective Create entities to use within a DTD

Create entities to use within a DTD

You can create entities to use within the DTD itself. These are parameter entities. To denote that an entity is to be used as a parameter entity, you must include the % character in the manner indicated.

Declaring a parameter entity

The syntax for declaring a parameter entity is as follows:
<!ENTITY %parameterEntityName parameterEntityDefinition > 

Using parameter entities


Suppose that you need to create an attribute-list declaration to be used for severaldifferent elements. Rather than define each element with the full listing of attributes, you can create an attribute-list entity and then reference it for each of the several elements. In the following example, three elements have the same three attributes: ID, MAKE and MODEL.
These three attributes are defined once in a parameter entity (commonAtts),and then used in the attribute-list declaration for each of the elements.

<!ENTITY % commonAtts
"ID ID #REQUIRED 
MAKE CDATA #IMPLIED
MODEL CDATA #IMPLIED"> 
<!ELEMENT CAR (#PCDATA)>
<!ATTLIST CAR %commonAtts>
<!ELEMENT COMPUTER (#PCDATA)>
<!ATTLIST COMPUTER %commonAtts>
<!ELEMENT MODEL (#PCDATA)>
<!ATTLIST MODEL %commonAtts> 

In these declarations, each element of CAR, COMPUTER and MODEL shares the same common set of attributes defined by the parameter entity commonAtts.
As you may have noticed, the order in which you declare elements and attributes does not matter, so long as you accurately declare all that you will use before the XML document loads. Within an element declaration, the order of child elements matters, but the actual order of element declarations or attribute declarations does not matter.
In the next lesson, you will learn how to use namespaces to establish clear naming conventions.

parameter-entities-exercise

Click the Exercise link below to practice including entities and parameters in the DTD.
Parameter Entities - Exercise