<?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: xslt_intro.xml 31185 2017-03-24 23:41:46Z syd $ -->
<TEI xmlns="http://www.wwp.northeastern.edu/ns/yaps" version="5.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>A Gentle Introduction to XSLT</title>
        <author xml:id="jf" ref="p:jflanders.lfw">Julia Flanders</author>
        <author xml:id="sb" ref="p:sbauman.emt">Syd Bauman</author>
      </titleStmt>
      <editionStmt>
        <edition>DHSI 2017, University of Victoria</edition>
      </editionStmt>
      <publicationStmt>
        <distributor>Women Writers Project (via website)</distributor>
        <address>
          <addrLine>url:mailto:wwp@neu.edu</addrLine>
        </address>
        <date when="2017-06-12"/>
        <availability status="restricted">
          <p>Copyright 2012 Syd Bauman, Julia Flanders, 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>A very gentle introduction to basic concepts of XSLT</p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <change when="2017-03-24" who="#sb">fixed a few links</change>
      <change when="2016-01-04" who="personography.xml#ksmith.awh">Added tutorials on all
      slides</change>
      <change who="#jf" when="2012-11-29">Created file from scratch</change>
    </revisionDesc>
  </teiHeader>
  <text>
    <presentation>
      <abstract>
        <p>This tutorial discusses how to begin an XSLT stylesheet and some basic
        transformations one can perform with XSLT. This tutorial provides important
        conceptual information about how an XSLT stylesheet processes information, which
        is necessary to understand how to structure a stylesheet.</p>
      </abstract>
      <section>
        <head>What does an XSLT stylesheet do?</head>
        <slide>
          <figure>
            <graphic height="600px"
                     url="../../../_utils/gfx/anatomy_of_transformation1.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>Note!! at this stage we are talking very imprecisely and impressionistically
          about XSLT, for the sake of making it clearer and learning things one at a
          time. We'll add precision later. </p>
          <p>At its simplest: it takes an input file (an XML tree structure) and
          transforms it: changes its form somehow.</p>
          <p>It does this by taking the input tree, piece by piece, and applying
          transformation rules to each piece: these rules say what to do with each
          piece of input</p>
          <p>We call these transformation rules "templates", and an XSLT stylesheet is
          basically a set of templates, each one with a condition of operation (when
          do I happen?) and its own specific task</p>
          <p>These tasks will take place if the conditions of operation are met, and not
          if not.</p>
        </lectureNote>
        <tutorial>
          <p>For the sake of clarity, we are going to be talking very imprecisely and
          impressionistically about XSLT. We'll add precision later. At its simplest,
          an XSLT stylesheet takes an input file (an XML tree structure) and
          transforms it or changes its form somehow. It does this by taking the input
          tree, piece by piece, and applying transformation rules to each piece. These
          rules say what to do with each piece of input. We call these transformation
          rules "templates", and an XSLT stylesheet is basically a set of templates,
          each one with a condition of operation (when do I happen?) and its own
          specific task (what do I do?). These tasks will take place if and only if
          the conditions of operation are met.</p>
          <p>In this slide, we see a very simple XML input on the top-left and a
          corresponding HTML output on the top-right. The boxes at the bottom of the
          slide are representations of the templates in an XSLT file: They are the
          conditions by which the XML input is transformed into an HTML output.</p>
          <p>You may notice that some of the stylesheet conditions are not met in the the
          input document. For example, there is no <gi>emph</gi> in the imput
          document, so that operation (and it's corresponding HTML <gi>em</gi> element
          it would output) is not carried out.</p>
        </tutorial>

      </section>
      <section>
        <head>A very simple example</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xslt_start1.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>Here's a very, very simple XML document, and a very very simple stylesheet.
          Looking at this stylesheet, what do you think it's going to do?</p>
          <p>OK, so let's look at how it does this, breaking it down into steps.</p>
        </lectureNote>
        <tutorial>
          <p>In this slide, we have a very simple XML document and a very simple XSLT
          stylesheet. Take a minute to think about what the output document might look
          like if we apply this stylesheet to the input document on the left.</p>
          <p>As you probably can guess, the stylesheet shown on the right is going to
          transform the input tree shown on the left into the elements within the
          <gi>xsl:template</gi>. Over the next two slides, we will go over this
          process step-by-step</p>
        </tutorial>
      </section>

      <section>
        <head>Build an output tree …</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xslt_start2.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>The first thing that happens is that we build an output tree--a structural
          skeleton for the file we are <emph>creating</emph>. The output tree is made
          up of pieces of code that are embedded in the XSLT stylesheet</p>
          <p>So if we look at this diagram, we can see that the stylesheet contains a
          whole little HTML document; <list>
          <item>when we run the stylesheet, we say in effect "I have a paragraph
          here, what do I do when I have a paragraph?"</item>
          <item>the stylesheet answers "aha! when I have a paragraph, I create an
          output tree like this..."</item>
          <item>Notice that our output tree can include some pre-defined content
          (like boilerplate) but at this point there's still no content
          <emph>from the input tree</emph> present in the output. </item>
        </list>
          </p>
        </lectureNote>
        <tutorial>
          <p>The first thing that happens in this example is that we build an output
          tree—a structural skeleton for the file we are creating. If we look at this
          diagram we can see that the stylesheet contains an entire miniature HTML
          document within this template.</p>
          <p>When we run this particular stylesheet, we say, in effect <said>I have a
          paragraph here. What do I do with a paragraph?</said> The stylesheet
          answers, <said>When I have a paragraph, I create an output tree like
          this!</said></p>
          <p>Notice that the output tree can include some predefined content (like the
          <gi>head</gi> and <gi>title</gi> in the example). </p>
          <p>But where does the content of our input document (i.e. <quote>Hello,
          World!</quote>) show up in our output document?</p>
        </tutorial>
      </section>

      <section>
        <head>… then fill it with data</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xslt_start3.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>Once we have the output tree, we next have to populate it with content from
          the input. For this step, there's a special part of the stylesheet that
          serves as a kind of conduit for content to come from the input and get
          placed in the output. We can think of this <gi>xsl:apply-templates</gi>
          component as being sort of like a variable or a placeholder within the
          stylesheet: it says "right here, content is going to flow from the input
          tree into this place in the output tree." Or maybe it says, more
          imperatively "Go get that content, apply templates to it, and put it in the
          output tree!" In a very simple scenario like this one, that content is very
          simple and the templates being applied are very simple, so the process is
          easy to follow: <list>
          <item>we go back to the input document, to the element that we matched
          earlier (i.e. <gi>paragraph</gi>) and we get the content from
          there</item>
          <item>then we put it into the appropriate place in the output
          tree</item>
        </list>
        http://2.bp.blogspot.com/-l23P6-iCPNc/TfDqRvDzWsI/AAAAAAAAAUw/JjaV5_QVzRg/s1600/fetchblog3.jpg
          </p>
        </lectureNote>
        <tutorial>
          <p>Once we have the output tree, we need to fill it with the content of the
          input tree. As you may have guessed, the <gi>xsl:apply-templates</gi>
          element allows us to do precisely this. Think of it as a conduit from the
          input tree to the output tree. It instructs the stylesheet to replicate the
          content of the input tree within the output tree that you have created in
          your stylesheet. In this case, it grabs the content of the matched
          <gi>paragraph</gi>, <quote>Hello, World!</quote>, and puts it in the
          appropriate place in the output tree (in the HTML <gi>p</gi> element).</p>
          <p>The processor works much like the dog playing fetch in this slide. The
          <gi>xsl:templates</gi> instructs the processor to fetch the content (in
          this case, text).</p>
          <p>We can imagine more complex examples, where the content of <gi>paragraph</gi>
          is not only text, but additional elements that one may want to process in
          some way. A more complex example like this will be covered in the next
          slide.</p>
        </tutorial>
      </section>

      <section>
        <head>A more detailed example</head>
        <slide>
          <figure>
            <graphic height="600px"
                     url="../../../_utils/gfx/xslt_processing_model_simple.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>So now let's take a closer look at the stylesheet...</p>
          <p>Here's another view of our first scenario, this time with the actual data
          showing in XML notation</p>
          <p>This slide will be useful to come back to, but let's look closely at a few
          specific pieces in an introductory way...we'll come back to them in more
          detail later on.</p>
          <p>First, let's look at the templates themselves: <list>
          <item>templates in the stylesheet (those instructions we saw),
          represented by <gi>xsl:template</gi></item>
          <item>the <att>match</att> attribute: tells us what element in the input
          tree we are dealing with: this is also the "if", the "condition"
          that has to be met. The <att>match</att> attribute says basically
          "is there such an element? If so..."</item>
          <item>let's also note the fact that the value of @match is a location in
          the input tree; we can specify a context as well and we'll see how
          to do this later (don't gloss this yet)</item>
        </list>
          </p>
          <p>How about what a template <emph>does?</emph> Inside these templates here we
          have basically two things: <list>
          <item>a snippet of the output tree (in this case, HTML): these are
          basically the information skeleton of the output document</item>
          <item>an instruction to "apply templates": what this means is, "keep
          going!" "don't stop here". Another more precise way of interpreting
          this element is to say "go ahead and process any children of the
          matched element": in other words, keep drilling down into the input
          tree. Without this instruction, the stylesheet logic reaches a dead
          end.</item>
          </list> Note that both of these are optional: might be absent... </p>
        </lectureNote>
        <tutorial>
          <p>Let's take a closer look at an example with more elements to transform. This
          slide will be useful to come back to as we continue on throughout this
          tutorial, but let's have an introductory look now. We'll add more details
          later.</p>
          <p>Looking at the templates themselves: You will notice that each template in
          the stylesheet is represented by an <gi>xsl:template</gi> element. The
          @match attribute tells us what element on the input tree we are dealing
          with. This attribute essentially specifies a condition that has to be met:
          "if we find the "x" element, perform this transformation." It is important
          to note that the value of @match specifies a location on the tree. This
          means that we can provide different rules for elements when they occur in
          different contexts. We can treat a <gi>persName</gi> that occurs in
          <gi>body</gi> differently than we treat a <gi>persName</gi> that occurs
          in the <gi>teiHeader</gi>. We will learn how to specify the context of the
          element later in this primer.</p>
          <p>Now that we know how the <gi>xsl:template</gi> interacts with the input tree,
          we should ask: what does the template do? Inside these templates, there is a
          snippet of the output tree (in this case, html). There is also the
          <gi>xsl:apply-templates</gi> element, which essentially indicates to the
          stylesheet: <q>don't stop here! go back and process the children or spit out
          text!</q> Another, more precise way of interpreting this element is to
          say "go ahead and process any children of the matched element." Without this
          instruction, the stylesheet logic reaches a dead end.</p>
        </tutorial>
      </section>

      <section>
        <head>What order do things happen?</head>
        <slide>
          <figure>
            <graphic height="600px"
                     url="../../../_utils/gfx/xslt_processing_model_simple_process.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>The trickiest thing about XSLT is perhaps understanding the order in which
          things happen, and the logic that determines what happens next. It's
          important to understand this because otherwise you can get very puzzling
          results. </p>
          <p>We're used to thinking of computer programs as sets of steps that will happen
          in order, where the sequence of events is determined by the computer program
          itself. XSLT is different, in that the order of steps is primarily
          determined by the input tree: <list>
          <item>You start with the root of the input tree (for now, we'll pretend
          that's the root element)</item>
          <item>Then you go to the stylesheet to look for the template that
          matches that root.</item>
          <item>When you find the template that matches, you do what it
          says</item>
          <item>And if it contains an instruction to continue applying templates,
          then you go back to the input tree and consider the children of that
          matched element.</item>
          <item>And for each child, you go back to the stylesheet and see whether
          there are any templates that match it.</item>
        </list>
          </p>
        </lectureNote>
        <tutorial>
          <p>Perhaps the trickiest thing about XSLT is understanding the order in which
          things happen, and the logic that determines what comes next. It is
          important to understand this because, otherwise, you may get some puzzling
          results.</p>
          <p>The processor begins with the root of the input tree (for now, we'll pretend
          that's also the root element of the document). Then the processor goes to
          the stylesheet to look for the template that matches that root. When it
          finds the matching template, it does what the stylesheet says.</p>
          <p>If the template contains an instruction to continue applying templates, the
          processor goes back to the input tree to consider the children of the
          matched element. For each child, it goes back to the stylesheet to see if
          there are templates to match it. And on and on until it reaches the end of
          the stylesheet.</p>
          <p>We might be used to thinking of computer programs as sets of steps that will
          happen in order, where the sequence of events is determined by the computer
          program itself. XSLT is different in that the ordering of the steps is
          determined by the input tree. As you can see here in the example, since
          <gi>opener</gi> appears in the input document before <gi>l</gi>, the
          <gi>opener</gi> template is applied first, even though it appears after
          the <gi>l</gi> template in the stylesheet. Thus, the <gi>h2</gi> appears
          before the two <gi>p</gi> elements in our output document.</p>
        </tutorial>
      </section>
      <section>
        <head>Recursion and dead ends</head>
        <slide>
          <figure>
            <graphic height="600px"
                     url="../../../_utils/gfx/xslt_processing_model_simple_process1.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>So the process is <emph>recursive</emph>, in that you keep drilling down into
          the input tree and processing the children, and then their children, and
          then their children</p>
          <p>But it can also come to a dead end: that drilling process requires that we
          receive an instruction to keep applying templates</p>
          <p>So in this diagram what we're seeing is how in fact the "apply templates" at
          step 3 is really the key to accessing all of the other templates in the
          stylesheet. If that instruction isn't there, nothing else happens. We never
          get to find out whether the <gi>opener</gi> or the <gi>l</gi> or
          <gi>byline</gi> is matched.</p>
        </lectureNote>
        <tutorial>
          <p>So the process is <emph>recursive</emph> in that you keep drilling down the
          input tree and processing the children, and then their children, and so on.
          But it can also come to a dead end. The drilling-down process requires that
          we receive an instruction to continue applying templates (using the
          <gi>xsl:apply-templates</gi> element).</p>
          <p>In the diagram, we can see that the <gi>xsl:apply-templates</gi> instruction
          is really the key to everything past step 3. It is this element that allows
          the stylesheet to process any children of <gi>lg</gi> (i.e. <gi>opener</gi>,
          <gi>l</gi>, and <gi>byline</gi>). If this element is absent, nothing
          else happens. The stylesheet isn't instructed to process more children, so
          the children are ignored.</p>
          <p>Note that although this diagram does not show how each <gi>l</gi> element is
          processed, each would treated in the same way as the first two in the output
          document (that is, transformed into HTML <gi>p</gi> elements.</p>
        </tutorial>
      </section>

      <section>
        <head>Over to a real example …</head>
        <slide>
          <p>Let's look at this same example in oXygen.</p>
        <list>
            <!-- <item><ref target="http://wwp.northeastern.edu/outreach/seminars/_current/demos/publishing/hello_world.xml">hello_world.xml</ref></item> -->
            <!-- <item><ref target="http://wwp.northeastern.edu/outreach/seminars/_current/demos/publishing/hello_world.xslt">hello_world.xslt</ref></item> -->
            <item><ref target="../../demos/publishing/verse.xml">verse.xml</ref></item>
            <item><ref target="../../demos/publishing/verse.xslt">verse.xslt</ref></item>
          </list>
        </slide>
        <tutorial>
          <p>Let's try a real example. For this example, you should have &lt;oXygen&gt;
          downloaded. If you have not set it up already, please visit the <ref
          target="../../../../resources/tutorial_setup.html">tutorial setup page</ref>.</p>
          <p>In our workshops, we have students download and open these two files: <list>
          <item><ref target="../../demos/publishing/verse.xml">verse.xml</ref></item>
          <item><ref target="../../demos/publishing/verse.xslt">verse.xslt</ref></item>
        </list>
          </p>
          <p>Once you open these files in Oxygen, you will also need to go to the XSLT
          debugger. This ought to be in the upper right hand corner of the
          &lt;oXygen&gt; window, and should say <q>XSLT.</q></p>

          <p>If you have not used the XSLT debugger in Oxygen yet, we recommend that you
          use the <ref
          target="../xslt/djb_transformations-in-oxygen_tutorial_00.xhtml">XSLT
          Debugger</ref> tutorial to get yourself set up and comfortable with the
          debugger view.</p>
          <p>These are the same input and XSLT files we've been looking at throughout this
          tutorial. In the Verse example, run the transformation (with the blue arrow)
          to see the XML file transformed into XHTML (your output document). If you
          choose an output file and location with the XSLT debugger, you can then open
          that file in a browser if you wish (or enable XHTML output in the debugger
          to view the file in Oxygen). With these test files, you can play around with
          the input document (verse.xml) and the XSLT stylesheet to make changes to
          the output document.</p>
          <list>
            <head>This tutorial is complete, please see links below to continue:</head>
            <item><ref target="../xslt/djb_transformations-in-oxygen_tutorial_00.xhtml"
                       >Proceed to XSLT Debugger Tutorial</ref></item>
            <item><ref target="./xslt_processing_tutorial_00.xhtml">Proceed to next
            tutorial in Transformation and Publication Primer</ref></item>
            <item><ref target="../../../../resources/transformation.html">Return to
            Transformation and Publication Primer</ref></item>
            <item><ref target="../../../../resources/tutorial_main.html">Return to main
            tutorial page</ref></item>
          </list>
        </tutorial>
      </section>

    </presentation>
  </text>
</TEI>
