XML technologies / XML / XML in a Nutshell / Internationalization / Text declarations

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

As you have learned, XML documents can consist of several parsed entities. These external parsed entities may be DTD fragments or XML subdocuments inserted into the main document using external general entity references. In any case, an external parsed entity need not use the same character set as the main document. Different files can reference the same external parsed entity even if those files use different character sets. It is therefore important to specify an external parsed entity’s character set independently of the including document’s character set.

To achieve this, every external parsed entity should have a text declaration. If present, this declaration must be the first thing in the entity. For example, this text declaration states that the entity uses the KOI8-R character set:

 <?xml version="1.0" encoding="KOI8-R"?> 

A text declaration looks like an XML declaration. It contains version information and an encoding declaration. However, a text declaration must not contain a standalone declaration, and the version information may be omitted. A valid text declaration specifying KOI8-R encoding could also look like this:

 <?xml encoding="KOI8-R"?> 

It is not, however, a valid XML declaration.

The following code example shows an external parsed entity containing several verses from Pushkin’s poem "The Bronze Horseman" in Cyrillic script. The text declaration identifies KOI8-R as the encoding. This is not a well-formed XML document, since it has no root element! It exists only to be included in other documents.

<?xml version="1.0" encoding="KOI8-R"?>
<стих>Была ужасная пора,</стих>
<стих>Об ней свежо воспоминанье...</стих>
<стих>Об ней друзья мои, для вас</стих>
<стих>Начну свое повествованье.</стих>
<стих>Печален будет мой рассказ</стих>

Code example: An external parsed entity with a text declaration specifying KOI8-R

External DTD subsets reside in external parsed entities and may therefore contain text declarations. They should contain text declarations if not written in one of the Unicode character sets. The next code example shows a DTD fragment in KOI8-R that could validate the preceding example after its inclusion in a larger document.

<?xml version="1.0" encoding="KOI8-R"?>
<!ELEMENT стих (#PCDATA)>

Code example: A DTD with a text declaration specifying KOI8-R

  

 

 

 

back to top