p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Sorting? (http://p2p.wrox.com/showthread.php?t=5430)

pbernardo October 27th, 2003 10:23 AM

Sorting?
 
I'm new to XSLT so please bear with me.

I have XML looking like this.

<Event>
    <AirDateTime>10/24/2003</AirDateTime>
    <ArriveTime>10:00 AM</ArriveTime>
    <BeginTime>10:30 AM</BeginTime>
        <Location>House</Location>
    <State>FL</State>
    <Street>123 Biscanye Blvd.</Street>
        <City>Miami</City>
    <TimeZone>EST</TimeZone>
</Event>
<Event>
    <AirDateTime>10/24/2003</AirDateTime>
    <ArriveTime>10:30 AM</ArriveTime>
    <BeginTime>11:00 AM</BeginTime>
        <Location>Apartment</Location>
    <State>FL</State>
    <Street>123 Biscanye Blvd.</Street>
        <City>Miami</City>
    <TimeZone>EST</TimeZone>
</Event>
<Event>
    <AirDateTime>10/25/2003</AirDateTime>
    <ArriveTime>10:30 AM</ArriveTime>
    <BeginTime>11:00 AM</BeginTime>
        <Location>House</Location>
    <State>FL</State>
    <Street>123 Biscanye Blvd.</Street>
        <City>Miami</City>
    <TimeZone>EST</TimeZone>
</Event>
What I would like to do is match all Events that have the same AirDateTime and display the date with the location. Then go through again and show each with more detail. Below I have included a sample of what I'm looking for.

10/24/2003 - House
10/24/2003 - Apartment

10/25/2003 - House
~~~~~~~~~~~~~~~~~~~~~~~~~
10/24/2003 - 10:00AM
House
123 Biscanye Blvd.
Miami, FL

10/24/2003 - 10:30AM
Apartment
123 Biscanye Blvd.
Miami, FL

10/25/2003 - 10:00AM
House
123 Biscanye Blvd.
Miami, FL


Any help is appreciated, Please keep in mind that the XML events will not be presorted, dates will be jumbled up.

Thank you
Pete


pgtips October 27th, 2003 11:06 AM

Looks like just a straightforward sorting problem. Take a look at this stylesheet which applies the same sort twice - once for the summary and once for the detail. (Note that I had to add an <Events> container node to the XML you posted to make it a valid XML document, and that is reflected in the path used in the xsl:for-each).

Also, it would be easier to see that the sort is working correctly if you didn't have the same address in each Event ;)

Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Events</title>
      </head>
      <body>

        <xsl:for-each select="Events/Event">
          <xsl:sort select="AirDateTime"/>
          <xsl:sort select="ArriveTime"/>
          <xsl:value-of select="AirDateTime"/> - <xsl:value-of select="Location"/>
          <br/>
        </xsl:for-each>
        <br/>
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        <br/><br/>

        <xsl:for-each select="Events/Event">
          <xsl:sort select="AirDateTime"/>
          <xsl:sort select="ArriveTime"/>
          <xsl:value-of select="AirDateTime"/> - <xsl:value-of select="ArriveTime"/>
          <br/>
          <xsl:value-of select="Location"/>
          <br/>
          <xsl:value-of select="Street"/>
          <br/>
          <xsl:value-of select="City"/>, <xsl:value-of select="State"/>
          <br/>
          <br/>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

hth
Phil

pbernardo October 27th, 2003 11:34 AM

Thank you so much....

Seeing it like you have laid it out makes a lot of sense. I tested it and it works exactly like I hoped!

Thank you
Peter



All times are GMT -4. The time now is 08:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.