XML technologies / XML / XML in a Nutshell / Internationalization / Character set metadata

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

Some environments keep track of which encodings particular documents use. For example, web servers sending XML documents precede them with an HTTP header that looks something like this:

HTTP/1.1 200 OK
Date: Sun, 28 Oct 2001 11:05:42 GMT
Server: Apache/1.3.19 (Unix) mod_jk mod_perl/1.25 mod_fastcgi/2.2.10 Connection:
close
Transfer-Encoding: chunked
Content-Type: text/xml; charset=iso-8859-1

The field Content-Type in the HTTP header contains the MIME media type of the document. As here, it can specify the character set in which the document is written. When an XML parser reads a document from a web server, it should use this information to determine the document’s character encoding.

Many web servers omit the parameter charset from the MIME media type. If the MIME media type is text/xml, the character encoding is assumed to be US-ASCII. If the MIME media type is application/xml, the parser tries to guess the character set by examining the document’s first few bytes.

Note: Since ASCII is almost never the appropriate character set for an XML document, application/xml is preferred to text/xml. Unfortunately, most web servers, including Apache 2.0.36 and earlier, are configured to use text/xml by default. If you use such a version, you should obtain a newer one before serving XML files. You could correct Apache’s MIME types instead of upgrading, but upgrading is preferable. All Apache versions old enough to use the wrong MIME type for XML have several security vulnerabilities that have since been fixed.

We have focused on MIME types in HTTP headers because this is the most common place where character set metadata is applied to XML documents. MIME types are also used in some file systems (such as BeOS), email, and other environments. Other systems may provide character set metadata in a completely different form. The parser should always use such metadata when available, regardless of its form. Unfortunately, in practice not all parsers and programs conform as closely as they should.

  

 

 

 

back to top