<html>
<head>
<title>current() 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">current() 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 a node-set that has the current node as its only member. </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 node-set that has the current node as its only member. Most of the time, the current node is no different than the context node. These two XSLT elements have the same meaning:</p>
<span class="PROGRAMLISTING"><pre>
&lt;xsl:value-of select="current()"/&gt;
&lt;xsl:value-of select="."/&gt;</pre></span>
<p>Within a predicate expression, however, the current node and the context node are usually different. The example section that follows illustrates when you need to use the <span class="LITERAL">current()</span> function.</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>XSLT section 12.4, Miscellaneous Additional 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">
<!--<?troff .hw transform?>-->
<p>We'll use the <span class="LITERAL">current()</span> function along with a lookup table. Here's the document we'll transform:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;report&gt;
  &lt;title&gt;Miles Flown in 2001&lt;/title&gt;
  &lt;month sequence="01"&gt;
    &lt;miles-flown&gt;12379&lt;/miles-flown&gt;
    &lt;miles-earned&gt;35215&lt;/miles-earned&gt;
  &lt;/month&gt;
  &lt;month sequence="02"&gt;
    &lt;miles-flown&gt;32857&lt;/miles-flown&gt;
    &lt;miles-earned&gt;92731&lt;/miles-earned&gt;
  &lt;/month&gt;
  &lt;month sequence="03"&gt;
    &lt;miles-flown&gt;19920&lt;/miles-flown&gt;
    &lt;miles-earned&gt;76725&lt;/miles-earned&gt;
  &lt;/month&gt;
  &lt;month sequence="04"&gt;
    &lt;miles-flown&gt;18903&lt;/miles-flown&gt;
    &lt;miles-earned&gt;31781&lt;/miles-earned&gt;
  &lt;/month&gt;
&lt;/report&gt;</pre></span>
<p>Here's our stylesheet. We'll do the same transform twice, one time with the <span class="LITERAL">current()</span> function and one time without it:</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"
  xmlns:months="Lookup table for month names"&gt;

  &lt;months:name sequence="12"&gt;December&lt;/months:name&gt;
  &lt;months:name sequence="01"&gt;January&lt;/months:name&gt;
  &lt;months:name sequence="02"&gt;February&lt;/months:name&gt;
  &lt;months:name sequence="03"&gt;March&lt;/months:name&gt;
  &lt;months:name sequence="04"&gt;April&lt;/months:name&gt;
  &lt;months:name sequence="05"&gt;May&lt;/months:name&gt;
  &lt;months:name sequence="06"&gt;June&lt;/months:name&gt;
  &lt;months:name sequence="07"&gt;July&lt;/months:name&gt;
  &lt;months:name sequence="08"&gt;August&lt;/months:name&gt;
  &lt;months:name sequence="09"&gt;September&lt;/months:name&gt;
  &lt;months:name sequence="10"&gt;October&lt;/months:name&gt;
  &lt;months:name sequence="11"&gt;November&lt;/months:name&gt;

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

<!--<?troff .Nd 10?>-->
  &lt;xsl:variable name="newline"&gt;
