Insert snippet before closing </li> in recursive menu? Options
tkahn
Posted: Monday, August 11, 2008 5:59:20 PM

Rank: Fanatic

Joined: 11/24/2006
Posts: 322
Location: Stockholm, Sweden
I have an XSLT-script which generates a sub menu recursively. The structure of the HTML generated is basically an unordered list:

Code:

<ul>
<li>Item 1</li>
<li>Item 2
     <ul>
     <li>Item 2.1</li>
     <li>Item 2.2</li>
     </ul>
</li>
<li>Item 3</li>


I'm doing some graphic stuff which involves rounded corners and therefore I need to insert code before the closing </li> tag in the nested unordered list. Like this:

Code:

<ul>
<li>Item 1</li>
<li>Item 2
     <ul>
     <li>Item 2.1</li>
     <li>Item 2.2
     <I WANT TO INSERT CODE HERE>
     </li>
     </ul>
</li>
<li>Item 3</li>


This should only be done at a certain level in the structure Does anyone have an example of how to do this?

Is it for example possible to check if the current node is the last node at the current level? If I could do that check, perhaps I could inject the code snippet before the closing tag?

Thanks in advance!

Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
drobar
Posted: Monday, August 11, 2008 6:17:07 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,695
Location: KY, USA
You can count() the number of nodes and check your current position() against the count to see if you're at the last one, and then add your extra code. Or, you compare the last() function in xslt.

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
tkahn
Posted: Tuesday, August 12, 2008 8:47:55 AM

Rank: Fanatic

Joined: 11/24/2006
Posts: 322
Location: Stockholm, Sweden
Thanks Doug!

I ended up with this simple test:

Code:

<xsl:if test="count(../node) = position()">

<!-- INJECT CODE HERE -->

</xsl:if>


I checks how many nodes there are at the current level in the tree against the position of the current node in the loop. If they are the same, I know that I'm on the last node and I can inject the code before the closing </li> tag.



Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
Petr Snobelt
Posted: Tuesday, August 12, 2008 9:29:17 AM
Rank: Aficionado

Joined: 10/2/2007
Posts: 165
Location: Czech Republic
Hi,
you can use
<xsl:if test="position() = last()">

I think it may be little faster, count must counting in every loop.

Petr
tkahn
Posted: Wednesday, August 13, 2008 11:08:44 AM

Rank: Fanatic

Joined: 11/24/2006
Posts: 322
Location: Stockholm, Sweden
Thanks Petr!
Your alternative works just like my first attempt.
It's always good to learn how to optimize for better performance!

Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.