[Previous] [Contents] [Next]


Template Rules


The basic XSL style sheet building block is a template rule. A template rule describes how an XML element node (that element and all the elements it contains) is converted into an XSL element node that can be rendered. Don't forget that you can construct new trees out of parts of the document tree (discussed on Day 19, "Converting XML with DSSSL"). The element node could be a whole branch of the document tree, or it could even be the whole of the XML document processed in a different way (for example, when creating a table of contents).

A template rule consists of two parts:

1. A pattern that identifies the XML node (element) in the XML document.
2. An action (rendering or processing part) that details the transformation and rendering of the resulting node (the element or elements that you have identified).

If all you wanted to do was pass the XML document through untouched, the following would probably be enough, although another instruction (xsl:copy) that you will learn later in this chapter can be used to pass XML code through or even to make a hybrid XML/HTML mix:

      <xsl:template match="/">
        <fo:page-sequence>
          <xsl:apply-templates/>
        </fo:page-sequence>
      </xsl:template>
Many of the XSL code fragments shown here rely on flow objects because this is the easiest and preferred way to render XML code. It is, however, not the only way, and flow objects are not currently supported in Internet Explorer 5. (There is some public doubt that they ever will be supported because they aren't relevant to Microsoft's express purpose of converting XML code into HTML code for display in a Web browser). Later in this chapter you will see some examples of XSL style sheets that do not rely on flow objects and that can actually be used in IE5.
The process-children element is used in some of the examples in this chapter because this is what is currently supported. In the latest XSL working draft this element is changed to xsl:apply-templates, and it is this element that is supported in the beta preview 2 release of Microsoft Internet Explorer 5.

It is often useful to put all the basic style properties on the root of the document so that all the descendant elements inherit the properties. This is done by using a single forward slash to represent the root element:

<xsl:template match= "/">

There are many ways of matching elements, including:

Matching by ID
Matching by element name
Matching by ancestry
Matching by children
Matching by attributes

[Previous] [Contents] [Next]