The DevCenter has moved!

The Developer Center has a new home, developer.ektron.com. The site you are currently viewing is available as a read-only archive.
Things to note:
  1. Your username & password is the same on the new site, developer.ektron.com
  2. Going forward, please use the new URL, developer.ektron.com
  3. The old discussion forums and old KB articles are still available as an archive (read-only)
RSS Feed Print Category View
Help on XSLT loop - Find last item in loop apply different style
mcjimbo
Posted: Saturday, March 15, 2008 3:26 PM
Joined: 3/15/2008
Posts: 18


Hi

I'm trying to create a basic utility menu using a Collection control + XSLT. Here is my issue I want to loop through the items in the collection and add a pipe | character to every menu item apart from the last. So the output of the menu would be something like:
 
FirstLink | SecondLink | FinalLink

I know that all I really need to do is find a way to find when i'm at the final item in the loop and not add pipe | character. However i'm having issues find a way to do it in XSLT. Any ideas below is the my XSLT for your reference, right now its kind of working in reverse....

<?xml version="1.0" encoding="iso-8859-1"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<ul class="tool_links">
<
xsl:for-each select="Collection/Content">
<
xsl:choose>
<!--
Every item: Show pipe | character -->
<
xsl:when test="position() = 1">
<
li>
<
a>
<
xsl:attribute name="href">
<
xsl:value-of select="QuickLink" />
</
xsl:attribute>
<
xsl:value-of select="Title" />
</
a>
| &#160;
</li>
</
xsl:when>
<!--
Last item: Do not show pipe | character -->
<xsl:otherwise>
<
li>
<
a>
<
xsl:attribute name="href">
<
xsl:value-of select="QuickLink" />
</
xsl:attribute>
<
xsl:value-of select="Title" />
</
a>
&#160;
</li>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:for-each>
</
ul>
</
xsl:template>
</
xsl:stylesheet>


mcjimbo
Posted: Saturday, March 15, 2008 3:47 PM
Joined: 3/15/2008
Posts: 18


I've got it! Code is below in case anyone else needs it for reference.

 

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<ul class="tool_links">

<xsl:for-each select="Collection/Content">

<xsl:choose>

<!-- Every item: Show pipe | character -->

<xsl:when test="not(position() = 1)">

<li>

|

<a>

<xsl:attribute name="href">

<xsl:value-of select="QuickLink" />

</xsl:attribute>

<xsl:value-of select="Title" />

</a>

</li>

</xsl:when>

<!-- Last item: Do not show pipe | character -->

<xsl:otherwise>

<li>

<a>

<xsl:attribute name="href">

<xsl:value-of select="QuickLink" />

</xsl:attribute>

<xsl:value-of select="Title" />

</a>

</li>

</xsl:otherwise>

</xsl:choose>

</xsl:for-each>

</ul>

</xsl:template>

</xsl:stylesheet>


Charles Collins
Posted: Sunday, March 16, 2008 7:25 PM
Joined: 4/10/2007
Posts: 61


I haven't tried this, but this line seems wrong:

<xsl:when test="not(position() = 1)">

Wouldn't this show the '|' for position 1 and not for anything else? How about this:

<xsl:when test="not(position() = last())">

 

 


Charles Collins
Web Developer
Versant [http://www.versantsolutions.com]

mcjimbo
Posted: Sunday, March 16, 2008 10:02 PM
Joined: 3/15/2008
Posts: 18


The code worked but further investigation yielded this find:

<xsl:when test="position()!= last()">

This XSLT code let's you identify the last record.


oompal
Posted: Thursday, June 24, 2010 12:05 PM
Joined: 6/24/2010
Posts: 1


this was so helpful. I have a question though - what if I want to treat BOTH first AND last differently? Do I need to when structures or can I do something like when something OR something else? Can anyone share the syntax for this?
Bill C.
Posted: Thursday, June 24, 2010 1:11 PM
Joined: 6/6/2006
Posts: 692


If you're looking to handle first/last differently but still build off of the approach listed below, then add a test for first, test for last, then a default case that handles the rest:

<xsl:choose>
 <xsl:when test="position() = first()">
  ...
 </xsl:when>
 <xsl:when test="position() = last()">
  ...
 </xsl:when>
 <xsl:otherwise>
  ...
 </xsl:otherwise>
</xsl:choose>


 

Jump to different Forum...