In Practice
While this chapter introduces just enough XSLT to get you started, there's enough information in this chapter to create some fairly sophisticated Web pages based on an XML document. For example, the World Wide Web Consortium's (W3C's) site now uses XSLT to transform portions of its content into HTML. In particular, the XSL page uses the basic techniques described here to create text boxes, margins, wrapping text, and overlaps. Of course, the style sheet employs some more advanced features of XSLT. However, the basic structure of the style sheet is exactly as presented in this chapter. You can view the page at http://www.w3.org/Style/XSL/. This page also contains a link to the XSLT source transformation for the page.
If you examine this style sheet, you'll notice a common technique for rendering XML documents. The basic technique, whether it's a Web page or not, is to transform your XML into HTML and include a Cascading Style Sheet (CSS) in the transformation using an HTML <STYLE> element. This is exactly how the W3C does it
In Chapter 1, I mentioned another XML-based Web site, called BeyondHTML.com. This site takes the CSS approach several steps further. First, BeyondHTML selects an XSLT style sheet based on the browser making the request. For example, if the requesting browser is Internet Explorer 5, a style sheet is selected that transforms the XML markup into HTML customized for IE 5. Because you know that IE 5 supports CSS, you can attach an external .css file to the transformation. This technique uses HTML's <LINK> element, which allows you to link a CSS style sheet to an HTML document. Therefore, you can place the following line of code in the <HEAD> element of your transformation:
<LINK REL="STYLESHEET" TYPE="text/css" href="myStyle.css" />
Instead of hard coding the styles directly in every transformation as the W3C has done, this approach lets you maintain the CSS in a single file. Thus, making changes are much easier because you don't have to modify every style sheet to alter a style.