(Excerpt from "The MathML Handbook" by Pavi Sandhu)
The stylesheet shown in the example An XSLT stylesheet that uses xsl:apply-templates did not contain a template for the publisher element. Yet, the output from this stylesheet includes the text contained in the child elements of the publisher element, namely name, address, and phone. The reason for this is that XSLT defines a default template that is automatically applied to any elements in the document for which no explicit template is given. The default template looks like the following:
<xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template>
The value of the match attribute in the template element is an XPath expression that identifies a specific part of the document. In XPath syntax, the * refers to any element in the current context, while / refers to the root element. The vertical bar is used to separate two choices. Hence, the default template shown above means that the xsl:apply-templates element should be applied to all child elements as well as the root element in the document.
If this default template is applied to any element in the document, its effect is to write out into the output document any text contained in that element. This is because XSLT also defines the following default template for the text or attributes of elements:
<xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template>
The match attribute here is set to the XPath pattern text()|@*. In XPath syntax, text() stands for all text in the current context. and @* stands for any attribute.
The following example shows a simple XSLT stylesheet that illustrates the effect of the default templates:
Example: An XSLT stylesheet that illustrates the concept of default templates.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="book"> <html> <body> <p><xsl:apply-templates/></p> </body> </html> </xsl:template> </xsl:stylesheet>
When the above stylesheet is applied to example An XML document with several nested elements, the following output document is obtained:
<html> <body> <p> The MathML Handbook Pavi Sandhu Charles River Media 20 Downer Avenue, Hingham, MA 02043 781-740-0400 </p> </body> </html>
Notice that the text of the title, author, publisher, name, address, and phone elements appears in the output document, even though the stylesheet does not contain explicit templates for these elements. This behavior is a result of the XSLT processor applying the default templates for these elements.
Although the examples discussed so far are very simple, they illustrate some of the key features common to any XSLT stylesheet. A more elaborate XSLT stylesheet has the same basic structure. It would differ only in the number of templates and the complexity of the processing rules defined in each template.
|<< back||next >>|
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