<?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: djb_built-in_template_rules.xml 51709 2026-05-28 23:29:13Z syd $ -->
<TEI xmlns="http://www.wwp.northeastern.edu/ns/yaps" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Built-in template rules and how to override them</title>
        <author>David J. Birnbaum</author>
      </titleStmt>
      <editionStmt>
        <edition>Introduction to XSLT for Digital Humanities, DHSI, 04-08 June 2012</edition>
      </editionStmt>
      <publicationStmt>
        <distributor>Women Writers Project (via website)</distributor>
        <address>
          <addrLine>url:mailto:wwp@northeastern.edu</addrLine>
        </address>
        <date when="2012-06-04"/>
        <availability status="restricted">
          <p>Copyright 2011 David J. Birnbaum and the Women Writers Project</p>
          <p>This TEI-encoded XML file is available under the terms of the <ref target="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons
          Attribution-ShareAlike 3.0 (Unported)</ref> license.</p>
        </availability>
        <pubPlace>Providence, RI USA</pubPlace>
      </publicationStmt>
      <sourceDesc>
        <p>XSLT built-in template rules</p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <change who="#syd" when="2012-03-10">Per request DJB, put wrapper element around wombat example output -- untested</change>
      <change who="#mholmes" when="2011-05-30">Reformatted some of the CDATA sections to add indenting to tags.</change>
    </revisionDesc>
  </teiHeader>
  <text>
    <presentation>
      <section>
        <head>What’s built in?</head>
        <slide>
          <list type="incremental">
            <item>Where do we start?
            <list><item>at the document node (itself the parent of the root <term>element</term>)</item></list>
            </item>
            <item>What happens when there is no applicable template rule?<list>
            <item>The built-in defaults are applied. <bibl>Kay, 79</bibl></item>
            </list></item>
            <item>What happens where there is exactly one applicable template rule?<list>
            <item>Er ... it gets applied.</item>
            </list></item>
            <item>What happens when more than one template rule would seem to apply?<list>
            <item>Built-in priority <bibl>Kay, 686</bibl></item>
            <item>User-specified priority <bibl>Kay, 483</bibl></item>
            </list></item>
          </list>
        </slide>
      </section>
      <section>
        <head>Launching a transformation</head>
        <slide>
          <p>Processing starts at the document node, which sits above the root element
          (the top-level element that contains all other nodes)</p>
          <p>In a TEI document, the root element is normally <gi>TEI</gi></p>
          <p>Built-in template rules will walk the tree and process the entire document
          (see below)</p>
          <p>Override the built-in rules by specifying your own to:<list>
          <item>Perform non-built-in processing</item>
          <item>Cause items not to be processed</item>
          </list></p>
        </slide>
      </section>

      <section>
        <head>Built-in template rules</head>
        <slide>
          <list>
            <item>Element: process children (elements and text nodes), applying
            applicable template rules (built-in or specified)</item>
            <item>Attribute: do nothing (no output)</item>
            <item>Text: output string value</item>
          </list>
          <bibl>Kay 79</bibl>
        </slide>
      </section>
      <section>
        <head>Priority</head>
        <slide>
          <p>Built-in priority: the rule with the most specific match is applied</p>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:template match="div">
              <act>
                <xsl:apply-templates/>
              </act>
            </xsl:template>

            <xsl:template match="div/div">
              <scene>
                <xsl:apply-templates/>
              </scene>
            </xsl:template>
          </egMarkup>
        </slide>
      </section>
      <section>
        <head>User-specified priority</head>
        <slide>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:template match="div" priority="10">
              <act>
                <xsl:apply-templates/>
              </act>
            </xsl:template>
          </egMarkup>
          <list>
            <item>Built-in priorities range from 0 to 0.5</item>
            <item>User-specified priorities may be any number (including
            negative)</item>
          </list>
        </slide>
      </section>
      <section>
        <head>Overriding a template rule</head>
        <slide>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:template match="/">
              <xsl:apply-templates select="//head"/>
            </xsl:template>
          </egMarkup>
          <list>
            <item>The built-in rule for any element is to process its child elements and
            text nodes.</item>
            <item>The built-in rule for the document node would process its single root
            element. In the case of a TEI document, that’s usually the single
            <gi>TEI</gi> node.</item>
            <item>In the example above, instead you grab all <gi>head</gi> elements
            everywhere. No other nodes are processed unless they’re inside a
            <gi>head</gi>.</item>
          </list>
        </slide>
      </section>
      <section>
        <head>How to use built-in template rules</head>
        <slide>
          <p>Should you write rules that duplicate the built-in?</p>
          <list>
            <item>Yes if you like to be reminded of what happens with every node.</item>
            <item>No if you find a briefer stylesheet more legible.</item>
          </list>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                            version="2.0">
              <xsl:output method="xml" indent="yes"/>
              <xsl:template match="head">
                <wombat><xsl:apply-templates/></wombat>
              </xsl:template>
            </xsl:stylesheet>
          </egMarkup>
          <list>
            <item>The preceding stylesheet will apply built-in templates to everything
            <emph>except</emph> to <gi>head</gi> elements</item>
            <item>For <gi>head</gi> elements in the input it will create <gi>wombat</gi>
            elements in the output.</item>
            <item>The contents of the original <gi>head</gi> elements will be processed
            according to the built-in template rules, with the output inserted into
            the new <gi>wombat</gi> element.</item>
            <item>(The result, BTW, is typically not well-formed XML — why not?)</item>
          </list>
        </slide>
      </section>
      <section>
        <head>Built-in templates and the wombat</head>
        <slide>
          <p><ref target="../../demos/xslt_intro/djb_wombat.xml"><name type="file">djb_wombat.xml</name></ref></p>
          <egMarkup scheme="ad-hoc" type="valid">
            <animals nationality="australian">
              <koala>The koala is also known as the marsupial bear.</koala>
              <bilby>The bilby has <size>long</size> ears, and is a symbol of Easter 
              in Australia, where rabbits are held in ill repute.</bilby>
              <wombat>The wombat is a <size>large</size> burrowing animal.</wombat>
              <echidna>The echidna, also known as the spiny anteater, is a 
              monotreme.</echidna>
              <platypus bill="duck">The platypus is an improbable beast.</platypus>
            </animals>
          </egMarkup>
          <p><ref target="../../demos/xslt_intro/djb_wombat.xsl">djb_wombat.xsl</ref></p>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
              <xsl:output method="xml" indent="yes"/>
              <xsl:template match="/">
                <animal-output><xsl:apply-templates/></animal-output>
              </xsl:template>
              <xsl:template match="wombat">
                <hooray-for-wombats>
                  <xsl:apply-templates/>
                </hooray-for-wombats>
              </xsl:template>
            </xsl:stylesheet>
          </egMarkup>
        </slide>
      </section>
      <section>
        <head>Any questions?</head>
        <slide>
          <figure>
            <figDesc>Uh-oh. There should be a picture of a wombat here!</figDesc>
            <graphic url="../../../_utils/gfx/wombat.jpg" height="100%" width="100%"/>
          </figure>
        </slide>
      </section>
    </presentation>
  </text>
</TEI>
