Wednesday 1 December 2010

Sixth Task - Discuss the roles of entities in XML, giving examples what problems can occur when they are not used properly

Within XML, entities can be classified underneath three dissimilar categories with different functionalities. Below are the specific entity definitions and their purpose in the XML, and all  will be explained, entities are classified in three different ways in XML and here they are.

  • Internal Entities
  • External Entities
  • Parameter Entities 

1) Internal Entities

Internal entities function as shortcuts or macros. With internal entities, you can associate an essentially arbitrary piece of text (which may have other markup, including references to other entities) with a name. The text that is inserted by an entity reference is called the replacement text.

The replacement text of an internal entity can contain markup elements, attributes, processing instructions, other entity references, and so on, but the content must be balanced any element that is started in an entity must end in the same entity) and recursive entity references are not allowed. There is no separate physical storage object, and the content of the entity is given in the declaration. Internal entities can include references to other internal entities, but it is an error for them to be recursive.

Thus Innternal entites are important from a processing point of view. Every conforming XML processor is required to do internal entity processing, if the entities are declared in the internal DTD subset.

Predefined Entities

There are five predefined internal entities in XML, as shown in the table . The < and & characters in the declarations of "lt" and "amp" are doubly escaped to meet the requirement that entity replacement be well-formed.

Entity Entity Name Replacement Text
The left angle bracket (<) lt &#38;#60;
The right angle bracket (>) gt &#62;
The ampersand (&) amp &#38;#38;
The single quote or apostrophe (') apos &#39;
The double quote (") quot &#34;

2) External Entities

External entities are all those that are not internal. They can consist of more than a single element, such as, an external entity consisting of some character data with embedded inline markup. The tags in an external entity must be well balanced within the entity you can not start a tag in an entity and end it in your document or in another entity. External entities can reference internal or other external entities, but must not contain a recursive reference to itself, either directly or indirectly.

The same external entity can be referred several times in a single document i.e if the document is to be validated, however, ID attributes should not be used in the external entity. in other words you can have several external entities that all refer to the same external file. There are no additional restrictions placed on the character encodings used by external entities.

In particular, external entities with different encodings can be used in the same document. Consequently external entities, like internal entities, have names and are referenced in the same manner, although they are declared differently, which serves as one way of recognizing them.

Uses of External General Entities

The following are some primary uses of external general entities:
  • External entities allow an XML document to incorporate content from external files. This provides a mechanism for dividing a document up into logical  well (chunks) if may say, each of which can be stored in a separate file and external entities can be used to "refer" to them.
  • When the parent file is parsed by an XML processor, it will have the effect of inserting the contents of each of the individual files at that location of the respective entity references.
  • External entities can contain binary data, which can be used to reference images and other non-XML content in the document. 
3) Parameter Entities

Parameter entities are only available within the internal and external subsets of the DTD. They can be either internal or external, but they can not refer to non-XML data.
 
 Uses of Parameter Entities

Parameter entities are most frequently used to customize and extend DTDs. Definition of variables for use inside the DTD (parameter entities). This is used to modularize a DTD for reuse and ease of maintenance.
 
Declaring Entities

Internal Entity declaration

All internal entities must be declared in the internal or external DTD subsets. Entity references should follow their declaration in the source. At the time of being  processed and parsed, an entity reference at a given point in an XML document instance triggers the substitution of its contents at that point. Entity definitions can themselves refer to other internal and previously defined entities. 

You create internal entities with entity declarations in the internal subset or the DTD. thus It is an error to insert an entity reference to an unparsed entity directly into the flow of an XML document. Unparsed entities can only be used as attribute values on elements with ENTITY attributes.

Internal general entity declarations, which can occur only in the DTD, have the following form:

<!ENTITY entityname "replacement text">
External Entity Declaration 

Declaring External Entities

For External entity declarations come in two  given forms. If the external entity contains XML text, the declaration has the following form:
 
<!ENTITY entityname [PUBLIC "public-identifier"] SYSTEM "system-identifier">



Reference: http://www.w3.org/TR/2004/REC-xml-20040204/

No comments:

Post a Comment