Skip to main content

XSLT current() Function

XSLT current() Function

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

note

Usually the current node and the context node are the same.

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

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

The 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.xml
<?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.xsl
<?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>