Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 20th, 2008, 11:53 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Position() Method Problem



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):
Code:
<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:
Code:
<?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="''" />
    <xsl:param name="current_year" select="''" />
    <xsl:template match="/">

        <xsl:variable name="current_month_id" select="events/month[@id = $current_month and @year = $current_year]/@id" />



        <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.

KWilliams
  #2 (permalink)  
Old May 20th, 2008, 12:13 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Why do you store the id in a variable? If you want to use a variable then I would simply store the month element e.g.
Code:
<xsl:variable name="current_month" select="events/month[@id = $current_month and @year = $current_year]"/>
<xsl:for-each select="$current-month | $current_month/following-sibling::month[position() &lt; 3]">
--
  Martin Honnen
  Microsoft MVP - XML
  #3 (permalink)  
Old May 20th, 2008, 01:41 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I tried your code, but received a "this expression must evaluate a node" error. But I received a response from someone else that worked great. Here it is in-case anyone's interested:
Code:
<xsl:for-each select="events/month[@id >= $current_month]">
  <xsl:if test="position() < 3">
    <xsl:value-of select="name" /><br />
  </xsl:if>
</xsl:for-each>
Again, thanks so much for your input!

KWilliams
  #4 (permalink)  
Old May 20th, 2008, 02:11 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

...also, in order to pull nodes for the next year, I added this to the <for-each> method:
<xsl:for-each select="events/month[(@id &gt;= $current_month or @year = $current_year + 1) and position() &lt;= 3]">

It worked like a charm. Thanks again.

KWilliams


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem while using position() in for-loop LeoMathew XSLT 4 March 25th, 2008 07:08 AM
Customised list - Image position problem terra CSS Cascading Style Sheets 6 September 1st, 2005 01:28 PM
Problem with a method? savoym C# 1 January 6th, 2005 09:34 AM
unspecified method problem? nerssi BOOK: Professional Jakarta Struts 0 September 12th, 2004 06:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.