Template-Driven Transformations

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Template-Driven Transformations

In general, XSLT transformations fall into two categories: template-driven and data-driven. This section provides information about template-driven transformations. The Data-Driven Transformations section follows.

In a template-driven transformation, the sequence of the output is determined by template-rules used in the transformation. The following is an example of a simple template-driven transformation.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
    <HTML>
      <HEAD>
      </HEAD>
      <BODY>
         <H1><xsl:value-of select="/books/book/title"/></H1>
         <H3><xsl:value-of select="/books/book/author"/></H3>
         <P>
           <SPAN>Abstract:</SPAN>
           <SPAN><xsl:value-of select="/books/book/abstract"/></SPAN>
         </P>
      </BODY> 
    </HTML>
  </xsl:template>
</xsl:stylesheet>

Here, the style sheet expects certain data elements from a source document, which might look like this:

<?xml version="1.0"?>
<books>
   <book>
      <title>Synchronized Jamming</title>
      <author>Kari Hensien</author>
      <abstract>
         A post modern flight of fancy.
      </abstract>
   </book>
</books>

The style sheet then decides how the output should appear. This means that a template-driven transformation produces its result by pulling the specified data elements from the source document, and putting them through the appropriate templates.

To process multiple book entries, a template-driven XSLT style sheet can use <xsl:for-each> to set up a loop to process each entry. It can also use <xsl:sort> to process the entries in specified order, or it can use <xsl:if> or <xsl:choose> to selectively process some entries based on a certain condition. A template-driven transformation is suitable for processing an information-rich source document, as long as the data is well structured and the result pattern is predictable.

On the other hand, data-driven transformations are ideal for processing a data source when the data is poorly structured, or when the output depends on recursive processing. In practice, template-driven and data-driven transformations are often combined in a single style sheet for a wide variety of XML applications.

The following topics walk you through some common tasks for writing a template-driven XSLT style sheet.

See Also

Using XPath Expressions to Select Nodes | Starter Kit | Data-Driven Transformations