What Is a Template?

A template defines what the XSLT processor should do when it processes a particular node in the XML source document. The XSLT processor populates the result document by instantiating a sequence of templates. Instantiation of a template means that the XSLT processor

  • Copies any literal data from the template to the result document
  • Executes the XSLT instructions in the template

The following topics further describe what a template is:

Contents of a Template

The stylesheet example in Stylesheet used defines the following templates using the xsl:template instruction:

<xsl:template match="/">
               

              
<html><head><title>Stylesheet Example</title></head>
                   
<body>
                   
<table align="center" cellpadding="5">
                   
<tr><th>Title</th><th>Author</th><th>Price</th></tr>
                   
<xsl:apply-templates select="/bookstore/book">
                   
<xsl:sort select="author">
                   
</xsl:apply-templates>
                   
</table></body></html>
                   

                
</xsl:template> <xsl:template match="book">
<tr><td><xsl:value-of select="title"/></td>
                   
<td><xsl:value-of select="author"/></td>
                   
<td align="right"><xsl:value-of select="price"/></td></tr>
                   

                
</xsl:template>

In the xsl:template tag, the value of the match attribute is an XPath pattern. This pattern matches (identifies) a node or a set of nodes in the source XML document. The value of the match attribute is the template rule.

The template body defines actions you want the XSLT processor to perform each time it instantiates this template. It contains

  • XSLT instructions you want the XSLT processor to follow; for example, xsl:apply-templates in the first template, and xsl:value-of in the second template.
  • Elements that specify literal output you want the XSLT processor to insert in the result document. For example:
    <table align="center" cellpadding="5">
                           
    
                        

Determining Which Template to Instantiate

When the XSLT processor applies a stylesheet to an XML document, it begins processing with the root node of the XML source document. To process the root node, the XSLT processor searches the stylesheet for a template rule that matches the root node. A template rule matches the root node when the value of the template's match attribute is "/".

If you explicitly defined a template rule that matches the root node, the XSLT processor finds it and instantiates its template. If the XSLT processor does not find an explicitly defined template rule that matches the root node, the processor instantiates the default template that matches the root node. Every stylesheet includes this default template.

Note

 

Whether or not you explicitly define a template rule that matches the root node, the XSLT processor always instantiates a template that matches the root node.

In the sample stylesheet on , the template rule in the first template matches the root node:

<xsl:template match="/">
               

            

The XSLT processor instantiates this template to start generating the result document. It copies the first few lines from the template to the result document. Then the XSLT processor reaches the following XSLT instruction:

<xsl:apply-templates select="/bookstore/book"/>
               

            

When the XSLT processor reaches the select attribute, it creates a list of all source nodes that match the specified pattern. In this example, the list contains book elements. The processor then processes each node in the list in turn by instantiating its matching template. First, the XSLT processor searches for a template that matches the first book element. The template rule in the second template matches the book element:

<xsl:template match="book">
               

            

After instantiating this template for the first book element, the XSLT processor searches for a template that matches the second book element. The XSLT processor instantiates the book template again, and then repeats the process for the third book element. That is, the XSLT processor searches for a matching template, and instantiates that template when it is found.

After three instantiations of the book template, the XSLT processor returns to the first template (the template that matches the root node) and continues with the line after the xsl:apply-templates instruction.

How the select and match Attributes Are Different

Consider the following instructions:

<xsl:apply-templates select=expression/>
               
<xsl:template match=pattern/>
               

            

The xsl:apply-templates instruction uses the select attribute to specify an XPath expression. The xsl:template instruction uses the match attribute to specify an XPath pattern.

When the XSLT processor reaches an expression that is the value of a select attribute, it evaluates the expression relative to the current node. The result of the evaluation is that the XSLT processor selects a set of nodes to be processed.

When the XSLT processor reaches a pattern that is the value of a match attribute, it evaluates the pattern alone. The result of the evaluation is that the XSLT processor determines whether or not the pattern matches the node already selected for processing.

For example, suppose you have the following instruction:

<xsl:apply-templates select="/bookstore/book"/>
               

            

This instruction selects the book elements for processing. For each book element, the XSLT processor searches for a template that matches the book element. The following template matches the book element because the pattern identifies all elements that contain author elements. Because book elements contain author elements, this template is a match:

<xsl:template match="*[author]">
               

              
<td><xsl:value-of select="author"/></td>
                   

                
</xsl:template>

This example shows that the expression that the XSLT processor uses to select nodes and the pattern it uses to match nodes are independent of each other.

 
Free Stylus Studio XML Training: