| Previous | Next
ModesSometimes the same input content needs to appear multiple times in the output document, formatted according to a different template each time. For instance, the titles of the chapters in a tutorial would be formatted one way in the chapters themselves and a different way in the table of contents. Both Example 8-12. A stylesheet that uses modes<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="people"> <html> <head><title>Famous Scientists</title></head> <body> <ul><xsl:apply-templates select="person" mode="toc"/></ul> <xsl:apply-templates select="person"/> </body> </html> </xsl:template> <!-- Table of Contents Mode Templates --> <xsl:template match="person" mode="toc"> <xsl:apply-templates select="name" mode="toc"/> </xsl:template> <xsl:template match="name" mode="toc"> <li><xsl:value-of select="last_name"/>, <xsl:value-of select="first_name"/></li> </xsl:template> <!-- Normal Mode Templates --> <xsl:template match="person"> <p><xsl:apply-templates/></p> </xsl:template> </xsl:stylesheet> Example 8-13 shows the output when this stylesheet is applied to people.xml. The people template in Example 8-12 applies templates to its Example 8-13. Output from a stylesheet that uses modes to process each person twice with different templates<html> <head> <title>Famous Scientists</title> </head> <body> <ul> <li>Turing, Alan</li> <li>Feynman, Richard</li> </ul> <p> Alan Turing computer scientist mathematician cryptographer </p> <p> Richard P Feynman physicist Playing the bongoes </p> </body> </html> For every mode you use in the stylesheet, the XSLT processor adds one default template rule to its set of built-in rules. This applies to all element and root nodes in the specified mode and applies templates to their children in the same mode (since the usual built-in template rule for element and root nodes doesn't have a mode). For instance, the extra default rule for Example 8-10 looks like this: <xsl:template match="*|/" mode="toc"> <xsl:apply-templates mode="toc"/> </xsl:template> |