XML technologies / XML / XML in a Nutshell / Internationalization / Character references
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
Unicode contains more than 96,000 distinct characters covering almost every written language. Predefining entity references for each, most of which would never be used in a document, would unnecessarily bloat and burden XML parsers. Rather than choosing which characters deserve entities, XML takes the opposite extreme. It predefines entity references only for characters with special markup meaning: <, >, &, " and '. All are ASCII characters easily entered in a text editor.
For other characters that an ASCII editor may not provide, XML lets you use character references. A character reference gives a Unicode character’s number as a decimal or hexadecimal value. Decimal references look like this: њ. Hexadecimal references add an x after &#, looking like this: њ. Both refer to the same character, Њ, the Cyrillic lowercase “nje” used in Serbian and Macedonian. Suppose you want the Greek maxim “σόφος έαυτóν γιγνώσκει” (“The wise man knows himself”) in your XML document but have only an ASCII editor. Replace each Greek letter with the corresponding reference:
<maxim>
σοφός
έαυτόν
γιγνώσκει
</maxim>To an XML application, references to Unicode characters absent from the current encoding are equivalent to the actual characters in a Unicode document. In other words, this document is equivalent to the preceding one:
<maxim>
σόφος έαυτóν γιγνώσκει
</maxim>Character references are recognized and replaced only in element content and attribute values. They cannot appear in element or attribute names, processing-instruction targets, or XML keywords such as DOCTYPE or ELEMENT. They may occur in comments or processing-instruction data, but are not recognized there. They may appear in the DTD in default attribute values and entity replacement text. Names can be Greek, Russian, Arabic or Chinese, but you must use a character set allowing those characters to be entered directly, not through references. For example, this is well-formed:
<λογος>
σοφός
<λογος>This is not well-formed:
<λογος>
σοφός
</λογος>Interested readers can find all Unicode character codes in Unicode 7.0 Character Code Charts.
If you often use a particular group of character references, it may be easier to define them as entities and refer to those. The following DTD defines entities for the Greek words in the previous examples.
<!ENTITY sigma "σ">
<!ENTITY omikron_betont "ό">
<!ENTITY phi "φ">
<!ENTITY omikron "ο">
<!ENTITY schluss_sigma "ς">
<!ENTITY epsilon_betont "έ">
<!ENTITY alpha "α">
<!ENTITY lambda "λ">
<!ENTITY ypsilon "υ">
<!ENTITY tau "τ">
<!ENTITY ny "ν">
<!ENTITY gamma "γ">
<!ENTITY jota "ι">
<!ENTITY omega_betont "ώ">
<!ENTITY kappa "κ">
<!ENTITY epsilon "ε">Code example: A DTD defining general entity references for some Greek letters
These entities work even in invalid documents, provided their declarations are in the internal DTD subset, which every parser must process, or the parser reads the external subset. By convention, fragments defining only entities use the three-letter suffix .ent. External parameter entity references import these fragments into a document’s DTD. The next example writes the maxim using entities assumed to be at the relative URL greek.ent.
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE maxim [<!ENTITY % griechisches_alphabet SYSTEM "greek.ent">%griechisches_alphabet;]>
<maxim>
σ&omikron;φ&omikron_betont;&schluss_sigma;
&epsilon_betont;α&ypsilon;τ&omikron_betont;&ny;
γ&jota;γ&ny;&omega_betont;σκε&jota;
</maxim>Code example: The maxim using entity references instead of character references
Some standard entity subsets already exist. The XHTML 1.0 DTD contains three useful entity sets you can use in your work (download as a DTD entity file):
- Latin-1 characters
Non-ASCII ISO-8859-1 characters from 160 upwards.
- Special characters
ISO-8859-2 (Latin-2) letters absent from Latin-1, such as Œ, and punctuation such as the dagger, euro sign and em dash.
- Symbols
The Greek alphabet, excluding accented characters, plus punctuation, mathematical operators and other mathematical symbols.
Use these sets directly from their relatively stable W3C URLs or copy them to your own systems. To use the symbol set, for example, add this to the document’s DTD:
<!ENTITY % HTMLsymbol PUBLIC "-//W3C//ENTITIES Symbols for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent">
%HTMLsymbol;As standardized, reasonably established DTDs, they have both PUBLIC identifiers and URLs. Other people and groups have created usable sets too, but no universal collection covers all Unicode. SGML includes almost 20 entity sets covering Greek, Cyrillic, extended Latin, mathematics, diacritics, box-drawing characters and publishing marks. They are not part of XML, but applications including DocBook and MathML have ported them to XML. MathML also has useful entity sets containing further mathematical symbols.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top