&lt;xsl:text&gt;
&lt;/xsl:text&gt;
  &lt;/xsl:variable&gt;

  &lt;xsl:template match="/"&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;A test of the current() function:&lt;/xsl:text&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:for-each select="/report/month"&gt;
      &lt;xsl:text&gt;   &lt;/xsl:text&gt;
      &lt;xsl:value-of 
        select="document('')/*/months:name[@sequence=current()/@sequence]"/&gt;
      &lt;xsl:text&gt; - &lt;/xsl:text&gt;
      &lt;xsl:value-of select="format-number(miles-flown, '##,###')"/&gt;
      &lt;xsl:text&gt; miles flown, &lt;/xsl:text&gt;
      &lt;xsl:value-of select="format-number(miles-earned, '##,###')"/&gt;
      &lt;xsl:text&gt; miles earned.&lt;/xsl:text&gt;
      &lt;xsl:value-of select="$newline"/&gt;
      &lt;xsl:text&gt;      (Averaged &lt;/xsl:text&gt;
      &lt;xsl:value-of 
        select="format-number(miles-earned div miles-flown, '##.#')"/&gt;
      &lt;xsl:text&gt; miles earned for each mile flown.)&lt;/xsl:text&gt;
      &lt;xsl:value-of select="$newline"/&gt;
      &lt;xsl:value-of select="$newline"/&gt;
    &lt;/xsl:for-each&gt;
    &lt;xsl:value-of select="$newline"/&gt;

    &lt;xsl:text&gt;Let's try it again, without using current() this time:&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;

    &lt;xsl:for-each select="/report/month"&gt;
      &lt;xsl:text&gt;   &lt;/xsl:text&gt;
      &lt;xsl:value-of 
        select="document('')/*/months:name[@sequence=./@sequence]"/&gt;
      &lt;xsl:text&gt; - &lt;/xsl:text&gt;
      &lt;xsl:value-of select="format-number(miles-flown, '##,###')"/&gt;
      &lt;xsl:text&gt; miles flown, &lt;/xsl:text&gt;
      &lt;xsl:value-of select="format-number(miles-earned, '##,###')"/&gt;
      &lt;xsl:text&gt; miles earned.&lt;/xsl:text&gt;
      &lt;xsl:value-of select="$newline"/&gt;
      &lt;xsl:text&gt;      (Averaged &lt;/xsl:text&gt;
      &lt;xsl:value-of 
        select="format-number(miles-earned div miles-flown, '##.#')"/&gt;
      &lt;xsl:text&gt; miles earned for each mile flown.)&lt;/xsl:text&gt;
<!--<?troff .Nd 10?>-->
      &lt;xsl:value-of select="$newline"/&gt;
      &lt;xsl:value-of select="$newline"/&gt;
    &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>Here are the results:</p>
<span class="PROGRAMLISTING"><pre>

A test of the current() function:

   January - 12,379 miles flown, 35,215 miles earned.
      (Averaged 2.8 miles earned for each mile flown.)

   February - 32,857 miles flown, 92,731 miles earned.
      (Averaged 2.8 miles earned for each mile flown.)

   March - 19,920 miles flown, 76,725 miles earned.
      (Averaged 3.9 miles earned for each mile flown.)

   April - 18,903 miles flown, 31,781 miles earned.
      (Averaged 1.7 miles earned for each mile flown.)


Let's try it again, without using current() this time:

   December - 12,379 miles flown, 35,215 miles earned.
      (Averaged 2.8 miles earned for each mile flown.)

   December - 32,857 miles flown, 92,731 miles earned.
      (Averaged 2.8 miles earned for each mile flown.)

   December - 19,920 miles flown, 76,725 miles earned.
      (Averaged 3.9 miles earned for each mile flown.)

   December - 18,903 miles flown, 31,781 miles earned.
      (Averaged 1.7 miles earned for each mile flown.)
</pre></span>
<p>The second time around, our stylesheet matched each <span class="LITERAL">&lt;month&gt;</span> element to the month <span class="LITERAL">December</span>. The difference is that the dot syntax (<span class="LITERAL">.</span>) represents the current node at that point in the XPath expression, while the <span class="LITERAL">current()</span> function represents the current node before the XSLT processor began evaluating the XPath expression. </p>
<p>In other words, the XSLT processor starts with the first <span class="LITERAL">&lt;months:name&gt;</span> element, attempting to find the element whose <span class="LITERAL">sequence</span> attribute matches another <span class="LITERAL">sequence</span> attribute we're examining. If we specify the other <span class="LITERAL">sequence</span> attribute with <span class="LITERAL">./@sequence</span>, it indicates the <span class="LITERAL">sequence</span> attribute of the current node at this point in the expression, which is the first <span class="LITERAL">&lt;months:name&gt;</span> element. That always returns the value of the first <span class="LITERAL">&lt;months:name&gt;</span> element. Using the <span class="LITERAL">current()</span> function, on the other hand, returns the node that was current when we started to evaluate this expression; <span class="LITERAL">current()</span> gives us the behavior we want. </p>
</td>
</tr>
</table>
</div>
</body>
</html>