XSLT. For some reason, do predicate filters sometimes use the XSLT function current () rather than the XPath node context operator?

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

The example on the w3schools XSLT current()Function page helps only a little, as it basically illustrates the difference without explaining the reasons for This. And Michael Kay discusses the feature current()in Chapter 13, “XSLT 2.0 and XPath 2.0, 4th Edition” and Paul Jungwirth to the question Current node vs. Context node in XSLT / XPath? help me figure out the difference personally; but each of them challenges me how to explain the difference to others, and not just illustrate the difference using diagrams and code.

If someone tells how they went further, explaining this, I would be very grateful.

+4
source share
3 answers

XPath node, :

head/title

@class

(node -sets v1). ( node) , . .

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

current() XSLT ( XPath), node, xsl:template xsl:for-each.

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

+3

:

<?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>

:

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

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

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

"" , node, , , .

0

node , , node XSLT, apply-templates for-each.

0

Source: https://habr.com/ru/post/1533795/


All Articles