This is the seventh of a nine-part series of tips on XSLT, the XML Stylesheet Language Transformations. So far,...
we've looked at XSLT structure and syntax, its processing model, elements, expressions, functions, and processing techniques. In this tip, we cover the broad outlines of what's involved in using XML documents for Web display -- which these days typically means translating them into HTML 4 or XHTML 1.0 or 1.
At the highest possible level of abstraction, here's what's involved in transforming an XML document (or one or more such documents, and perhaps even just well-formed XML content from a database or other content repository):
- Output the right kind of (X)HTML document preamble and header information
- Convert XML into desired (X)HTML markup
- Output a proper (X)HTML document closing
Of course, the real action's in the second item, which involves opening, processing, and emitting (X)HTML output from one or more XML documents. The nature of such processing varies widely case by case, but generally involves parsing one or more input documents looking for specific content items of interest. When found, such content items must be recast in (X)HTML form. This generally means copying content from the input document to the output document, but also includes handling document pages, hyperlinks, page transitions, and may even involve synthesizing meta-content such as tables of contents, indexes, navigation text and hyperlinks, and so forth.
Among many other good sources of information that deal with this topic, Sal Mangano's tasty little XSLT Cookbook (O'Reilly, 2003, ISBN: 059600372-2) devotes a chapter (8) to this subject. Therein, Mangano elaborates a bit further on the ways in which XSLT can help to create (X)HTML documents from XML sources:
- Perform a static transform of XML into HTML for storage and access on a server, or for on-demand delivery to a Web browser (this is basically what's described in the preceding paragraph)
- Use XSLT as a server-side scripting tool where XML is extracted from flat files or database records and dynamically transformed into (X)HTML by the Web server.
- Use XSLT as a client-side stylesheet -- provided, of course, that the browser supports XSLT processing (an even larger number of browsers do this nowadays than when Mangano wrote his book two years ago, but older browsers may require installation of MSXML (currently available as MSXML 4.0 SP2 for those who don't already have it installed on their Windows machines for use with IE: search for msxml*.* in the %windir%\system32 folder to see if it's installed on your machine). IE 6.0, Netscape Navigator 7.1, Mozilla 1.4, and FireFox 1.0 (plus higher-numbered versions of each one) all include built-in client-side XSLT processing capabilities, though results will vary depending on what MIME type the server associates with XSLT style sheets (for best results, it should be set to "application/xml").
For static content, static translation remains the best and most expeditious approach. For dynamic content, many sites still elect to have server-side functionality do the transform because they can't assume all browsers are properly equipped. For intranets or other environments where software choices can be dictated (or at least checked), many sites are moving toward client-side stylesheet handling, as it moves much of the processing load to the client-side and off the server.
Other good sources of tutorials, descriptions, and examples of transforming XML source documents into (X)HTML form for client-side display are available online at:
- W3 Schools "Example Study: How to transform XML into XHTML using XSLT…"
- FreeVBCode.com: "Transform XML into HTML using XSL Style Sheets"
- Wei Meng Lee "An Introduction to XSLT and XPath" Wireless Developer Network
- Elizabeth deCastro "Transforming XML with XSLT" Peachpit Press Web site
By the time you work through a couple of detailed examples, you'll get the hang of this activity, too.
About the author
Ed Tittel is a full-time writer and trainer whose interests include XML and development topics, along with IT Certification and information security topics. E-mail Ed with comments, questions, or suggested topics or tools for review.