In XSLT there are two ways to grab the current node value and I always struggle between the two, mixing them up and sometimes creating problems with my templating logic. This post is primarily meant as a note to myself so I can try and avoid the problems I did today.
current()
current() is used to pull the current node (outside of the scope of the xpath expression)
”.”
”.” is used to pull the current node (is affected by the current xpath expression)
XML Data
<data>
<record name="a">a</record>
<record name="b">b</record>
<record name="a">a</record>
</data>
XSL Template 1
<xsl:apply-templates select="//record[@name=.]"/>
Since in this instance both values would be the same it would print out the following:
Output
aba
Now to see where these values would be different you would use a template like the following:
XSL Template 2
<xsl:apply-templates select="//record[@name=current()]"/>
This template ofcourse would be dependent on the node that is outside of the xpath scope. So let’s say that your current node’s value would be s it would return nothing becuase none of the nodes in the data nodeset have a name attribute that equals s. Pay attention to scope of your desired nodes when using current() and .. It may help save a few hours and a headache or two. :)