Item 44. Serve XMLCSS to the Client

Item 44. Serve XML+CSS to the Client

Given the limitations of the installed base of browsers, I'm not sure I would ever recommend serving XML directly rather than transforming on the server side. However, most current desktop web browsers do have reasonable support for direct display of XML documents with attached CSS stylesheets. The exact part of CSS any browser supports varies from one to the next , but browsers that include some reasonable level of CSS support include Internet Explorer 5.0 and later, Opera 4.0 and later, Netscape 6.0 and later, Mozilla, Safari, and Konqueror. Support for CSS is much better on the client side than support for XSLT. If you really want to serve XML documents directly to clients , CSS is the way to go.

Unlike HTML, XML does not normally include the CSS directly inside the document. Instead, a separate stylesheet is referenced by an xml-stylesheet processing instruction in the document prolog. This stylesheet has an href pseudo-attribute containing a relative or absolute URL pointing to the stylesheet and a type pseudo-attribute containing the MIME media type text/css. For example, an RSS document might begin like this:

 <?xml version="1.0"?> <?xml-stylesheet type="text/css" href="styles/rss.css"?> <rss> ... 

A recent web browser that loaded this document would know that the processing instruction means it should load and apply the CSS stylesheet found at the relative URL styles/rss.css. Most other applications would ignore the processing instruction.

You can even provide multiple stylesheets for different media by setting the optional media pseudo-attribute to one of these values:

  • screen : typical computer monitors

  • tty : monospaced text terminals such as xterm windows , DEC VT-100s, and IBM 3270s

  • tv : televisions

  • projection : wall and movie screen display

  • handheld : PDAs, cell phones, and similar small devices

  • print : paper that comes out of a laser, inkjet, or other printer

  • braille : braille tactile feedback devices used by blind persons

  • aural : speech synthesizers

  • all : stylesheet appropriate for all devices

The browser should then pick the stylesheet that most closely matches its environment. For example, these instructions specify different stylesheets for printers, monitors, and speech synthesizers.

 <?xml-stylesheet type="text/css" media="print"                  href="styles/rss_print.css"?> <?xml-stylesheet type="text/css" media="screen"                  href="styles/rss_screen.css"?> <?xml-stylesheet type="text/css" media="aural"                  href="styles/rss_aural.css"?> 

The media pseudo-attribute can also contain a comma-separated list of media types, in which case it applies to all listed media. For example, the xml-stylesheet processing instruction that follows applies to print, screen, and projection media.

 <?xml-stylesheet type="text/css"                  media="print, screen, projection"                  href="styles/rss_main.css"?> 

If more than one stylesheet is available for a given medium, all are applied, using the normal cascading rules of CSS to resolve conflicts. For example, faced with the following batch of stylesheet processing instructions, a web browser that was printing the XML document would use both rss_main.css and rss_print.css but not rss_screen.css or rss_aural.css .

 <?xml-stylesheet type="text/css"                  media="print, screen, projection"                  href="styles/rss_main.css"?> <?xml-stylesheet type="text/css" media="print"                  href="styles/rss_print.css"?> <?xml-stylesheet type="text/css" media="screen"                  href="styles/rss_screen.css"?> <?xml-stylesheet type="text/css" media="aural"                  href="styles/rss_aural.css"?> 

You can offer users a choice of different stylesheets by giving each stylesheet a title pseudo-attribute and an alternate="yes" pseudo-attribute. The browser should then indicate the choices to users and allow them to pick. For example, the following set of xml-stylesheet processing instructions offers different stylesheets for different web browsers.

 <?xml-stylesheet type="text/css" title="Opera" alternate="yes"                                  href="styles/rss_opera.css"?> <?xml-stylesheet type="text/css" title="Internet Explorer"                  alternate="yes" href="styles/rss_ie.css"?> <?xml-stylesheet type="text/css" title="Safari" alternate="yes"                                  href="styles/rss_khtml.css"?> <?xml-stylesheet type="text/css" title="Konqueror" alternate="yes"                                  href="styles/rss_khtml.css"?> <?xml-stylesheet type="text/css" title="Netscape" alternate="yes"                                  href="styles/rss_gecko.css"?> <?xml-stylesheet type="text/css" title="Mozilla" alternate="yes"                                  href="styles/rss_gecko.css"?> 

Alternate stylesheets are not loaded by default, even if the media matches. The user has to make a choice, in which case the choice replaces the default nonalternate stylesheet. Unfortunately, this is mostly theoretical. Current browsers have limited if any support for allowing users to pick a stylesheet from a list of alternates.

Since CSS does not include a transformation step, this only works when the content in the document is already pretty close to the content you want to show the user. The content should be essentially words in a row that can be straightforwardly divided into paragraphs, lists, and tables. The structures in the document are going to have to clearly map to the presentation. This works well for narrative structures such as DocBook and even for simple record-like structures such as RSS. It doesn't work nearly as well for more complex, data-heavy applications such as RDF or the MegaBank Statement Markup Language. Of course, applications like SVG and MathML that don't describe text at all may need a specialized renderer.

It is possible to use both XSLT and CSS. An XSLT stylesheet can transform a document into a form that's closer to the finished presentation, and then CSS can describe how the individual parts of that document are styled. This works especially well when you transform into HTML+CSS because all browsers handle that at least somewhat and most current browsers handle it quite well.



Effective XML. 50 Specific Ways to Improve Your XML
Effective XML: 50 Specific Ways to Improve Your XML
ISBN: 0321150406
EAN: 2147483647
Year: 2002
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net