title: In what order do templates in an XSLT document execute, and do they match on the source XML or the buffered output?
body: <p>Here is something that has always mystified me about XSLT:</p>
  
  <ol>
  <li>In what order do the templates execute, and</li>
  <li>When they execute, do they match on (a) the original source XML, or (b) the current output of the XSLT to that point?</li>
  </ol>
  
  <p>Example:</p>
  
  <pre><code>&lt;person&gt;
    &lt;firstName&gt;Deane&lt;/firstName&gt;
    &lt;lastName&gt;Barker&lt;/lastName&gt;
  &lt;/person&gt;
  </code></pre>
  
  <p>Here is a fragment of XSLT:</p>
  
  <pre><code>&lt;!-- Template #1 --&gt;
  &lt;xsl:template match="/"&gt;
    &lt;xsl:value-of select="firstName"/&gt; &lt;xsl:value-of select="lastName"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;!-- Template #2 --&gt;
  &lt;xsl:template match="/person/firstName"&gt;
    First Name: &lt;xsl:value-of select="firstName"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Two questions about this:</p>
  
  <ol>
  <li>I am assuming that Template #1 will execute first.  I don't know why I assume this -- is it just because it appears first in the document?</li>
  <li>Will Template #2 execute?  It matches a node in the source XML, but by the time the we get to this template (assuming it runs second), the "firstName" node will not be in the output tree.</li>
  </ol>
  
  <p>So, are "later" templates beholden to what has occurred in "earlier" templates, or do they operate on the source document, oblivious to what has been transformed "prior" to them?  (All those words are in quotes, because I find it hard to discuss time-based issues when I really have little idea how template order is determined in the first place...)</p>
  
  <p>In the above example, we have a template that matches on the root node ("/") that -- when it is done executing -- has essentially removed all nodes from the output.  This being the case, would this pre-empt all other templates from executing since there is nothing to match on after that first template is complete?</p>
  
  <p>To this point, I've been concerned with later templates not executing because the nodes they have operated on do not appear in the output, but what about the inverse?  Can an "earlier" template create a node that a "later" template can do something with?</p>
  
  <p>On the same XML as above, consider this XSL:</p>
  
  <pre><code>&lt;!-- Template #1 --&gt;
  &lt;xsl:template match="/"&gt;
    &lt;fullName&gt;
      &lt;xsl:value-of select="firstName"/&gt; &lt;xsl:value-of select="lastName"/&gt;
    &lt;/fullName&gt;
  &lt;/xsl:template&gt;
  
  &lt;!-- Template #2 --&gt;
  &lt;xsl:template match="//fullName"&gt;
    Full Name: &lt;xsl:value-of select="."/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Template #1 creates a new node called "fullName".  Template #2 matches on that same node.  Will Template #2 execute because the "fullName" node exists in the output by the time we get around to Template #2?</p>
  
  <p>I realize that I'm deeply ignorant about the "zen" of XSLT.  To date, my stylesheets have consisted of a template matching the root node, then are completely procedural from there.  I'm tired of doing this.  I would rather actually understand XSLT correctly, hence my question.</p>