A few important things have happened in the Firefox world since my article "XML in Firefox 1.5, Part 1: Overview of XML features" was first published in September 2005 (updated March 2006). Chief among these is the release of Firefox 1.5, and then the release of the current version of Firefox which is 220.127.116.11, a minor bug-fix. Firefox 18.104.22.168 is soon to be released (it's due in early April). In this article, I offer a detailed look at the basics of XML processing in Firefox. I include Firefox screenshots, all of which were created using Firefox 22.214.171.124 on Ubuntu Linux, with a fresh profile (that is, with no extensions, and default options as installed).
The most basic thing you can do with Firefox and XML is to load an XML file in an unknown vocabulary with no associated stylesheet. Listing 1 is such a file.
Listing 1 (listing1.xml). Simple XML file example
<memo> <date form="iso-8601">2002-08-14</date> This is just to <strong>say</strong>: I ate the eggs you left in the fridge And were probably saving for breakfast. Do you know? They were <emph>quite</emph> rotten. </memo>
Viewing this file with Firefox yields the display in Figure 1.
Figure 1. View Listing 1 in Firefox
Pay close attention to the message at the top of the browser area. In particular "The document tree is shown below." This underscores that you should not consider this a source view of the XML. It is simply a logical layout of the parts of the document Firefox cares about. It does omit details that might matter to you, though not to Firefox, and it does introduce some distortion of the document. For an example of distortion, notice that Firefox puts each element on a new line, even though it is not so in the source document. For a document such as this one, which uses mixed content, this is a significant rearrangement of the content. As an example of Firefox's omitting details, try to view Listing 2 in the browser.
Listing 2 (listing2.xml). Simple XML file example with namespaces and more
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE memo> <memo xmlns='http://example.com'> <date form="iso-8601">2002-08-14</date> This is just to <strong>say</strong>: I ate the eggs you left in the fridge And were probably saving for breakfast. Do you know? They were <emph>quite</emph> rotten. </memo>
The display of Listing 2 is precisely the same as that of Listing 1, so the XML declaration, document type declaration and namespace declarations are all omitted from the display. If you do want to see the original XML in all its glory, use the view-source function. From the menu bar, select View, then Page Source. The usual shortcut is Ctrl+U. You can also use the context menu (right click) from the main browser pane. The view-source display is shown in Figure 2, a perfect match to the source listing.
Figure 2. View-source display of Listing 2 in Firefox
Go back to the display in Figure 1 and notice the minus sign next to the
memo element's opening tag. Each container element has such a marker, and you can click on it to collapse or fold that element. This can be useful when debugging, if you want to pack away parts of the XML file that do not interest you at the moment.
To demonstrate Firefox's treatment of ill-formed documents I added a bogus character entity
(some characters are illegal in XML, even if expressed as entities) just before the
date element of Listing 2 and viewed it in Firefox. Figure 3 shows the Firefox output, reporting the error and the location at which it was detected.
Figure 3. Firefox display of ill-formed XML.
Notice how just enough of the source file is displayed to pin-point the error. You can always see the full source document again using the view-source feature.
Types of XML
XML is just a base format with which you can build more specific formats, and Firefox uses special processing and rendering for prominent XML formats it happens to support. I touched on some of these in the previous article, including XHTML, Scalable Vector Graphics (SVG), and XSLT. The primary means for Firefox to determine whether a browsed resource is XML, and if so whether it's some special form of XML, is the internet media type (commonly known as the MIME type). A Web server sends MIME type information for every resource delivered to the browser. For files opened from your local file system, the browser guesses the MIME type based on the file's extension. Table 1 summarizes the XML-related MIME types that are recognized by a clean Firefox install.
Table 1. XML MIME types handled by Firefox
|MIME type||Handling by Firefox||Notes|
|text/xml||Apply CSS, if available, otherwise default XML handler||Avoid this media type, if possible|
|application/xml||Apply CSS, if available, otherwise default XML handler|
|application/*+xml||Apply CSS, if available, otherwise default XML handler||Applies to any media type using the convention for XML|
|application/xhtml+xml||Render as XHTML, according to doctype declaration||This includes vocabularies -- such as MathML, XLink, and even SVG -- that Firefox recognizes when embedded within XHTML|
|application/vnd.mozilla.xul+xml||Mozilla chrome handler (for custom Mozilla UIs)|
|application/rdf+xml||Default RDF renderer (displays all text literal objects)||Firefox does use RDF in its configuration registries|
Some Firefox extensions allow Firefox to recognize additional media types. You can also add an application handler for some XML format. For example, if you want to handle VoiceXML with a voice browser, you register that application with the MIME type
You can always check the MIME type that Firefox associates with a page once it loads the page. Right click on the page, and select View page info from the context menu. Firefox uses
text/xml for files with a .xml extension, although according to current best practice it should use
A series of unfortunate limitations
Firefox does not support a few XML facilities you might use. When you design pages with Firefox in mind, you want to know its limitations in basic XML processing. Many of these limitations are acknowledged in bug reports and enhancement requests ("bugzilla"). You'll find links to many of these in Resources. You can vote for a bug or enhancement request to be given higher priority by Mozilla developers, so if these limitations affect you, please consider getting a Mozilla bug tracker account (very simple to obtain) and vote for resolution.
The first limitation to mention is that whenever Firefox parses an XML file, it pretty much hangs up that thread of processing until the parse is complete. This means that if you send Firefox a very large XML file, your users might have a long wait before they see anything happen. If you send Firefox a large HTML file, it uses incremental rendering to display the HTML bit by bit as it reads it. It would be nice to have the equivalent capability for XML, but for now, just consider the size of XML files that you send the browser.
Firefox does not support DTD validation. It doesn't read DTDs in external files, but it also doesn't use any declarations within the document (called the internal subset) for validation. As far as reading external files, Firefox does not read any external entities at all, whether parameter entities (such as DTDs and DTD fragments) or general entities (external, well-formed XML fragments). This means that Listing 3 is logically processed by Firefox the same way as Listing 4 regardless of the contents of extFile.ent.
Listing 3. XML file that uses an external parsed entity
<!DOCTYPE myXML[ <!ENTITY extFile SYSTEM "extFile.ent"> ]> <myXML>&extFile;</myXML>
Listing 4. XML without entities that is logically treated by Firefox identically to Listing 3
Support of such external entities does have possible security implications, and possible performance implications, but both have workarounds, and I hope Firefox addresses these limitations soon.
If you happen to use RDF/XML, be aware that Firefox does not employ well-formedness checks when parsing RDF. As a consequence, the fact that Firefox processes RSS 1.0 Web feeds (which are RDF) without regard to well-formedness, which is unfortunate because the Web community is trying to increase the enforcement of well-formed Web feeds.
Elements of style
The easiest way to get Firefox to render arbitrary XML in a non-generic way is to use a stylesheet. Firefox supports cascading stylesheets and XSLT. I won't dwell too much on the use of these technologies in Firefox because IBM developerWorks already offers a set of in-depth tutorials on the topic. See Resources for more details. One thing I shall mention here is that you must ensure that any stylesheets are loaded from the same Internet domain as the source XML document, otherwise Firefox will not load and apply the stylesheets. This security restriction is to avoid cross-site scripting attacks (XSS).
For Firefox-specific XSLT
Even in an established standard such as XSLT, precise behavior can differ across engines. If you need to specify a section of XSLT to execute only under Mozilla and thus Firefox, or indeed under any form of Transformiix, which is the XSLT engine bundled with Mozilla, use a conditional block such as in Listing 5.
Listing 5. Example of a Mozilla-specfic code block in XSLT
<xsl:if test="system-property('xsl:vendor')='Transformiix'"> <xsl:text>This will only be output by Firefox/Mozilla/Transformiix</xsl:text> </xsl:if>
Each XSLT engine will have a different value for
system-property, and if necessary you can use
xsl:choose instead to provide sections specific to each one.
As you can see, Firefox has a lot of capabilities. You can view XML in a simplified logical view or in original source form. You are notified of any well-formedness errors. You can tailor the display using CSS or XSLT. Firefox recognizes several important XML vocabularies based on MIME type, and handles them accordingly. Firefox does have some limitations when you process XML with it. All the major browsers could use some work in their XML support, and so understanding such abilities and limitations is important. As XML-based technologies such as Web feeds, SVG and XSLT become more important, one can expect better XML support in browsers. Meanwhile you can do a lot with XML in Firefox today. Look for future articles in this series on IBM developerWorks to learn more.
So to summarize, the internal subset of a DTD is checked for internal general entities, which are correctly processed. All other non-character entity types are ignored, and no validation is performed, even for declarations within the internal subset.
- "XML in Firefox 1.5, Part 1: Overview of XML features:" Review the first article in this series and look at the different XML-related facilities in Firefox (September 2005, developerWorks).
- A survey of XML standards: Part 4: In this detailed cross-reference of the most important XML standards by Uche Ogbuji, explore many of the XML technologies mentioned in this article (developerWorks, March 2004).
- Get the basics of CSS and CSS with XML in the "Display XML with Cascading Stylesheets" tutorial series on developerWorks:
- "Use Cascading Stylesheets to display XML, Part 1" features basic techniques to present XML in Web browsers (November 2004)
- "Use Cascading Stylesheets to display XML, Part 2" covers advanced topics for the use of CSS to style XML in browsers (February 2005)
- "Use Cascading Stylesheets to display XML, Part 3" discusses how to apply XSLT to XML source, including XSLT techniques that work with CSS for HTML or XML output (June 2005)
Several Mozilla bugs and feature requests relate to XML, which only underscores some of the limitations you'll find working with the software.
- 18333: "XML Content Sink should be incremental". This request is very important, because it leads to unfriendly behavior of the browser when dealing with large XML documents.
- 69799: "External entities are not included in XML document". This covers the fact that Mozilla ignores external entities. Presumably Mozilla should support at least entities served from the same Internet domain.
- 22942: "Load external DTDs (entity/entities) (local and remote) if a pref is set". This request covers loading the external subset of the DTD independently of whether validation is actually performed.
- 196355: "Implement validating XML parser (validate with DTDs)". This request covers validation with DTD, internal or external.
- 98413: "Implement XML Catalogs" would allow more flexible management of external entities for performance, security, versioning or other reasons.
- developerWorks XML zone: Find more XML resources here, including articles, tutorials, tips, and standards.
- IBM Certified Solution Developer -- XML and related technologies: Learn how to get certified. Free developerWorks tutorials, IBM XML certification success, help you prepare for the exam.
Get products and technologies
- Firefox: Get the Mozilla-based Web browser that offers standards compliance, performance, security, and solid XML features. The current version is 126.96.36.199.
Dig deeper into XML on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Keep up with the best and latest technical info to help you tackle your development challenges.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.