Presenting XML to Clients

XML is a useful way of exchanging data between applications and for moving and storing data within an application. However, XML is not very convenient for presenting data to a user because it primarily describes the content of the data and not how this data should be presented to the user.

A user requires that the data be formatted so that it is easy to read. XML defines the data and its metadata, but it does not define how to format the data for presentation. This was in fact one of the design criteria; XML deliberately does define data presentation formats. Because the same data will be presented in different ways to different users, any attempt to include presentation information would turn the XML document into an incoherent mix of data, metadata, and formatting instructions for multiple output devices.

The technique of presenting XML data to a user involves transforming the data from XML to another format. Typical applications are to transform XML into

  • HTML for output to a Web browser

  • WML for output to a WAP-enabled mobile phone

  • PDF for displaying on any graphic client

  • Postscript for output to a printer

  • RTF or TeX for presentation to a word processor or text formatter

  • XML for presentation to another application that requires the data in a different format

The transformation of the XML document can be undertaken either by the server that has access to the document or by the client that controls the display. There are pros and cons for each approach. The more work that is done on the server, the fewer clients it can support. However, relying on the client to perform complex transformations tends to rule out the use of many lightweight (thin) clients.

In practice, most current implementations of XSLT perform the transformations on the server. This means that the server must be aware of all possible client display requirements. In practice, this is not a major problem because most clients support a standard presentation language, such as HTML, WML, and PDF, for visual displays and Postscript for printing.

Presenting XML to Browsers

Web browsers display HTML information. As you saw on Day 16, a well-written HTML page can also be a well-formatted XML document. Given the close relationship between HTML and XML, it is feasible for an HTML browser to be adapted to present XML. Many recent browsers, such as Mozilla, Netscape, Internet Explorer, Opera, and countless others, support presentation of XML documents. However, additional formatting information must be provided with the XML data to enable the browser to format the data. By default, XML documents are rendered in plain text, usually with the XML tags highlighted in color and neatly aligned to highlight the nested tag structure of the document.

Like HTML browsers, there is some variation between the browsers on the market as to how they support presentation of XML. To address this problem, the World Wide Web Consortium (W3C) has defined a standard mechanism called XML Stylesheet Language (XSL) for supplying information on transforming an XML document into different formats. The most common use of XSL, at the present time, is to convert XML into HTML for display by a Web browser, but the technology is much more flexible than this limited use implies.

Early XSL support in popular HTML browsers was based on draft versions of the standard, and some browsers added in their own proprietary extensions. With a fully ratified standard, recent versions of the browsers are now implementing the standard, but at least one manufacturer is maintaining backward compatibility at the expense of standards conformance. You must take into account the variations in client support for XSL when designing your systems. In practice, this means either dictating which client browser is used and targeting your application to that browser, providing multiple XSLT stylesheets for each supported browser, or transforming the XML data into HTML on the server.

XML Stylesheet Language (XSL)

XSL is a family of technologies used to transform XML documents into any other format. The two components of XSL are

  • XSLT— XML Stylesheet Language for Transformation (XSLT) is applied to an XML document to transform it to another format.

  • XSL-FO— XSL Formatting Objects are used to define formatting semantics in a device independent manner.

XSLT has been widely adopted by the computing industry, and you will look in detail at this technology in the “XML Stylesheet for Transformation” section later in this chapter.

XSL-FO XSL Formatting Objects

Formatting objects define a device-independent grammar for defining how data should be presented. The actual data is intermixed with the formatting objects to define a portable formatted document.

Formatting objects are widely referred to as XSL-FO, but this is not an acronym defined in the W3C standard. You may see XSL-FO written in different forms—XSL:FO, XSLFO, XSL/FO, and others.

Listing 17.1 shows a fragment of an XSL-FO document that defines a table of skills in the Agency case study.

Listing 17.1. Fragment of an XSL-FO Document
<fo:block font-family="sans-serif" color="blue">
<fo:table border-style="solid">
 <fo:table-body>
  <fo:table-row>
   <fo:table-cell padding="1mm">
    <fo:block>Bodyguard</fo:block>
   </fo:table>
   <fo:table-cell padding="1mm">
    <fo:block>Critic</fo:block>
   </fo:table>
  <fo:table-row>
 <fo:table-body>
<fo:table>

As you can see from listing 17.1, XSL-FO is an XML document defining formatting instructions and data. In this example, the formatting objects provide the bulk of the document with a very small amount of data. XSL-FO is quite a verbose format and maintains device independence at the cost of some generalizations; the font name on line 1 is sans-serif instead of a device-specific font name, such as Arial. This helps improve portability of XSL-FO but at the expense of control over the exact rendering of a document on a specific device.

XSL-FO requires two stages for presenting an XML document to a client device.

  1. The XML document must be transformed into an XSL-FO document.

  2. The XSL-FO document must be transformed into the necessary formatting instructions.

XSLT is used to transform XML into XML-FO. There are several tools around for converting XSL-FO into other formats. One example is the open-source Apache FOP utility that can produce PDF output from an XSL-FO document. FOP can also create a Java Swing application that will display an XSL-FO document using Swing components. More information on FOP can be obtained from http://xml.apache.org/fop/.

The main criticism of XSL-FO is that it does not bring anything new to the process of formatting XML documents. HTML and WML already do a very good job of defining the presentation of data for two of the most popular client devices—PCs and mobile phones. Similarly, PDF defines a portable format for online presentation, and Postscript defines a portable format for printing documents. There is no requirement for yet another device independent presentation format.

You can find out more about XSL-FO from online Web resources and the book Sams Teach Yourself XML in 21 Days from Sams Publishing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.13.164