A Brief XSLT Cookbook

As I mentioned in the introduction, the goal of this chapter is to provide just enough information to allow you to be productive as quickly as possible with XSLT and AxKit. To that end, we will complete our whirlwind tour by looking at how XSLT can be used for several tasks that web developers are commonly asked to perform.

Delivering Browser-Friendly HTML

Problem

Your stylesheets work fine, but older HTML browsers are choking on tags such as <br/> and <img/>.

Solution

Use the xsl:output element and set its method attribute to “html”:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
  . . .

Discussion

The xsl:output element offers an easy way to control the formatting of the result of a given transformation. Other valid values for the method attribute include “text”, and “xml” (the default). This element offers several other useful options, including the ability to set the encoding of the result document (via the encoding attribute) and the ability to add a document type declaration to the output (using the doctype-system and doctype-public attributes).

Alternating Colors in HTML Table Rows

Problem

You are transforming a document that consists of a long list of line items into HTML. You want to make the background of every other row a different color so that the page is more readable.

Solution

Use an xsl:if element that tests the value of the current node’s position and conditionally adds the ...

Get XML Publishing with AxKit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.