Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old November 3rd, 2008, 02:45 PM
Authorized User
 
Join Date: Oct 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default Sort by date...select....display first item prob

Thought I had it solved, but it isn't working correctly.

Background:

XSL 1.0...client-side transformation

Goal:

Sort data by date ascending. Select items from XML by program, type of event, and those dates greater than or equal to today's date. Display first item from that which has a value greater than or equal to today's date. There will be multiple dates after today's date and I only want the first instance.(There will also be multiple programs that may have events on the same date as other programs too.)

What I have so far:

[u]Sample XML</u>
Code:
<event>
   <type>meeting</type>
   <evname>Executive Committee Meeting</evname>
   <evdate>20081217</evdate>
   <evstart>6:30pm</evstart>
   <evend>9:30pm</evend>
   <location>Large Conference Room</location>
   <locmap></locmap>
   <program id="board">Board of Directors</program>
   <contact>Secretary</contact>
   <email>secretary#64;ourorg.org</email>
   <notes>Dinner is served at 6pm.</notes>
   <siteurl></siteurl>
   <postdate></postdate>        
</event>

[u]Sample XSL</u>
Code:
<xsl:template match="/">
   <xsl:for-each select="//calendar/event/program[@id=$project]">
     <xsl:sort select="../evdate" order="ascending" data-type="number"/>
     <xsl:if test="../type ='meeting'">
     <xsl:if test="../evdate &gt;= $today and position() = 1">
         <xsl:element name="h1">
         <xsl:attribute name="class">side</xsl:attribute>    
                    UPCOMING MEETING
         </xsl:element>
         <xsl:element name="ul">
         <xsl:attribute name="class">sbox</xsl:attribute>
         <xsl:element name="li">
             <xsl:element name="b">
             <xsl:attribute name="style">color: #a77909</xsl:attribute>
             <xsl:value-of select="../eventdate"/> :: <xsl:value-of select="../eventstart"/>-<xsl:value-of select="../eventend"/> :: 
             </xsl:element><xsl:value-of select="../eventname"/> at <xsl:value-of select="../location"/>#160;
        </xsl:element>
         </xsl:element>            
      </xsl:if>    
      </xsl:if>
   </xsl:for-each>
</xsl:template>
It outputs fine unless I have dates in the XML that are earlier than today's date as well as later ones. If that happens, I get nothing. Can anyone help me? Thanks.
 
Old November 3rd, 2008, 03:59 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Filter out the events you aren't interested in before doing the sort.

<xsl:for-each select="//calendar/event[evdate &gt;= $today][type ='meeting']/program[@id=$project]">
     <xsl:sort select="../evdate" order="ascending" data-type="number"/>
     <xsl:if test="position() = 1">

Your problem is that the position() numbers include all the events selected by the for-each, regardless of the xsl:if conditions.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 3rd, 2008, 04:31 PM
Authorized User
 
Join Date: Oct 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks soooo much. I had tried to do multiple filtering via the "for-each" statement a while back, but apparently was doing that incorrectly. Cool...this gives me a better idea of how things work and better tools to go forth. Thanks again!

Quote:
quote:Originally posted by mhkay
 Filter out the events you aren't interested in before doing the sort.

<xsl:for-each select="//calendar/event[evdate &gt;= $today][type ='meeting']/program[@id=$project]">
     <xsl:sort select="../evdate" order="ascending" data-type="number"/>
     <xsl:if test="position() = 1">

Your problem is that the position() numbers include all the events selected by the for-each, regardless of the xsl:if conditions.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I select an ITEM from a combobox? Ron Howerton VB.NET 2002/2003 Basics 18 October 2nd, 2015 09:18 PM
sort and display first item only athos XSLT 11 October 16th, 2008 01:10 PM
Tab item display prob / hide TABS query socoolbrewster CSS Cascading Style Sheets 4 June 13th, 2006 12:59 PM
dynamic select item? vickriz Javascript How-To 3 June 11th, 2003 09:47 PM
dynamic select item? vickriz Javascript 1 June 5th, 2003 03:21 PM





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