Preprocess XSLT on the Server Side





Item 43. Preprocess XSLT on the Server Side

One of the original goals of XML was to create "SGML for the Web" to allow markup with arbitrary vocabularies to be served to web browsers. To a large extent, this goal has not been met. While some of the latest versions of web browsers support full XML display, many do not; and I am not one who believes that it's acceptable to throw away even 1% of your potential readers just because the web designers think they need some cool new feature that's only available in Internet Exploder 8.0. In 2003, I know people still using Netscape 1 and Netscape 2. Recently I even spent a few minutes surfing with MacWeb. (Remember that one, old-timers?) With proper attention to design, it's still possible to serve these readers well, as long as you focus on serving readers instead of on making the web site look cool. Surprisingly, it is possible to serve old browsers XML, as long as you don't let them know that's what you're doing.

XML served over the Web requires a stylesheet to teach the browser how to format the content it has received. CSS works on the client side, at least as well as anything does. However, it really requires a very modern web browser: Internet Explorer 6, Netscape 6, Mozilla 1.0, Safari 1.0, or Opera 4 or later. This may be plausible in an intranet environment, but it just doesn't fly on the public Internet.

Browser support for XSLT is even more limited (and that's being polite). Internet Explorer 5.0 and 5.5 do not support XSLT, Microsoft's claims to the contrary notwithstanding. Instead they support something I like to call (with apologies to Douglas Adams) a language almost, but not quite, entirely unlike XSLT. Internet Explorer 6.0 does support real XSLT but it still has some nasty bugs. Netscape 4.x and earlier have no support for XSLT. Netscape 6.0 and later and Mozilla do support XSLT, but for various technical reasons it's extremely hard to convince Netscape/Mozilla to actually use an XSLT stylesheet. (It's roughly on a par with convincing a Vogon not to knock your planet down for an interstellar byway.) Opera, Konqueror, Safari, Lynx, and most other browsers don't even attempt to support XSLT.

Fortunately, therefore, it's not very hard to convince the web server to do the transformation work for you. Most XSLT engines including Saxon and Xalan provide servlets that automatically transform XML documents to HTML documents according to an XSLT stylesheet. By using these, you can send clients plain-vanilla HTML that can be read by web browsers going back to Mosaic 1.0. Many application servers such as Apache Cocoon also have built-in support for this process. You can post XML documents on the web server, but the clients only see HTML. With reasonable caching, it's not even that big of a performance hit. (Without caching, this can be a nasty performance hit. Worst case scenario: Some naïve, custom solutions I've seen load an entire Java virtual machine from scratch every time a page is requested. However, the major implementations of server-side transformation aren't nearly this stupid.)


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