10.5. The Extensible Stylesheet Language
The Extensible Stylesheet Language (XSL) is one of the most intricate specifications in the XML family. XSL can be broken into two parts: XSLT, which is used for transformations, and XSL Formatting Objects (XSL-FO). While XSLT is currently in widespread use, XSL-FO is still maturing; both, however, promise to be useful for any XML developer.
This section will provide you with a firm understanding of how XSL is meant to be used. For the very latest information on XSL, visit the home page for the W3C XSL working group at http://www.w3.org/Style/XSL/.
As we mentioned, XSL works by applying element-formatting rules that you define for each XML document it encounters. In reality, XSL simply transforms each XML document from one series of element types to another. For example, XSL can be used to apply HTML formatting to an XML document, which would transform it from:
<?xml version="1.0"?> <OReilly:Book title="XML Comments"> <OReilly:Chapter title="Working with XML"> <OReilly:Image src="http://www.oreilly.com/1.gif"/> <OReilly:HeadA>Starting XML</OReilly:HeadA> <OReilly:Body> If you havent used XML, then ... </OReilly:Body> </OReilly:Chapter> </OReilly:Book>
to the following HTML:
<HTML> <HEAD> <TITLE>XML Comments</TITLE> </HEAD> <BODY> <H1>Working with XML</H1> <img src="http://www.oreilly.com/1.gif"/> <H2>Starting XML</H2> <P>If you havent used XML, then ...</P> </BODY> </HTML>
If you look carefully, you can see a predefined hierarchy that remains from the source content to the resulting content. To venture a guess, the <OReilly:Book> element probably maps to the <HTML>, <HEAD>, <TITLE>, and <BODY> elements in HTML. The <OReilly:Chapter> element maps to the HTML <H1> element, the <OReilly:Image> element maps to the <img> element, and so on.
This demonstrates an essential aspect of XML: each document contains a hierarchy of elements that can be organized in a tree-like fashion. (If the document uses a DTD, that hierarchy is well defined.) In the previous XML example, the <OReilly:Chapter> element is a leaf of the <OReilly:Book> element, while in the HTML document, the <BODY> and <HEAD> elements are leaves of the <HTML> element. XSL's primary purpose is to apply formatting rules to a source tree, rendering its results to a result tree, as we've just done.
However, unlike other style sheet languages such as CSS, XSL makes it possible to transform the structure of the document. XSLT applies transformation rules to the document source and by changing the tree structure, produces a new document. It can also amalgamate several documents into one or even produce several documents starting from the same XML file.
10.5.1. Formatting Objects
One area of the XSL specification that is gaining steam is the idea of formatting objects. These objects serve as universal formatting tags that can be applied to virtually any arena, including both video and print. However, this (rather large) area of the specification is still in its infancy, so we will not discuss it further in this reference. For more information on formatting objects, see http://www.w3.org/TR/XSL/. The remainder of this section discusses XSL transformations.
Copyright © 2003 O'Reilly & Associates. All rights reserved.