Parameter Entities

The final type of entity is a special type called a parameter entity. Parameter entities differ from regular entities (which are also known as General Entities) because they can be used only inside the DTD. Parameter entities have no meaning inside the XML document—they are simply a way for you to group data within a DTD for the sake of convenience.

For example, let's say that we have a catalog with a number of clothing items, as elements, such as shirt, pants, skirt, and so on. Now, each one of these elements is going to have the same attributes, so we could type all the attributes in by hand, or we could use a parameter entity.

Parameter entities are declared in the DTD, similar to regular entities, with one important difference:

<!ENTITY % name "Value"> 

You will notice there is a percent sign % inserted between the ENTITY keyword and the name. That is what denotes this as a parameter entity. Parameter entities are used in the document with

%name; 

Also, parameter entities must be defined before they are used in the DTD, so they are usually defined at the top of the DTD.

So, let's take a look at our sample DTD:

<!ENTITY % common " 
   size (small | medium | large) 'medium'
   color (red | blue | green | black | white) "'white'
   price   CDATA   #REQUIRED">

<!ELEMENT shirt (#PCDATA)>
<!ELEMENT pants (#PCDATA)>
<!ELEMENT skirt (#PCDATA)>

<!ATTLIST shirt
   %common;>

<!ATTLIST pants
   %common;>

<!ATTLIST skirt
   %common;>

The first step is to define a parameter entity, which is actually the definition of our common attributes for each element:

<!ENTITY % common " 
   size (small | medium | large) 'medium'
   color (red | blue | green | black | white) 'white'
   price   CDATA   #REQUIRED">

Then, we declare the elements themselves, followed by the ATTLIST declarations, which is where we use the parameter entity:

<!ATTLIST shirt 
   %common;>

When the XML parser reads this, the entity is substituted, and the parser reads the ATTLIST as

<!ATTLIST shirt 
   size (small | medium | large) 'medium'
   color (red | blue | green | black | white) 'white'
   price   CDATA   #REQUIRED>

and so on for the other elements' ATTLISTs as well. As you can see, this saves a lot of typing of redundant information, which is exactly what parameter entities are useful for.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.152.139