The solution presented in this article is architecturally very simple:
- Source data (page content or database-derived data) is in XML format.
- XSLT is used to transform XML to HTML.
- Transformed HTML is displayed in the web browser or returned to search engine spiders.
- The XML-to-HTML transformation ideally is performed in an XSLT-capable browser or on the web server to support non-XSLT-capable clients.
The code examples we’ll use for this article also assume the following (although you can easily adapt them to fit your environment):
- XSLT transformation results in a complete (X)HTML document. Alternatively, you can use AJAX-based solutions to render XML fragments in XSLT-capable browsers or on the web server.
- XML data processed on the server should be in a format that allows XSLT local transformation. (In most cases, this requires parsing the XML data into a DOM tree.) Potential workarounds are available in case you serve static XML files.
- A cookie is set on XSLT-capable browsers to tell the web server that it doesn’t have to perform the XSLT transformation. This cookie can be set on the fly (by the first page downloaded by the visitor) or in a dedicated browser-check web page.
To request the browser to perform the XSLT transformation, we’ll use the <?xml-stylesheet ?> processing instruction, with the type attribute set to text/xsl. Upon encountering this instruction in an XML document, XSLT-capable browsers download the referenced XSLT style sheet and use it to transform the XML data into HTML markup, resulting in the following benefits:
- The XML-to-HTML processing is performed on the browser, reducing CPU utilization on the web server.
- The XSL transformation achieves perfect separation of data (XML) and its presentation (HTML).
- The web page formatting is stored in static .xsl files that are cached by the browser. The formatting rules are thus downloaded only once and used to transform many subsequent data pages.
For visitors without XSLT-capable browsers or for search engine spiders, the XML-to-HTML transformation is performed on the web server, and the resulting HTML markup is served to the visitor.