<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../../_utils/schema/yaps.rnc" type="application/relax-ng-compact-syntax"?>
<?xml-model href="../../../_utils/schema/yaps.isosch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-stylesheet type="text/xsl" href="yaps2slidy1.xslt"?>
<!-- $Id: mdh_xsl_modes.xml 51709 2026-05-28 23:29:13Z syd $ -->
<TEI xmlns="http://www.wwp.northeastern.edu/ns/yaps" version="5.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Template Modes</title>
        <author>Martin Holmes</author>
      </titleStmt>
      <editionStmt>
        <edition>DHSI 2013, University of Victoria</edition>
      </editionStmt>
      <publicationStmt>
        <distributor>Women Writers Project (via website)</distributor>
        <address>
          <addrLine>url:mailto:wwp@northeastern.edu</addrLine>
        </address>
        <date when="2013-06-08"/>
        </publicationStmt>
      <sourceDesc>
          <p>Born digital</p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <change when="2011-06-01" who="mdh">Began writing this file.</change>
    </revisionDesc>
  </teiHeader>
  <text>
    <presentation>
      <section>
        <head>Template Modes</head>
        <slide>
          <list type="incremental">
            <item>You know how to write templates to process nodes. Typically, you have one template to process <tag>head</tag> nodes, another to process <tag>p</tag> nodes, and so on.</item>
            <item>However, sometimes you want to process the same node in different ways.</item>
            <item>One effective way to do this is using the <att>mode</att> attribute.</item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Why process the same node in two different ways?</head>
        <slide>
          <p>Imagine you have a document structured like this:</p>
            <eg><![CDATA[
<body>
    <div type="chapter" n="1">
        <head>Chapter 1: How it all started</head>
        <p>[...]</p>[...]<p>[...]</p>
    </div>
    <div type="chapter" n="2">
        <head>Chapter 2: What happened next</head>
        <p>[...]</p>[...]<p>[...]</p>
    </div>
    <div type="chapter" n="3">
        <head>Chapter 3: Things that subsequently transpired</head>
        <p>[...]</p>[...]<p>[...]</p>
    </div>
    [...]
</body>
 ]]></eg>
        </slide>
      </section>
      
      <section>
        <head>Why process the same node in two different ways?</head>
        <slide>
          <list type="incremental">
            <item>Each <tag>head</tag> element needs to be processed in a particular way when it shows up in its chapter.</item>
            <item>However, you also want to create a table of contents, based on a list of the <tag>head</tag> elements.</item>
            <item>The <tag>head</tag>s will need to be processed differently for the TOC.</item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Two templates for the same node</head>
        <slide>
          <p>Using modes, we can write two different templates for the same node:</p>
          <eg><![CDATA[
<xsl:template match="head">
  <h2]]><hi rend="css(color: red;)"> id="chapter_{parent::div/@n}"</hi><![CDATA[><xsl:apply-templates /></h2>
</xsl:template>            
          ]]></eg>
          <eg><![CDATA[
<xsl:template match="head"]]><hi rend="css(color: red;)"> mode="toc"</hi><![CDATA[>
  <li>
    <a]]><hi rend="css(color: red;)"> href="#chapter_{parent::div/@n}"</hi><![CDATA[><xsl:apply-templates /></a>
  </li>
</xsl:template>            
          ]]></eg>
          
        </slide>
      </section>
      
      <section>
        <head>Two templates for the same node</head>
        <slide>
          <p>The first template, which processes the <tag>head</tag> at the beginning of the chapter, creates an <tag>h2</tag> element with a unique id:</p>
          <eg><![CDATA[
<xsl:template match="head">
  <h2]]><hi rend="css(color: red;)"> id="chapter_{parent::div/@n}"</hi><![CDATA[><xsl:apply-templates /></h2>
</xsl:template>            
          ]]></eg>
        </slide>
      </section>
      
      <section>
        <head>Two templates for the same node</head>
        <slide>
          <p>The second template, which processes the <tag>head</tag> element in the context of a table of contents, creates a list item (<tag>li</tag>) element containing a link which points to the chapter heading:</p>
          <eg><![CDATA[
<xsl:template match="head"]]><hi rend="css(color: red;)"> mode="toc"</hi><![CDATA[>
  <li>
    <a]]><hi rend="css(color: red;)"> href="#chapter_{parent::div/@n}"</hi><![CDATA[><xsl:apply-templates /></a>
  </li>
</xsl:template>            
          ]]></eg>
         
        </slide>
      </section>
      
      <section>
        <head>Creating the table of contents</head>
        <slide>
          <eg><![CDATA[
<div id="tableOfContents">
  ]]><hi rend="css(color: blue;)">&lt;ul class="tocList"&gt;</hi><![CDATA[
            <xsl:apply-templates]]><hi rend="css(color: red;)"> select="//div[@type='chapter']/head"</hi><hi rend="css(color: darkgreen;)"> mode="toc"</hi><![CDATA[/>
  ]]><hi rend="css(color: blue;)">&lt;/ul&gt;</hi><![CDATA[
</div>
          ]]></eg>
        </slide>
      </section>
      
      <section>
        <head>Results</head>
        <slide>
        <p>This is what the output might look like:</p>
          <figure><graphic url="../../../_utils/gfx/mdh_xsl_modes_toc.png" width="862px" height="424px"/></figure>
        </slide>
      </section>
      
      <section>
        <head>More on modes</head>
        <slide>
          <list type="incremental">
            <item>One template can serve multiple modes:
              <eg><![CDATA[
  <xsl:template match="head" mode="toc index abstract">
   ]]></eg>
            </item>
            <item>There are two special mode values you can apply to an <tag>xsl:template</tag>:
            <list type="incremental">
              <item><emph>#all</emph> (applies to all modes)</item>
              <item><emph>#default</emph> (applies when no <att>mode</att> attribute is specified)</item>
            </list>
            </item>
            <item>There is one special mode value you can apply to an <tag>xsl:apply-templates</tag>:
            <list type="incremental">
              <item><emph>#current</emph> (apply templates using whatever mode is current in the processing cascade)</item>
            </list>
            </item>
            <item>These only really become useful when you are using lots of modes throughout a transformation.</item>
          </list>
        </slide>
      </section>
      
    </presentation>
  </text>
</TEI>
