XSL Basics

As you've learned in previous tutorials, style sheets are special documents or pieces of code that are used to format XML content for display purposes. This definition of a style sheet is perfectly accurate for CSS, which is a style sheet technology that originated as a means of adding tighter control of HTML content formatting. XSL is also a style sheet technology, but it reaches beyond the simple formatting of content by also allowing you to completely transform content. Unlike CSS, XSL was solely designed as a style sheet technology for XML. In many ways, XSL accomplishes the same things that can be accomplished using CSS. However, XSL goes much further than CSS in its support for manipulating the structure of XML documents.

As you might expect, XSL is implemented using XML, which means that you code XSL style sheets using XML code. Even so, you may find it necessary to still use CSS in conjunction with XSL, at least in the near future. The reason for this has to do with the current state of XSL support in web browsers. The component of XSL currently supported in web browsers is XSLT, which allows you to transform XML documents via style sheet code. XSLT doesn't directly support the formatting of XML content for display purposes. The formatting component of XSL is XSL Formatting Objects, or XSL-FO, which consists of special style objects that can be applied to XML content to format it for display. Support for XSL-FO is currently weak in major web browsers, which doesn't make XSL-FO a viable alternative for the Web at the moment. In the meantime, XSLT is quite useful when paired with CSS; XSLT allows you to transform any XML document into HTML that can be styled and displayed with CSS in a web browser.

Although XSL-FO isn't currently a good option for styling XML documents for web browsers, it is quite effective at styling XML documents for printing. For example, you can use XSL-FO to convert an XML document into an Adobe Acrobat PDF document that has a very exacting layout. You learn how to carry out this exact task in Formatting XML With XSL-FO.

It is important to understand how an XSL style sheet is processed and applied to an XML document. This task begins with an XML processor, which is responsible for reading an XML document and processing it into meaningful pieces of information known as nodes. More specifically, XML documents are processed into a hierarchical tree containing nodes for each piece of information in a document. For example, every element and attribute in a document represents a node in the tree representation of a document. Thinking of an XML document as a tree is extremely important when it comes to understanding XSL. After a document has been processed into a tree, a special processor known as an XSL processor begins applying the rules of an XSL style sheet to the document tree.

The XSL processor starts with the root node (root element) in the tree and uses it as the basis for performing pattern matching in the style sheet. Pattern matching is the process of using patterns to identify nodes in the tree that are to be processed according to XSL styles. These patterns are stored within constructs known as templates in XSL style sheets. The XSL processor analyzes templates and the patterns associated with them to process different parts of the document tree. When a match is made, the portion of the tree matching the given pattern is processed by the appropriate style sheet template. At this point, the rules of the template are applied to the content to generate a result tree. The result tree is itself a tree of data, but the data in this case has somehow been transformed by the style sheet. To put it another way, the XSL processor takes a document tree as input and generates another tree, a result tree, as output. Figure 11.1 illustrates the process of using a pattern to match a portion of a document tree and then applying a pattern to it to generate a result tree.

Figure 11.1. A pattern is used to match a portion of a document, which is then transformed into a result tree using a template.

When I refer to a "tree" of data, I'm really talking about the logical structure of the data. To better understand what I mean, think in terms of your family tree, where each relationship forms a branch and each person forms a leaf, or node, on the tree. The same logical relationships apply to XML trees, except the nodes are elements and attributes as opposed to people.

The result tree may contain XML code, HTML code, or special objects known as XSL formatting objects. In the case of XML code, the result tree represents a transformation from one form of XML to another. In the case of HTML code, the result tree represents a transformation from XML code into HTML code that can be viewed in a web browser. Technically speaking, you can't use traditional HTML code as the basis for an XSL result tree because traditional HTML isn't considered an XML-based language. However, if the HTML code adheres to XHTML standards, which is a stricter version of HTML formulated as an XML language, everything will work fine. You learn more about XHTML in Adding Structure To The Web With XHTML, "Adding Structure to the Web with XHTML." You also learn how to transform XML code into HTML (XHTML) later in this tutorial in the section titled, "Your First XSLT Style Sheet."

In order to finish creating the result tree, the XSL processor continues processing each node in the document tree, applying all the templates defined in the style sheet. When all the nodes have been processed and all the style sheet templates applied, the XSL processor returns the completed result tree, which is often in a format suitable for display. I say "often" because it is possible to use XSL to transform a from one XML language to another instead of to XHTML, in which case the resulting document may or may not be used for display purposes.