Modes
Sometimes 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 book would be
formatted one way in the chapters themselves and a different way in
the table of contents. Both xsl:apply-templates and xsl:template
elements can have optional mode
attributes that connect different template rules to different
positions. A mode attribute on an
xsl:template element identifies in
which mode that template rule should be activated. An xsl:apply-templates element with a mode attribute only activates template rules
with matching mode attributes.
Example 8-12 demonstrates
with a stylesheet that begins the output document with a list of
people’s names. This is accomplished in the toc mode. Then a separate template rule, as
well as a separate xsl:apply-templates element in the default
mode (really no mode at all), outputs the complete contents of all
person elements.
<?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> ...