w3resource

XML Entity

What is an Entity

Before we read a definition, consider the following html code

<html>
<head>
<title>XML Entity Example</title>
</head>
<body>
<p>An xml&nbsp; tag starts with &lt; and ends with &gt;</p>
</body>
</html>

Click on this link to open this in a browser.

  • You can see an output like this : An xml  tag starts with < and ends with >
  • Now notice that greater than (>) and less than (<) signs are written as &lt; and &gt; in the html code above. These are examples of the entity.
  • Like html, xml also has entities and widely used to reap several advantages while creating an xml document.
  • By specification, XML documents consist of a set of storage units. These storage units are called Entities.
  • Entities act as a replacement mechanism. A similar kind of example of using entities is, creating and attaching mail-merge to Microsoft Word documents. We create a database of names and address and attach them to an MS Word document.
  • To explain in another note, we can say that Entities can be used as a kind of shortcut that allows you to embed blocks of text or even entire documents and files into an XML document. This makes updating documents across networks very easy.

Use of Entities in xml document

  • Denoting special markup, such as the > and < tags.
  • Managing binary files and other data not native to XML.
  • Reducing the code in DTD by bundling declarations into entities.
  • Offering richer multilingual support.
  • Repeating frequently used names in a way that guarantees consistency in spelling and use.
  • Providing for easier updates. By using entities in your markup for items you know will be changed later-such as weather reports or software version changes-you greatly improve dynamic document automation.
  • Merging multiple file links and interaction.

Rules for using legal Entity Markup

  • The entity must be declared in the DTD. If you are using an XML document which is not validated against a DTD or schema, then you have to declare one within the xml document itself and this must consist the entity you are using.
  • A general entity is referenced within an xml document must be surrounded by an ampersand (&) on one end and the semicolon (;) on the other (&myEntity;).
  • The name of an entity must begin with a letter or underscore (_) but can contain letters, underscores, whole numbers, colons, periods and/or hyphens.
  • An entity declaration cannot consist of markup that begins in the entity declaration and ends outside of it .
  • A parameter entity must be declared with a preceding percent sign (%) with a white space before and after the percent sign, and it must be referenced by a percent sing with no trailing white space. A typical parameter entity declaration looks like this: <!ENTITY % myParameterEntity "myElement">

Types of entities

In general, we have three types of entities: internal entities, external entities, and parameter entities.

Internal Entities

These are entities that refer to entities whose definitions can be found entirely within a document's DTD.

External Entities

These are entity references that refer to entities whose definitions can be found outside of a document.

Parameter Entities

These are available within internal or external  subsets of DTD.

In the subsequent pages, we have discussed each of these entities in detail.

Besides the type of Entities mentioned above, we find it necessary to discuss the Document Entity of an xml document to be discussed specially.

The document entity is the most important entity in an xml document and is actually one of only two kinds of entities that are allowed to exist without having  a name assigned to them (the another one is the external subset of the DTD ). This entity is the first thing the xml processor encounters when parsing a document. It is also referred to as the document root, and it provides  programmatic access to the rest of the document. The reason the document entity is important is that, at the end of the day, it's the only thing the xml specifications  requires an xml parser to read.

Document entities are defined as xml documents are parsed before it has been used. 

Previous: XML declarations
Next: XML Internal Entities



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/xml/entities.php