<html>
<head>
<title>last() Function</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Description">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">last() Function</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Returns the position of the last node in the current context. This function is useful for defining templates for the last occurrence of a given element or for testing if a given node is the last in the node-set to which it belongs.</td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Inputs</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>None.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Output</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>A number equal to the number of nodes in the current context. For example, if the current context contains 12 <span class="LITERAL">&lt;li&gt;</span> nodes, <span class="LITERAL">last()</span> returns 12.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Defined in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XPath section 4.1, Node Set Functions. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Example</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>We'll use the <span class="LITERAL">last()</span> function to handle the last item in a list in a special way. Here's the XML document we'll use:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;list&gt;
  &lt;title&gt;A few of my favorite albums&lt;/title&gt;
  &lt;listitem&gt;A Love Supreme&lt;/listitem&gt;
  &lt;listitem&gt;Beat Crazy&lt;/listitem&gt;
  &lt;listitem&gt;Here Come the Warm Jets&lt;/listitem&gt;
  &lt;listitem&gt;Kind of Blue&lt;/listitem&gt;
  &lt;listitem&gt;London Calling&lt;/listitem&gt;
  &lt;listitem&gt;Remain in Light&lt;/listitem&gt;
  &lt;listitem&gt;The Joshua Tree&lt;/listitem&gt;
  &lt;listitem&gt;The Indestructible Beat of Soweto&lt;/listitem&gt;
&lt;/list&gt;</pre></span>
<p>Here is the stylesheet that handles the last <span class="LITERAL">&lt;listitem&gt;</span> in the list differently:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:output method="html"/&gt;

  &lt;xsl:template match="/"&gt;
    &lt;html&gt;
      &lt;head&gt;
        &lt;title&gt;
          &lt;xsl:value-of select="/list/title"/&gt;
        &lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;h1&gt;
          &lt;xsl:value-of select="/list/title"/&gt;
        &lt;/h1&gt;
        &lt;ul&gt;
          &lt;xsl:for-each select="/list/listitem"&gt;
            &lt;xsl:choose&gt;
              &lt;xsl:when test="position()=last()"&gt;
                &lt;li&gt;&lt;b&gt;Last, but not least: &lt;/b&gt;&lt;xsl:value-of select="."/&gt;&lt;/li&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                &lt;li&gt;&lt;xsl:value-of select="."/&gt;&lt;/li&gt;
              &lt;/xsl:otherwise&gt;
            &lt;/xsl:choose&gt;
          &lt;/xsl:for-each&gt;
        &lt;/ul&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>When we transform the XML document with this stylesheet, here are the results:</p>
<span class="PROGRAMLISTING"><pre>
&lt;html&gt;
&lt;head&gt;
&lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;A few of my favorite albums&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;A few of my favorite albums&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;A Love Supreme&lt;/li&gt;
&lt;li&gt;Beat Crazy&lt;/li&gt;
&lt;li&gt;Here Come the Warm Jets&lt;/li&gt;
&lt;li&gt;Kind of Blue&lt;/li&gt;
&lt;li&gt;London Calling&lt;/li&gt;
&lt;li&gt;Remain in Light&lt;/li&gt;
&lt;li&gt;The Joshua Tree&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;Last, but not least: &lt;/b&gt;The Indestructible Beat of Soweto&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></span>
<p>When rendered, the HTML file looks like <link linkend="xslt-appc-c7">Figure C-7</link>.</p>
<figure id="xslt-appc-c7" label="C-7">
        <p class="TITLE">Generated HTML document</p>
        <graphic depth="252" width="396" fileref="figs/xslt.ac07.gif"/>
      </figure>
</td>
</tr>
</table>
</div>
</body>
</html>