Obtaining the Current Node for the Current XSLT Template

In a stylesheet, the current node is the node for which the XSLT processor instantiates a template. When the XPath processor evaluates an expression during stylesheet processing, the initial context node for the expression is set to the current node for the stylesheet instruction that contains the expression. Because the context node can change during evaluation of subexpressions, it is useful to be able to retrieve, from within a subexpression, the original context node for which the expression is being evaluated. You can use the current() function for this purpose. The format is

node-set current() 
               

            

The current() function returns a node set that contains only the current node for the current template. The current() function is specified in the W3C XSLT Recommendation.

For example, the following stylesheet causes the XSLT processor to pass the bookstore node to the outer xsl:for-each instruction:

<xsl:stylesheet 
               

              
xmlns:xsl="http://www.w3.org/XSL/Transform" version="1.0" >
                   
<xsl:template match="/"> 
                   
<xsl:for-each select="bookstore"> 
                   
<xsl:for-each select=
                   
"book[@style=current()/@specialty]"> 
                   
... 
                   
</xsl:for-each> 
                   
</xsl:for-each> 
                   
</xsl:template> 
                   

                
</xsl:stylesheet>

The bookstore node is the current node within the outer xsl:for-each instruction. Within the inner xsl:for-each instruction, a book node is the current node.

The current() function in the inner expression returns the bookstore element because the bookstore element is the current node for the inner xsl:for-each instruction. The result of the query contains book elements if the value of their style attribute is the same as the value of the specialty attribute of the bookstore element ( novel).

Suppose the select attribute in the inner xsl:for-each instruction specified the dot ( .) instead of the current() function:

<xsl:for-each select="book[@style=./@specialty]">
               

            

In a query, the dot specifies the context node. This query would return a book if the value of its style attribute was the same as the value of its specialty attribute.

You can nest xsl:for-each instructions more than one level deep. In any given nested xsl:for-each instruction, the current() function returns the current node for the closest enclosing xsl:for-each instruction.

EAI - Enterprise Application Integration

Using EAI tools included with Stylus Studio it's easy to unlock silos of enterprise data and seamlessly integrate it into your IT applications.

OASIS Catalogs for Fun and Profit

This tutorial about, Understanding Catalogs, A Use-Case for Catalogs, Registering Catalogs in a Project, Using the DOCTYPE Syntax Sense:X Aid, Navigating Catalogs, Building Composite Catalogs, Converting TR9401 Catalogs to OASIS XML Catalogs and more

Web Service Testing Tools

The Stylus Studio Web Service Tester allows you to easily locate, build references to, and test Web services for XML applications based on WSDL, SOAP, and UDDI standard Web service technologies.

Open XSLT Architecture

Learn how Stylus Studio's Open XSLT Architecture enables you to use any of the popular industry-standard XSLT processors (like Saxon, Xalan-J, MSXML, Microsoft .NET, and others) for all your XSLT development tasks.

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member