Applying templates

(Excerpt from "The MathML Handbook" by Pavi Sandhu)

An XSLT stylesheet must be a well-formed and valid XML document. All XSLT element and attribute names in the document should be associated with the XSL namespace, which is usually indicated by the prefix xsl. The root element of the document is called xsl:stylesheet. A typical XSLT stylesheet therefore has the following structure:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ...
</xsl:stylesheet>

The most important part of any XSLT stylesheet is its templates. Each template specifies a set of instructions about how to process a particular type of element in the input document. A template has the following structure:

<xsl:template match="expression">
  ...
</xsl:template>

The value of the match attribute in the xsl:template element is an XPath pattern. This is a specific type of XPath expression that specifies the type of content in the input document to which the template should be applied. For example, a template with match="title" contains instructions to be applied to all elements called title in the current context. The current context depends on what node in the document is being processed by the XSLT processor when the template is matched. Let’s look at a simple example of an XML document.

Example: A simple XML document.

<book>
  <title>The MathML Handbook</title>
</book>

The following example shows a simple XSLT stylesheet that converts the above document into HTML.

Example: An XSLT stylesheet that contains a single template.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="book">
    <html>
      <body>
        <h1><xsl:value-of select="title"/></h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

The following example shows a stylesheet written for example An XML document with several nested elements. It is possible to write a simpler stylesheet to do the same transformation, but the one shown here is useful for the purposes of illustration.

Example: An XSLT stylesheet that uses xsl:apply-templates.

Here is the result of applying the above stylesheet to the above XML document:

<html>
  <body>
    <h1>The MathML Handbook</h1>
  </body>
</html>

The above stylesheet consists of a single template that picks out the book element in the input document and then does the following things:

  • It first copies into the output the literal text that corresponds to the opening tags for the html, body, and h1 elements.
  • Next, the <xsl:value-of select="title"> element is applied. The effect of this element is to copy into the output document the text of any title element in the current context.
  • Finally, the template writes out the closing tags for the html, body, and h1 elements.

The next example shows a slightly more complicated XML document than the one before.

Example: An XML document with several nested elements.

<book>
  <title>The MathML Handbook</title>
  <author>Pavi Sandhu</author>
  <publisher>
    <name>Charles River Media</name>
    <address>20 Downer Ave., Hingham, MA 02043</address>
    <phone>781-740-0400</phone>
  </publisher>
</book>

The following example shows a stylesheet written for the example An XML document with several nested elements. It is possible to write a simpler stylesheet to do the same transformation, but the one shown here is useful for the purposes of illustration.

Example: An XSLT stylesheet that uses xsl:apply-templates.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="book">
    <html>
      <body><xsl:apply-templates/></body>
    </html>
  </xsl:template>
  <xsl:template match="title">
    <h1><xsl:value-of select="."/></h1>
  </xsl:template>
  <xsl:template match="author">
    <h2><xsl:value-of select="."/></h2>
  </xsl:template>
  <xsl:template match="name">
    <h3><xsl:value-of select="."/></h3>
  </xsl:template>
  <xsl:template match="address">
    <p><xsl:value-of select="."/></p>
  </xsl:template>
  <xsl:template match="phone">
    <p><xsl:value-of select="."/></p>
  </xsl:template>
</xsl:stylesheet>

Here is the output that results from applying the above stylesheet to the example An XML document with several nested elements:

<html>
  <body>
    <h1>The MathML Handbook</h1>
    <h2>Pavi Sandhu</h2>
    <h3>Charles River Media</h3>
    <p>Address: 20 Downer Avenue, Hingham, MA 02043</p>
    <p>Phone: 781-740-0400</p>
  </body>
</html>

The above stylesheet contains templates for many of the elements in the original XML document. Each template contains some literal text to be copied to the output document as well as an XSLT element that specifies a particular instruction to be executed. The xsl:apply-templates element in the template for the book element specifies that any templates found for all child elements of the current element should be applied. All the other templates contain an <xsl:value-of select="."> element. The . in the attribute value is an XPath pattern that refers to the current element being processed.

To understand the order in which templates are applied, it is useful to know how the input document is processed. The XSLT processor represents the input document as a tree consisting of nodes. Different types of nodes represent elements, attributes, text, comments, and so on. The XSLT processor then traverses the document tree from top to bottom. For each node in the document, the processor checks to see if a matching template is defined in the stylesheet. If a template matching that node is found, the instructions contained in that template are executed. If more than one template matching that node is found, the more specific template is used. After a template is executed, the processor moves on to the next node in the document tree.

One consequence of this traversal order is that the order in which the templates are written in the stylesheet is not important. A specific template is applied when it matches an element in the document tree, regardless of where that template occurs in the stylesheet. Hence, if you edit a stylesheet to change the order of some of its templates, this does not affect the output document that is produced.

Note that more than one template can be active at any given time because when a template for an element is applied, the template remains active until templates for all the child elements have been applied. Hence, one template can call another template, which can call another template, and so on. The processing of templates can therefore occur in a recursive manner, reflecting the nested structure of the document tree.

   

<< back next >>

 

 

 

Tipp der data2type-Redaktion:
Zum Thema MathML bieten wir auch folgende Schulungen zur Vertiefung und professionellen Fortbildung an:

 

Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America