Skip to content

XSLT current() Function

How the XSLT current() function differs from the shorthand '.' in XPath expressions and when to use each for precise node selection in transformations.

XSLT current() Function

The current function returns a node-set that contains only the current node.

select="current()" vs select="."

It may seem that <xsl:value-of select="current()"/> is equal to <xsl:value-of select="."/> but it is not!

They work differently and here's why:

Consider the following XPath expression: bookstore/book. This expression selects the <bookstore> child nodes of the current node, and then it selects the <book> child nodes of the <bookstore> nodes. This means that on each step of evaluation. the . has a different meaning!

For example:

  • The following line <xsl:apply-templates select="//book[@title=current()/@ref]"/> will process all book elements that have a title attribute with value equal to the value of the current node's ref attribute.

  • This is different from <xsl:apply-templates select="//book[@title=./@ref]"/> that will process all book elements that have a title attribute and a ref attribute with the same value.

Syntax

node-set current()

Example

Consider the following example about a bookstore:

books.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Tips</title>
		<author>Tom Nolan</author>
		<year>2022</year>
		<price>39.99</price>
	</book>
	<book category="web">
		<title lang="en">Learn XML</title>
		<author>Tutorial Reference</author>
		<year>2022</year>
		<price>9.99</price>
	</book>
</bookstore>
books.xslxml
<?xml version="1.0"  encoding="UTF-8"?>  
<xsl:stylesheet version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  
<xsl:template match="/">  
	<html>  
		<body>  
			<xsl:for-each select="bookstore/book/author">  
				Current node: <xsl:value-of select="current()"/>  
				<br />  
			</xsl:for-each>  
		</body>  
	</html>  
</xsl:template>  
  
</xsl:stylesheet>
Navigation