Lesson 5 | Parameter 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.
To create entities in a Document Type Definition (DTD) for use within XML documents, you use the declaration to define reusable content, such as text, markup, or external data. Entities act as placeholders that can be referenced in the XML document or DTD, promoting modularity and consistency. Below are the steps and examples for creating different types of entities in a DTD:
-
Internal Entity:
-
External Entity:
- Parameter Entity:
Best Practices:
- Ensure entity names are unique and follow XML naming rules (e.g., no spaces, start with a letter).
- Avoid circular references, as they cause parsing errors (e.g., an entity referencing itself).
- Validate external entity content to ensure itâs well-formed and compatible with the XML structure.
- Test entity references in the XML document to confirm correct replacement during parsing.
Example DTD with Multiple Entities:
<!ENTITY company "Acme Corp">
<!ENTITY chapter SYSTEM "chapter1.xml">
<!ENTITY % commonAttrs "id ID #IMPLIED">
<!ELEMENT document (title, content)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT content (#PCDATA | chapter)*>
<!ATTLIST document %commonAttrs;>
In the XML:
<document id="doc1">
<title>About &company;</title>
<content>&chapter;</content>
</document>
By defining entities in the DTD, you can streamline XML document creation, ensure consistency, and make maintenance easier. Internal entities are great for small, repeated text; external entities suit large or dynamic content; and parameter entities enhance DTD modularity. Always validate the DTD and XML to ensure proper parsing and functionality.
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 several
different 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.
Notation Declarations in XML
A
notation declaration references a non-XML resource and specifies instructions for that resource in a non-XML context.
For example, if you want to include a special media type, you would add a notation declaration that specifies the media type and perhaps an application on the system that could be used to render that media. The following code shows a fictitious notation declaration for the GIF image file type. In this example, the fictitious file name
gifview.exe
represents a file that enables the viewing of GIF images.
<!NOTATION GIF SYSTEM "gifview.exe">
The following entity declaration references non-XML content:
<!ENTITY BulletGif SYSTEM "bullet1.gif" NDATA GIF>
In this example, an entity referenced as
BulletGif
has been declared to reference the file
"bullet1.gif"
on the current system as non-character data
(NDATA)
and of the
GIF
type.
If a notation for
GIF
appears in the file, the application can choose whether or not to use the notation reference for rendering that file.
Parameter Entities - Exercise
