current() Function |
Returns a node-set that has the current node as its only member.
Inputs
None.
Output
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:
    <xsl:value-of select="current()"/>
    <xsl:value-of select="."/>
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 current() function.
Defined in
XSLT section 12.4, Miscellaneous Additional Functions.
Example
We'll use the current() function along with a lookup table. Here's the document we'll transform:
    <?xml version="1.0"?>
    <report>
      <title>Miles Flown in 2001</title>
      <month sequence="01">
        <miles-flown>12379</miles-flown>
        <miles-earned>35215</miles-earned>
      </month>
      <month sequence="02">
        <miles-flown>32857</miles-flown>
        <miles-earned>92731</miles-earned>
      </month>
      <month sequence="03">
        <miles-flown>19920</miles-flown>
        <miles-earned>76725</miles-earned>
      </month>
      <month sequence="04">
        <miles-flown>18903</miles-flown>
        <miles-earned>31781</miles-earned>
      </month>
    </report>
Here's our stylesheet. We'll do the same transform twice, one time with the current() function and one time without it:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:months="Lookup table for month names">

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

      <xsl:output method="text"/>

      <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
      </xsl:variable>

      <xsl:template match="/">
        <xsl:value-of select="$newline"/>
        <xsl:text>A test of the current() function:</xsl:text>

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

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

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

          <xsl:value-of select="$newline"/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>
      </xsl:template>

    </xsl:stylesheet>
Here are the results:
    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.)
The second time around, our stylesheet matched each <month> element to the month December. The difference is that the dot syntax (.) represents the current node at that point in the XPath expression, while the current() function represents the current node before the XSLT processor began evaluating the XPath expression. In other words, the XSLT processor starts with the first <months:name> element, attempting to find the element whose sequence attribute matches another sequence attribute we're examining. If we specify the other sequence attribute with ./@sequence, it indicates the sequence attribute of the current node at this point in the expression, which is the first <months:name> element. That always returns the value of the first <months:name> element. Using the current() function, on the other hand, returns the node that was current when we started to evaluate this expression; current() gives us the behavior we want.