Use Elements Instead of Entities to Avoid the “amp Explosion Problem”

Use replaceable elements as a solution to the “amp explosion problem."

Search for the string & using your favorite search engine. Then search for the string & and then & and so on. You will get lots of hits and see lots of interesting text. Here are some examples I found:

  • Why Choose Auto & Home Insurance

  • poésie, nouvelles, théâtre

These strange incantations can be traced back to the entity structure of XML (and SGML before it). Simply put, XML provides a number of ways in which textual units, known as entities [Hack #25] , can be spliced into other textual units by an XML parser. The mechanism involves referring to these entities by name. The name is preceded by an ampersand character and followed by a semicolon.

Some of these entities are built into XML itself and thus are built into every XML parser. The five built-in entities (see Table 7-1) provide ways of encoding characters that would otherwise have special meaning to an XML parser because of their roles in markup .

Table 7-1. XML predefined entities

Entity reference

Description

<

Less-than sign (<)

&gt;

Greater-than sign (>)

&apos;

Apostrophe (')

&quot;

Quotation mark (“)

&amp;

Ampersand (&)

The troublesome entity here is the ampersand. Note that the escaped version of it features an ampersand character—the very character we are ...

Get XML Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.