181

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>
John Smith
  • 7,243
  • 6
  • 49
  • 61
Nik
  • 9,063
  • 7
  • 66
  • 81

1 Answers1

382

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

(//element[@name='D'])[last()]
John Smith
  • 7,243
  • 6
  • 49
  • 61
Robert Christie
  • 20,177
  • 8
  • 42
  • 37
  • 9
    TIL: Apparently there is no [first()] but you can use [1] – Jessica Aug 10 '15 at 13:43
  • 14
    It's funny that I still get votes for this question after 7 years. It really shows how flawed and defective XSLT is. Avoid XSLT as a plague :D – Nik Jan 29 '16 at 23:02
  • 3
    @Nik . It rather shows how few people understand the strength of the language. – Siebe Jongebloed May 09 '21 at 08:14
  • @SiebeJongebloed I was about to say XSLT was already dead 15 years ago but occasionally got some hype when everyone was writing PHP code... Then I opened your profile and see you call yourself a PHP expert :D – Nik May 11 '21 at 12:36
  • @Nik, somehow a got a smile on my fase but I don't understand why. Do you mean PHP is dead also? Or do mean that PHP is flawed and defective as well? That said, being an expert in a software-language can be quite doubtfull. Allthough when it is dead for 15 years it must be possible. ;-) – Siebe Jongebloed May 11 '21 at 13:08
  • @SiebeJongebloed I mean I expected my message saying that XSLT was popular in PHP-community only to be quite subjective/playful. But then the person I was replying to turned out to be a member of the PHP community :) So my initial message got a real-world proof :) – Nik May 11 '21 at 13:49
  • 3
    @Nik: I'm a real-world-proof example. I like it. :). As I like XSLT, no better language if you have to handle XML. – Siebe Jongebloed May 11 '21 at 15:25