Click to See Complete Forum and Search --> : XSLT: Position() Method Problem


kwilliams
05-20-2008, 12:55 PM
I would like to pull 3 event nodes (May, June, and July) that are located within a bunch of other nodes from the following XML doc, starting with node #2 (name=May 2008):
<events>
<month id="04" year="2008">
<name>April 2008</name>
</month>
<month id="05" year="2008">
<name>May 2008</name>
</month>
<month id="06" year="2008">
<name>June 2008</name>
</month>
<month id="07" year="2008">
<name>July 2008</name>
</month>
<month id="08" year="2008">
<name>August 2008</name>
</month>
</events>

I'm pulling in a parameter value from an ASP.NET doc that contains the current month's month-value (05), so now I need to only display the 3 nodes after that starting node. This the the XSLT doc that I've created:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">
<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>
<xsl:param name="current_month" select="''" /><!-- Populated from ASP.NET doc / contains "05" value -->
<xsl:param name="current_year" select="''" /><!-- Populated from ASP.NET doc / contains "2008" value -->
<xsl:template match="/">
<!-- Pulls current month using $current_month parameter -->
<xsl:variable name="current_month_id" select="events/month[@id = $current_month and @year = $current_year]/@id" />

<!-- ***Q: How can I apply the "$current_month_id" variable to the <for-each" method below??? -->

<xsl:for-each select="events/month[position() &lt;= 3]">
<xsl:value-of select="name" /><br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

But I'm stuck on how to only pull the 3 nodes after that original node. I know that using the position() method is probably the way to go, but I can't figure out how to do it. If anyone could point me in the right direction, it would be greatly appreciated.

rpgfan3233
05-20-2008, 02:12 PM
<xsl:for-each select="events/month[@id &gt;= $current_month]">
<xsl:if test="position() &lt; 3">
<xsl:value-of select="name" /><br />
</xsl:if>
</xsl:for-each>

Is that close enough to what you're looking for?

kwilliams
05-20-2008, 02:39 PM
Absolutely! It worked great. I had to change the < to &lt; and > to &gt;, but it truly is what I needed. Thanks so much rpgfan3233!