XSLT - In predicate filters, why must one sometime use the XSLT current() function rather than the XPath context node dot operator?

StackOverflow https://stackoverflow.com/questions/22698203

  •  22-06-2023
  •  | 
  •  

Question

In a predicate filter, I am trying to figure out how best to explain why and when one must use the XSLT current() function rather than the XPath context node . expression.

The example given at the w3schools XSLT current() Function page helps only a bit as it mostly seems to illustrate the difference without really explaining it. Both Michael Kay's discussion of the current() function in Chapter 13 of "XSLT 2.0 and XPath 2.0, 4th Edition" and Paul Jungwirth's reply to the question Current node vs. Context node in XSLT/XPath? help me understand the difference personally; but each leaves me struggling with how to explain the difference to others rather than merely illustrating the difference using diagrams and code.

If someone will share how they have gone further in explaining this, I would be most grateful.

Was it helpful?

Solution

In XPath, a Location Path expression is relative to the context node, e.g.:

head/title

or

@class

Predicates are used to filter sequences (node-sets in v1). A Predicate Expression is evaluated against each item (or node) in the sequence, using the item as context. e.g.

div[@class] (: @class is relative to div :)

The current() function is available in XSLT (not XPath) to refer to the node currently being processed by the containing xsl:template or xsl:for-each instruction.

http://www.w3.org/TR/xslt#function-current

OTHER TIPS

whit this code I understood source:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<parent>
    <variable>A</variable>
    <child>
        <variable>B</variable>
    </child>
</parent>

xslt:

   <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/parent">

            <xsl:value-of select="string('using current')"/>
            <xsl:choose>
                <xsl:when test="child[current()/variable = 'A']">
                    <xsl:text>&#10;child[current()/variable = 'A']// -> is true</xsl:text> 
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>&#10;child[current()/variable = 'A']// -> is false</xsl:text> 
                </xsl:otherwise>
            </xsl:choose>

            <xsl:value-of select="string('&#10;&#10;using dot')"/>
            <xsl:choose>
                <xsl:when test="child[./variable = 'A']">
                    <xsl:text>&#10;child[./variable = 'A']// -> is true</xsl:text> 
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>&#10;child[./variable = 'A']// -> is false</xsl:text> 
                </xsl:otherwise>
            </xsl:choose>

            <xsl:value-of select="string('&#10;&#10;using dot')"/>
            <xsl:choose>
                <xsl:when test="child[./variable = 'B']">
                    <xsl:text>&#10;child[./variable = 'B']// -> is true</xsl:text> 
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>&#10;child[./variable = 'B']// -> is false</xsl:text> 
                </xsl:otherwise>
            </xsl:choose>


    </xsl:template>
</xsl:stylesheet>

output:

using current
child[current()/variable = 'A']// -> is true

using dot
child[./variable = 'A']// -> is false

using dot
child[./variable = 'B']// -> is true

so, you can check that with "current" function you are looking for inside the current node, not inside the child, such as with the dot.

The context node changes with any step and any predicate you add to a path expression while the current node is only changed by XSLT instructions like apply-templates and for-each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top