DevonDahon
DevonDahon

Reputation: 8350

XSLT: position() and last() not returning expected result

I would like to get the position of each svg:use[@href='#electron'] when it's matched. I'm expecting to have the position among other elements of the same match, but instead I'm getting numbers I don't understand. How should I use position() and last() to get expected figures ?

<!-- Add node "animateTransform" -->
<xsl:template match="svg:use[@href='#electron']">
    <!-- Copy the element -->
    <xsl:copy>
        <!-- And everything inside it -->
        <xsl:apply-templates select="@* | node()"/>
        <xsl:if test="position() = last()">
            <xsl:attribute name="class">val</xsl:attribute>
        </xsl:if>
        <animateTransform attributeName="transform" type="rotate" from="360 0 0" to="0 0 0" dur="20s" repeatCount="indefinite" />
        <xsl:value-of select="position()"/>/<xsl:value-of select="last()"/>
    </xsl:copy>
</xsl:template>

Upvotes: 1

Views: 315

Answers (1)

Lucero
Lucero

Reputation: 60190

The position() and last() functions return the position relative to the current node-set.

The node-set is, however, not determined by the match attribute; this is just a filter which determines which template shall be applied.

The problem lies in the part which does select the nodes on which to apply templates, e.g. the apply-templates which ends up having the svg:use matches.

Try to use this at that location:

<xsl:apply-templates select="svg:use[@href='#electron']"/>

You'll then get the expected position.

Alternatively you can change your check for the last item, for instance like this:

<xsl:if test="not(following-sibling::svg:use[@href='#electron'])">
    <xsl:attribute name="class">val</xsl:attribute>
</xsl:if>

Upvotes: 1

Related Questions