Sams Teach Yourself XML in 21 Days

Sams Teach Yourself XML in 21 Days

By Steven Holzner

Handling Entities

There's another type of item you can work with in XML documents—entities, which can be parsed or unparsed. An entity simply means a data item, such as a section of text or binary data. There are various ways to use entities, or to associate them with an XML document, as we'll be covering in the coming days, and it's appropriate to mention that now that we're discussing the parts of an XML document.

A parsed entity is one that you refer to with an entity reference. Entity references are replaced with the entities they refer to by the XML processor. There are two types of entity references: general entity references (starting with & and ending with ;) and parameter entity references (used in DTDs and starting with % and ending with ;). These references, such as one of the five predefined general entity references like ", will be replaced by the item the reference refers to. You can also specify characters with a general entity reference using the character's Unicode code—you saw that π is replaced by U041F.GIF by an XML processor, for example.

You can define your own general entity references, as you're going to see in Day 5. For example, you could assign the general entity reference &copyright; the text "(c)2005 Don't copy without permission.", and from then on, whenever you use &copyright; in your XML document, the XML processor will replace it with (c)2005 Don't copy without permission.

Unparsed entities can be binary data that you don't want parsed, or even non-XML text, and they're usually external to your XML document. You don't refer to an unparsed entity with an entity reference (which the XML processor will replace with the entity itself), but by a name. When you refer to an entity by name instead of with an explicit entity reference, that entity will not be parsed or placed into your XML document directly. We'll see how this works in Day 5.

More on all this is coming up—for the purposes of our present discussion on the structure of XML documents, however, what's important to note is that you can come across entity references in an XML document, and that it is possible to associate named external data, including binary data, with an XML document.

That's it—we've covered the items that can go into an XML document now, completing today's discussion. We're ready to start creating real XML documents now, which are well-formed documents (for an XML processor to read your documents, they must be well-formed). We're going to turn to that tomorrow.

Share ThisShare This

Informit Network