[xslt] XSLT getting last element

I am trying to find the last element in my xml, which looks like:

    <list>
        <element name="A" />
        <element name="B" >
            <element name="C" />
            <element name="D" >
                <element name="D" />
                <element name="E" />
                <element name="F" />
                <element name="G" />
            </element>
        <element name="H" />
        <element name="I" />
    </list>

I need to get some kind of reverse menu, where current element and parents are highlighted as "active" and sibling as "inactive". Instead in result I have a messy tree only when I suppose "D" element clicked.

Double D elements are my problem. When I use select="//element[@name='D'][last()]" or select="//element[@name='D' and last()]" (btw which one is correct?) first time first occurrence of D element is selected (debugger shows that). Here is xsl

<xsl:template match="list">
    <xsl:apply-templates select="//navelement[@name = 'D'][last()]" mode="active"/>
</xsl:template>

<xsl:template match="element">
    <ul class="menu">
    <xsl:apply-templates select="preceding-sibling::node()" mode="inactive"/>
        <li><a>....</a></li>
    <xsl:apply-templates select="following-sibling::node()" mode="inactive"/>
    </ul>   
    <xsl:apply-templates select="parent::element" mode="active"/>
</xsl:template>

<xsl:template match="element" mode="inactive">
        <li><a>....</a></li>
</xsl:template>

This question is related to xslt xpath menu

The answer is


You need to put the last() indexing on the nodelist result, rather than as part of the selection criteria. Try:

(//element[@name='D'])[last()]

Examples related to xslt

Generic XSLT Search and Replace template Concatenate multiple node values in xpath XSL if: test with multiple test conditions Error: The processing instruction target matching "[xX][mM][lL]" is not allowed Weird behavior of the != XPath operator Declaring a xsl variable and assigning value to it How to implement if-else statement in XSLT? How do I specify "not equals to" when comparing strings in an XSLT <xsl:if>? How to concat a string to xsl:value-of select="...? XPath with multiple conditions

Examples related to xpath

Xpath: select div that contains class AND whose specific child element contains text XPath: difference between dot and text() How to set "value" to input web element using selenium? How to click a href link using Selenium XPath: Get parent node from child node What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing? How to use XPath preceding-sibling correctly Selenium and xPath - locating a link by containing text How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug? Concatenate multiple node values in xpath What is the hamburger menu icon called and the three vertical dots icon called? Changing text color of menu item in navigation drawer How to get selected value of a dropdown menu in ReactJS Android custom dropdown/popup menu Creating a Menu in Python How to make a 3-level collapsing menu in Bootstrap? how to set active class to nav menu from twitter bootstrap How to make jQuery UI nav menu horizontal? Twitter Bootstrap add active class to li Adding a right click menu to an item