Pick the Correct MIME Media Type

Item 45. Pick the Correct MIME Media Type

XML processors, web browsers, and many other tools rely on MIME media types to dispatch XML content to the correct processor. Just a few minutes ago I got an e-mail from an annoyed reader because I'd published some SVG pictures with the MIME type text/xml instead of application/xml+svg, so his SVG viewer couldn't read them. It's not hard to set the correct MIME type in most web servers. However, most existing servers ship with incorrect or incomplete MIME types, so you probably have to fix your server's configuration before publishing.

Pure, undifferentiated XML comes in two flavors:

  1. text/xml; charset="iso-8859-2"

  2. application/xml; charset="utf-16be"

If no more specific MIME type has been registered for an application with the IANA, you'll almost always want to use application/xml. The semantics of the text MIME type require that parsers and browsers treat a text/xml document as ASCII, even if the encoding declaration or byte order mark says something different! Not all parsers, browsers, and applications get this right. Nonetheless, some do, and they will have trouble if you serve anything except an ASCII document with this MIME type.

The application/xml MIME type makes no assertions about the character encoding of the XML document. When a parser receives an XML document with the MIME type application/xml, it will read the XML declaration and byte order mark to determine the encoding.

You can also add a charset field to the MIME type to indicate the character encoding of the document. For example,

  • text/xml

  • application/xml

In this case, the specified charset should take precedence over whatever the encoding declaration specifies. However, in practice you can't rely on parsers or browsers paying attention to this, so it's best to choose application/xml, leave the charset unspecified, and include an encoding declaration in the document itself. This also makes it much easier to use different encodings for different documents with the same file-name extension on the same server or in the same directory.

There are two similar MIME types for external parsed entities that are not complete XML documents:

  1. text/xml-external-parsed-entity

  2. application/xml-external-parsed-entity

The text/xml-external-parsed-entity MIME type has the same flaw as text/xml; that is, it asserts that the content is ASCII in the face of all evidence to the contrary. Use application/xml-external-parsed-entity instead.

The application/xml-dtd MIME type has been registered for use on document type definitions and fragments thereof.

As of mid-2003, a few MIME types, including those listed below, have been registered for specific XML applications:

  • application/xhtml+xml for XHTML

  • text/vnd.wap.wml for the Wireless Markup Language

  • application/cnrp+xml for the Common Name Resolution Protocol

  • application/beep+xml for the Blocks Extensible Exchange Protocol

More are anticipated in the future, including the following:

  • application/mathml+xml for MathML

  • application/xslt+xml for XSLT

  • application/rdf+xml for RDF

  • image/svg+xml for SVG

However, these are not yet official.

One MIME nontype deserves special comment. This is the mythical text/xsl used by Microsoft Internet Explorer to identify XSLT stylesheets in xml-stylesheet processing instructions, as shown below.

<?xml-stylesheet type="text/xsl" href="book.xsl"?>

Microsoft made up this type without consulting the rest of the community or attempting to use the well-established procedures for registering new MIME types. text/xsl is not endorsed by the W3C, the IETF, or the IANA. Microsoft has not attempted to register this type and does not seem likely to do so in the future. Use of this alleged MIME type in software, documents, HTTP headers, or anywhere else is incorrect. Eventually, application/xslt+xml will be officially registered to identify XSLT documents. In the meantime, you should use application/xml instead.

<?xml-stylesheet type="application/xml" href="book.xsl"?>

Currently Mozilla, Galeon, and Netscape recognize this, but Internet Explorer does not. This is a bug in Internet Explorer. Most other browsers do not support XSLT at all, so for them the question is moot.

If you're using the Apache web server, you can adjust the system default MIME type mappings for various file-name extensions in the mime.types file. This customarily resides in the $SERVER_ROOT/conf directory. For example, here's a list that duplicates the suggestions in this item.

application/xml                         xml rdf svg
application/xml-dtd                     dtd
application/xml+xhtml                   xhtml xhtm
application/xml-external-parsed-entity  ent
text/vnd.wap.wml                        wml

These are not the mappings Apache uses by default, so you'll also want to delete the mappings it does use that map these types to text/xml.

If you don't have root access to your web server, you can add these mappings in an AddType directive in the .htaccess file in the directory where the documents reside.

AddType application/xml                         xml rdf svg
AddType application/xml-dtd                     dtd
AddType application/xml+xhtml                   xhtml xhtm
AddType application/xml-external-parsed-entity  ent
AddType text/vnd.wap.wml                        wml

If you're using the Microsoft Internet Information Services (IIS) HTTP server, the GUI can map file-name extensions to individual MIME types. The exact instructions depend on which versions of IIS and Windows you have, but generally you'll open the Properties panel for the web site (in Windows 2000 you'll find this in the Computer Management section of the Administrative Tools control panel), select the HTTP Headers tab, and click File Types to bring up the File Types dialog. After you click New Type, a dialog box pops up in which you can enter the file-name extension and the MIME type to map it to. Figure shows the various dialogs involved.

Adding a MIME Type to IIS on Windows 2000 Professional


For other web servers, consult the server documentation to determine how to change the MIME type mappings.

Whether you're using IIS, Apache, or something else, the main thing you need to remember is to reconfigure the MIME type mapping for XML as application/xml. The default is almost certainly text/xml, and this is almost never what you actually want. Using application/xml ensures that the encodings the documents declare for themselves will be respected.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows