Main Page

Previous Page
Next Page

[Page 1338]

J.7. Special Characters and More Line Breaks

When marking up text, certain characters or symbols (e.g., <) might be difficult to embed directly into an XHTML document. Some keyboards do not provide these symbols, or the presence of these symbols could cause syntax errors. For example, the markup

<p>if x < 10 then increment x by 1</p>

results in a syntax error, because it uses the less-than character (<), which is reserved for start tags and end tags such as <p> and </p>. XHTML provides special characters or entity references (in the form &code;) for representing these characters. We could correct the previous line by writing

<p>if x &lt; 10 then increment x by 1</p>

which uses the special character &lt; for the less-than symbol.

Figure J.7 demonstrates how to use special characters in an XHTML document. For a list of special characters, see Appendix K. Lines 2627 contain other special characters, which are expressed either as word abbreviations (e.g., &amp for ampersand and &copy for copyright) or as hexadecimal values (e.g., &#38; is the hexadecimal representation of &amp;). Hexadecimal numbers are base-16 numbersdigits in a hexadecimal number have values from 0 to 15 (a total of 16 different values). The letters AF represent the hexadecimal digits corresponding to decimal values 1015. Thus, in hexadecimal notation, we can have numbers like 876 consisting solely of decimal-like digits, numbers like DA19F consisting of digits and letters, and numbers like DCB consisting solely of letters. We discuss hexadecimal numbers in detail in Appendix D.

Figure J.7. Inserting special characters into XHTML.
(This item is displayed on pages 1338 - 1339 in the print version)

 1  <?xml version = "1.0"?>
 2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 3     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 5  <!-- Fig. J.7: contact2.html       -->
 6  <!-- Inserting special characters. -->
 8  <html xmlns = "http://www.w3.org/1999/xhtml">
 9     <head>
10        <title>Inserting special characters</title>
11     </head>
13     <body>
15        <!-- special characters are    -->
16        <!-- entered using form &code; -->
17        <p>
18           Click
19           <a href = "mailto:deitel@deitel.com">here
20           </a> to open an e-mail message addressed to
21           deitel@deitel.com.
22        </p>
24        <hr /> <!-- inserts a horizontal rule -->
26        <p>All information on this site is <strong>&copy;</strong>
27           Deitel <strong>&amp;</strong> Associates, Inc. 2005.</p>
29        <!-- to strike through text use <del> tags   -->
30        <!-- to subscript text use <sub> tags        -->
31        <!-- to superscript text use <sup> tags      -->
32        <!-- these tags are nested inside other tags -->
33        <p><del>You may download 3.14 x 10<sup>2</sup>
34           characters worth of information from this site.</del>
35           Only <sub>one</sub> download per hour is permitted.</p>
37        <p>Note: <strong>&lt; &frac14;</strong> of the information
38           presented here is updated daily.</p>
40     </body>
41  </html>

In lines 3335, we introduce three new elements. Most browsers render the del element as strike-through text. With this format, users can easily indicate document revisions. To superscript text (i.e., raise text on a line with a decreased font size) or subscript text (i.e., lower text on a line with a decreased font size), use the sup and sub elements, respectively. We also use special characters &lt; for a less-than sign and &frac14; for the fraction 1/4 (line 37).

[Page 1339]

In addition to special characters, this document introduces a horizontal rule, indicated by the <hr /> tag in line 24. Most browsers render a horizontal rule as a horizontal line. The <hr /> tag also inserts a line break above and below the horizontal line.

Previous Page
Next Page