XML technologies / XML / XML in a Nutshell / XML basics / References

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

In character data within an element, opening angle brackets < must not appear unescaped, since the character is always interpreted as the start of a tag. To include it in text, use the entity reference &lt;, the numeric character reference &#60; or the hexadecimal character reference &#x3C;. When the parser reads the document, it replaces every&lt;, &#x60; or &#x3C; reference it finds with the actual character <. However, it does not mistake &lt; for the beginning of a tag. For example:

<SCRIPT LANGUAGE="JavaScript">
  if (location.host.toLowerCase( ).indexOf("ibiblio") &lt; 0) {
    location.href="http://ibiblio.org/xml/";}
</SCRIPT>

Character data must not contain an unescaped ampersand &. This character always starts an entity reference. You can represent it with this entity reference: &amp;

<firma>W. L. Gore &amp; Associates</firma> 

The ampersand has code point 38, so it can also be represented by the numeric character reference &#38;:

 <firma>W.L. Gore &#38; Associates</firma> 

Entity references such as &amp; and character references such as &#60; are markup. When an application parses XML, it replaces this markup with the character or characters referenced.

XML has exactly five predefined entity references. They are:

&lt;

the less-than sign, or opening angle bracket (<)

&amp;

the ampersand (&)

&gt;

the greater-than sign, or closing angle bracket (>)

&quot;

straight double quotation marks (")

&apos;

the apostrophe, or straight single quotation mark (')

Only &lt; and &amp; must be used instead of the actual characters in element content. The others are optional. &quot; and &apos; are useful inside attribute values, where " or ' could be mistaken for the end of the value. For example, this bild tag uses the entity reference &apos; to represent the apostrophe in O'Reilly:

<bild src='oreilly_koala3.gif' width='122' height='66' alt='Powered by O&apos;Reilly Books'/>

Although an unescaped greater-than sign > cannot incorrectly close a tag, &gt; is allowed mainly for symmetry with &lt;.

Note: There is one unusual case where the greater-than sign must be escaped. Character data must not contain the three-character sequence ]]>. Instead, write it as ]]&gt;.

Beyond the five predefined entity references, you can define more in the Document type definition. We discuss this further on the page General entity declarations.

Entity and character references occur only in element content and attribute values, not in element names, attribute names, or other markup. Text such as &amp; or &#60; may occur in a comment or processing instruction, but references are not resolved there. The parser replaces them only in element content and attribute values; elsewhere it does not recognize references.

  

 

 

 

back to top