XML technologies / XML / XML in a Nutshell / Internationalization / The encoding declaration

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

Every XML document should include, as part of its XML declaration, an Encoding declaration with the keyword encoding. This declaration tells the parser which character set is used in the document. It is used only when other metadata outside the document are unavailable. For example, the following XML declaration states that the document uses US-ASCII encoding:

<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>

This one states that the document uses the Latin-1 character set, under the more official name ISO-8859-1:

 <?xml version="1.0" encoding="ISO-8859-1"?> 

Sometimes the encoding declaration using encoding can be omitted even when no external metadata is available, namely when the document uses the Unicode encodings UTF-8 or UTF-16. UTF-8 is a proper superset of ASCII, so ASCII files can be valid XML documents even without an encoding declaration. However, this applies only to genuine 7-bit ASCII files. It excludes the extended ASCII character sets that some editors produce with characters such as ©, ç or ".

Even when character set metadata are available, many parsers simply ignore them. We therefore strongly recommend including an encoding declaration in every document that does not use UTF-8 or UTF-16. It can do no harm.

  

 

 

 

back to top