<?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_variables.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"
     version="5.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>XSL Variables</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="2016-06-13"/>
        <availability status="restricted">
          <p>Copyright 2017 Martin D. Holmes 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>
      </publicationStmt>
      <sourceDesc>
        <p>Born digital</p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <change when="2017-06-13" who="#mdh">Updates to account for changes in course sequence.</change>
      <change when="2012-06-06" who="#mdh">Removed @as (more complex than useful) and replaced example with one from the actual class.</change>
      <change when="2012-03-27" who="#sdb">Updated holding/retrieving sequence to not use <gi>for-each</gi></change>
      <change when="2012-03-27" who="#sdb">Fixed error in holding/retrieving sequence xmp</change>
      <change when="2011-05-31" who="mdh">Began writing this file.</change>
    </revisionDesc>
  </teiHeader>
  <text>
    <presentation>
      <section>
        <head>XSL Variables</head>
        <slide>
          <list>
            <item>XSL variables allow you to store values (strings, integers etc.) so that you can re-use them easily.</item>
            <item>XSL variables are NOT VARIABLE. Once you set the value, you're stuck with it.</item>
            <item>This is different from other programming languages, where variables can have different values assigned to them.</item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Creating an XSL variable</head>
        <slide>
          <egMarkup scheme="XSLT" type="valid">
	    <xsl:variable name="uvicName" 
			  select="'University of Victoria'" />
	  </egMarkup>
          <list>
            <item>The required <att>name</att> attribute gives us a
            way to refer to the variable later, using a dollar sign:
            <code>$uvicName</code>.</item>
            <item>The optional <att>select</att> attribute specifies a
            value that is assigned to the variable. More on this
            later.</item>
            <!--<item>The optional <att>as</att> attribute allows us to
		specify what type of value this is, to make sure that it's
		handled correctly when we use it in later expressions or
		calculations.</item>-->
          </list>
        </slide>
      </section>
      
      <section>
        <head>Assigning a value to your variable (1)</head>
        <slide>
          <list type="incremental">
            <item>A variable can hold lots of different value types,
            either hard-coded (as in the example above) or assigned
            through XPath expressions. Here are some examples:</item>
            <item>A hard-coded integer:
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="myAge" 
			    select="57" />
	    </egMarkup>
            </item>
            <item>The result of a calculation:
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="numCards" 
			    select="13 * 4 + 2" />
	    </egMarkup>
            </item>
            <item>A hard-coded string (note the single quotes inside the double quotes):
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="myName" 
			    select="'Martin Holmes'" />
	    </egMarkup>
            or
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="myName">Martin Holmes</xsl:variable>
	    </egMarkup>
            </item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Assigning a value to your variable (2)</head>
        <slide>
          <list type="incremental">
            <item>An element from the input document, found through XPath:
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="docTitle" 
			    select="/TEI/teiHeader/titleStmt/title[1]" />
	      <xsl:variable name="docAuthor" 
			    select="/TEI/teiHeader/titleStmt/author[1]" />
	    </egMarkup>
            </item>
            <item>A sequence of elements from the input document, found through XPath:
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="docAuthors" 
			    select="/TEI/teiHeader/fileDesc/titleStmt/author" />
	    </egMarkup>
            </item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>How to use an XSL variable (1)</head>
        <slide>
          <list type="incremental">
            <item>If your variable contains an atomic value such as a
            string or a number, you can output it with
            <tag>xsl:value-of</tag>:
            <egMarkup scheme="XSLT" type="valid">
	      My name is <xsl:value-of select="$myName"/>.
	    </egMarkup>
            </item>
            <item>You can do XPath calculations with the value of your variable:
            <egMarkup scheme="XSLT" type="valid">
	      In ten years I shall be <xsl:value-of select="$myAge + 10"/>.
	    </egMarkup>           
            </item>
            <item>If your variable contains an element, you can treat it just like an element.
            <egMarkup scheme="XSLT" type="valid">
	      This book was written by
	      <xsl:value-of select="$docAuthor/persName/forename"/>
	      <xsl:value-of select="$docAuthor/persName/surname"/>.
	    </egMarkup>           
            </item>
            
          </list>
        </slide>
      </section>
      
      
      
      <section>
        <head>How to use an XSL variable (2)</head>
        <slide>
          <list type="incremental">
            <item>If your variable contains a sequence of elements, you can treat it just like any sequence.
            <egMarkup scheme="XSLT" type="valid">
	      <p>This book was written by the following people:</p>
	      <ul>
		<xsl:apply-templates select="$docAuthors"/>
	      </ul>
	      <p>But <xsl:value-of select="$docAuthors[1]/persName/surname"/> is listed first.</p>
	    </egMarkup>
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:template match="author">
		<li>
		  <xsl:text>author #</xsl:text>
		  <xsl:value-of select="position()"/>
		  <xsl:text> is </xsl:text>
		  <xsl:apply-templates select="./persName/forename"/>
		  <xsl:text> </xsl:text>
		  <xsl:apply-templates select="./persName/surname"/>
		</li>
	      </xsl:template>
	    </egMarkup>           
            </item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Why use XSL variables? (1)</head>
        <slide>
          <p>XSL variables can be very convenient in a lot of different circumstances:</p>
          <list type="gloss" subtype="incremental">
            <label>When you have a static value that you want to
            use many times in your output.</label>
            <item>For
            instance, you might need to output the title of a document
            many times in different places. If you put it in an XSL
            variable, and the title changes, you only need to change
            it in one location in your XSLT.</item>
            
          </list>
        </slide>
      </section>
      
      <section>
        <head>Why use XSL variables? (2)</head>
        <slide>
          <list type="gloss" subtype="incremental">
            <label>To avoid running the same complicated operation many
            times during a transformation.</label>
            <item>For instance, you may want to use a regularized form of someone's name many times. Instead of doing this every time:
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:value-of select="//person[@xml:id='fred']/surname"/>
	      <xsl:text>, </xsl:text>
	      <xsl:value-of select="//person[@xml:id='fred']/forename"/>
	    </egMarkup>
            
            you can store the result in a variable and just use:
            
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:value-of select="$fredName"/>
	    </egMarkup>
            
            </item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Why use XSL variables? (3)</head>
        <slide>
          <list type="gloss" subtype="incremental">
            <label>When you need to disambiguate contexts in a
            complex XPath expression.</label>
            <item> For instance:
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:variable name="maxGeos" select="max(//place/count(descendant::geo))"/>
	      <xsl:value-of select="//place[count(descendant::geo) = $maxGeos]/@xml:id"/>
	    </egMarkup>
            
            <egMarkup scheme="XSLT" type="valid">
	      <xsl:for-each select="ref">
		<xsl:variable name="targetBiblId" select="substring-after(@corresp, '#')" />
		<xsl:value-of select="//back/div[@xml:id='bibliography']/listBibl/
				      bibl[@xml:id=$targetBiblId]/title"/>
	      </xsl:for-each>
	    </egMarkup>          
            </item>
          </list>
        </slide>
      </section>
      
      <section>
        <head>Where to put your XSLT variables</head>
        <slide>
          <p>You can put a variable in lots of places, including</p>
          <list type="gloss" subtype="incremental">
            <label>Inside the root &lt;xsl:stylesheet&gt; element</label>
            <item>This makes the variable GLOBAL.</item>
            <label>As a child of &lt;xsl:template&gt;</label>
            <item>This makes the variable accessible everywhere in that template.</item>
            <label>Inside another &lt;xsl:variable&gt; element</label>
            <item>A variable is always accessible only at the level it is declared.</item>
      </list></slide></section>
      
      <section>
        <head>Task</head>
        <slide>
          <list>
            <item>Declare a variable in the root of your XSLT.</item>
            <item>Set its value to something taken from the teiHeader of your document.</item>
            <item>Use the variable in your output.</item>
          </list>
        </slide>
      </section>
      
    </presentation>
  </text>
</TEI>
