XML is proving to be a useful way of storing and exchanging data, but it isn’t very often that you want to use the data in XML format. You usually need to transform the data into some other form to present it to the end user or use it in programs.
By using the classes in the XML namespaces, such as XmlTextReader and XmlDocument, you can obviously write programs that parse XML and turn it into other forms. The problem is that doing so requires programming skills, and in the Web world, a lot of people want to manipulate XML but don’t have such skills.
The Extensible Stylesheet Language (XSL) was developed to provide a way of transforming XML that doesn’t rely on programming skills. Anyone who understands XML syntax can write a style sheet, which is then applied to an XML document to transform it into some other form.
If you’ve encountered cascading style sheets (CSS) when putting a Web site together, you already know how style sheets can be used to manipulate data. XSL is far more full-featured than CSS and permits a much greater range of transformations.
XSL Transformations (XSLT) adds extra constructs to XSL to make it into a simple (and cumbersome) programming language, which greatly increases the scope and complexity of the transformations that you can perform. XSL and XSLT are not normally thought of as separate entities, so when I talk about XSL in the rest of this chapter, you can assume that I’m including XSLT as well.
XSL is used for several different types of text-based transformations, such as:
Transforming XML into HTML for display. This is perhaps the most common use for XSL, where data stored in XML is transformed into HTML to be displayed on a browser. One advantage of this approach is that different style sheets can be developed to suit different clients: one for a Pocket PC, one for a desktop browser, one for a Wireless Application Protocal (WAP) phone, and so on. Generating the HTML is simply a matter of applying the correct style sheet to the data.
Transforming XML to XML. You might need to extract some data from a document, reorder or reformat, or convert between different schemas. For example, two companies might want to exchange customer details in XML, but it’s likely that the formats they use will be different. A style sheet can be used to convert XML documents from one format to the other.
Transforming XML to other text-based formats, such as PDF or RTF. This area is still under development, and few tools are available at present.
To use XSL, you create a style sheet and then pass the XML document and the style sheet through an XSL engine, which performs the transformation. An XSL engine is included with the Microsoft XML (MSXML) parser, and because this parser is installed with Microsoft Internet Explorer, you don’t need to install anything extra to start using XSL.
Before getting into specifics, I’ll mention XPath. It is very common in XSL to want to pick out particular elements or groups of elements. For instance, I might want to display all the title elements as <h1> HTML headings, or all author elements in bold. XPath (the XML Path Language) provides a syntax for selecting elements based on their names, relationships within the document, and other criteria.
If you’ve met regular expressions in text editors or programming languages, you have an idea of how XPath works: XPath is used to select elements, rather than text, from a document.