title: Selecting the last element in the whole document with XSLT
body: <p>I want to select the last node matching a particular pattern anywhere in the document.</p>
  
  <p>I was trying something like </p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:template match="/"&gt;
      &lt;xsl:copy-of select="//node_name[last()]"/&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>However, when running this with Saxon 9.4 on the following document :- </p>
  
  <pre><code>&lt;a&gt;
    &lt;node_name attr="1"/&gt;
    &lt;b&gt;
      &lt;c&gt;
      &lt;/c&gt;
      &lt;node_name attr="2"/&gt;
    &lt;/b&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>I get this output where the copy statement lies :- </p>
  
  <pre><code>&lt;node_name attr="1"/&gt;&lt;node_name attr="2"/&gt;
  </code></pre>
  
  <p>Whereas i actually want the output :- </p>
  
  <pre><code>&lt;node_name attr="2"/&gt;
  </code></pre>
  
  <p>What am I missing out on here?</p>
  
  <p>Also, the nature of my document is such that I do not know in advance what the exact path to this node will be (since it's composed of a bunch of recursive elements).</p>