Small Business Resources, Business Advice and Forms from AllBusiness.com

Picking Your Style of XSL

* From  XML For Dummies, 4th Edition
Date: Friday, August 12 2005

XML's style needs are pretty extensive. Clean XML document structures are rarely in the form you need for presentation, and you may need to present the same XML document in several ways — in print, on the screen, or even in a multimedia presentation. In addition, for those who want to take

full advantage of XML's power as a tool for sharing data across systems, there should be an easy way to convert documents from one vocabulary to another.

The architects of XML have you covered. They decided that the responsibility for display and for document conversion should be handled by a separate mechanism rather than XML proper, so they developed XSL as a special vocabulary of XML, designed to describe stylesheets for XML documents. During development, these same architects realized that creating one mechanism for both display and conversion was a Herculean task. So they split the style and conversion mechanisms of XSL into two different but related mechanisms:

  • XSL Transformations (XSLT) handles the conversion from one set of markup to another.
  • XSL Formatting Objects (XSL-FO) helps you format XML for devices that want to display XML.

XSLT

The first and most well-developed face of XSL is a conversion tool known as XSLT. The T stands for transformation, and that's exactly what this part of XSL is designed to do — use a set of rules to transform (that is, convert) documents described by one set of elements into documents described by another set of elements. The two sets of elements don't even have to look anything alike.

You can use XSLT in a couple of especially intriguing ways:

  • To transform documents described using XML elements into HTML or XHTML for display in a Web browser. This nifty trick overcomes the dilemma created by the overall lack of consistency in browser support for XML and its friends, such as XSL-FO and even CSS. One day, browsers will all be XML savvy, but until then, XSLT lets us have our cake and eat it too — with XSLT, you can describe data with XML for storage and manipulation and transform it into HTML or XHTML to display it on the Web for all the world to see.
  • In XML-based data exchange systems. Systems need to exchange data regularly, and many use XML for that exchange. More often than not, the two systems don't use the same XML internally, so they have to spit out and receive data written in an XML vocabulary they aren't ready to work with. These systems use XSLT to convert data from their internal XML vocabulary to one that another system can work with, and vice versa.

To write an XSLT stylesheet, you simply identify an element in one document and specify how it should be described using a different element or set of elements in the new document. You can grab entire elements or just an element's content. You can even reference attribute values and turn them into element content (or turn element content into attribute values). All in all, XSLT is really cool.

The ins and outs of data exchange

It's entirely possible that two (or more) systems exchanging data don't use the exact same schema or DTD. One possible solution to this quandary is for one system to change its internal programming to work with the other's DTD or schema. As much as this fosters a cooperative spirit, often it simply isn't practical. If a system is built around a particular DTD or schema, you can't just come along and rebuild it.

A more practical solution is for each system to support data transformation so that each can continue to use the format it needs. Here's how that solution works:

1. When My Business, Inc., gets data from Your Company, Ltd., that's described with markup based on Your Company's schema or DTD, My Business simply transforms that data into its own data formats (described with markup based on My Business's schema or DTD) before pulling it in.

2. Alternatively, Your Company, Ltd., may be nice enough to transform its data to My Business's XML format before sending it along to My Business.

Either way, a data transformation has to take place.

The mechanism XML uses for defining how data should be transformed from one flavor of XML markup to another is XSL, and specifically the transformation functionality of XSL known as XSL Transformations (XSLT).

The role of XPath

Without XPath, XSLT simply wouldn't work. Before you can transform an element, attribute, or even a chunk of content using XSLT, you have to be able to identify its exact location. For example, if you want to do something special to every third instance of a list item, you have to be able to point to it — and that's what XPath makes possible. XPath is the mechanism XSLT uses to point to a piece of an XML document so that it can be transformed. If you can't find it, you can't change it.

XPath is a very sophisticated set of rules for identifying the most specific pieces of an XML document. You can not only specify particular elements or attributes or their content, but you can also find individual pieces of content — strings — based on the elements and letters around them.

XPath is such a helpful specification that it's not only used by XSLT to guide transformations, but it's also what the XML Linking (XLink) specification uses to make very specific pieces of an XML document into links — and the XML Query language uses XPath for data exchange.

XSL-FO

XSL-FO is used to specify how the structured content of an XML document should be displayed — how content should be styled, laid out, and paginated for presentation on a Web page, a handheld device, or a set of pages in a catalog or book.

As XSL-FO's name indicates, formatting objects are the key to making your XML ready for display.

If you're familiar with CSS, many of these formatting objects will be very familiar, as will the general concepts of formatting.

The different XSL-FO formatting objects are organized into these eight categories:

  • Declaration, Pagination, and Layout formatting objects include all the things you need to set up the basic layout of a document, including pages, master pages, and a title.
  • Block formatting objects create paragraphs, block quotes, and the other building blocks of a document.
  • Inline formatting objects create styles such as boldface, italics, specific colors, and even special individual characters (or strings) inside a block.
  • Table formatting objects create and control tables.
  • List formatting objects create and control lists of all kinds.
  • Links and Multi formatting objects handle — and style — links and multimedia components.
  • Out-of-line formatting objects create and style floating areas and footnotes.
  • Other formatting objects are catch-alls for the objects that don't fit in other categories.

Every formatting object has properties that can be applied to it. For example, the object that creates a table (fo:table) can take properties that specify how thick its borders are, what color they are, and so on. You can use properties to control just about every aspect of a particular object.

XSL-FO is intended to do more than just specify how XML documents should appear on-screen; it's also designed to make the content easier for computers to read aloud. The aural part of the XSL-FO specification focuses on how to make XML content accessible to everyone, including those who can't see the content.

Internet Marketing: Why Metatags Are Important
Interview with Lee Odden, AllBusiness.com's Internet marketing advisor.