Subject: Sorting?
Posted By: pbernardo Post Date: 10/27/2003 9:23:55 AM
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

Reply By: pgtips Reply Date: 10/27/2003 10:06:15 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


<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>
        <!-- summary section, no "tricks" just a straight for-each -->
        <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/>
        <!-- detail section - the same for-each as the summary, just displaying different data-->
        <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
Reply By: pbernardo Reply Date: 10/27/2003 10:34:01 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


Go to topic 5711

Return to index page 1018
Return to index page 1017
Return to index page 1016
Return to index page 1015
Return to index page 1014
Return to index page 1013
Return to index page 1012
Return to index page 1011
Return to index page 1010
Return to index page 1009