Problems using conditional and selecting last entry

Here should go questions about transforming XML with XSLT and FOP.
slaterino
Posts: 11
Joined: Sat Jan 09, 2010 8:13 pm

Problems using conditional and selecting last entry

Post by slaterino »

Hi,
I have a navigation bar which is made up from the headings of chapters, but only ones that adhere to a conditional statement. In this navbar I am trying to edit it so that all links have a small boundary between each other. At the moment I am just using the '|' symbol, but I am having problems. I am using the position() function but it is not picking out the last of the chapter headings. I think the problem may be that it is looking for the last position across all of my chapter headings rather than just the ones in the conditional statement, and so doesn't actually recognise any headings as actually being the last one. Can someone have a look at my code and let me know if this is the problem or something else, and how I could actually sort it out?!

Code: Select all

                <xsl:for-each select="//chapter">
<xsl:if test="../title='About'">
<xsl:choose>
<xsl:when test="position()=last()">
<li>
<a href="{titleabbrev}.php"><xsl:value-of select="title" /></a>
</li>
</xsl:when>
<xsl:otherwise>
<li>
<a href="{titleabbrev}.php"><xsl:value-of select="title" /></a> |
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Problems using conditional and selecting last entry

Post by george »

Move the test as a predicate in the for select expression

Code: Select all


<xsl:for-each select="//chapter[../title='About']">
<xsl:choose>
<xsl:when test="position()=last()">
<li>
<a href="{titleabbrev}.php">
<xsl:value-of select="title"/>
</a>
</li>
</xsl:when>
<xsl:otherwise>
<li>
<a href="{titleabbrev}.php"><xsl:value-of select="title"/></a> | </li>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
Regards,
George
George Cristian Bina
Post Reply