Friday, May 26, 2006
So you think you know XML
Over the past couple of days, a co-worker and I have engaged in a lot of metaphorical hair-pulling over an attempt to load XML data into a Tamino database. Apparently the server was interpreting the data as ISO 8859-1 encoding, even though it was UTF-8. There was no "encoding" attribute in the XML declaration; I understood this to mean a default of UTF-8. But the database's default encoding was 8859-1, and XML in a Nutshell (Second Edition) says:
All you have to do is tell the parser which character encoding the document uses. Preferably this is done through metainformation, stored in the filesystem or provided by the server. However, not all systems provide character-set metadata so XML allows documents to specify their own character set with an encoding declaration inside the XML declaration...
The encoding attribute is optional in an XML declaration. If it is omitted and no metadata is available, then the Unicode character set is assumed. The parser may use the first several bytes of the file to try to guess which encoding of Unicode is in use. If metadata is available and it conflicts with the encoding declaration, then the encoding specified by the metadata wins. [Emphasis added]
For confirmation, I checked the XML 1.1 specification. As far as I can tell, it doesn't have any clear statement about how the encoding attribute in the XML declaration relates to external metadata. So Tamino may be acting correctly, or at least with a good excuse. But it's hard to tell.