^.~ HELP!!!!! XML translation into HTML tables using XSL

Discussion in 'XML' started by Thuan Seah, Sep 10, 2003.

  1. Thuan Seah

    Thuan Seah Guest

    Hi ^.^
    Having problems formatting my xml document into a timetable(into html
    using xsl) with days on x axis and time on y asix. My xml structure is
    as follows:

    <TimetableItem>
    <CourseID>Math1005</CourseID>
    <Lesson>Lecture</Lesson>
    <Location>
    <Building>Copland</Building>
    <Room>Theatre</Room>
    </Location>
    <Day>Tuesday</Day>
    <Time>1200</Time>
    </TimetableItem>

    I tried to use for-each to format but no matter how i loop though, if
    i have two class on the same time but on different day(10:00 tuesday
    and 10:00 thursday) would have two rows of 10:00 showing up with the
    two classes.

    As people who have hd a timetable before would know its repetitive and
    does not form a regular timetable.

    :(

    HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ~Moon
    Thuan Seah, Sep 10, 2003
    #1
    1. Advertising

  2. Thuan Seah

    Martin Boehm Guest

    Thuan Seah wrote:

    Hi Thuan,

    I rewrote your XML Input a little, as you need a grid of some sorts to
    make up a at least a skeleton of a timetable:

    ---timetable.xml---------------------------------------------------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="timetable.xsl"?>
    <timetable>
    <grid>
    <week>
    <day id="mo">Monday</day>
    <day id="tu">Tuesday</day>
    <day id="we">Wednesday</day>
    <day id="th">Thursday</day>
    <day id="fr">Friday</day>
    </week>
    <day>
    <time id="8">0800</time>
    <time id="10">1000</time>
    <time id="12">1200</time>
    <time id="14">1400</time>
    <time id="16">1600</time>
    </day>
    </grid>
    <TimetableItem day="th" time="12">
    <CourseID>Math1005</CourseID>
    <Lesson>Lecture</Lesson>
    <Location>
    <Building>Copland</Building>
    <Room>Theatre</Room>
    </Location>
    </TimetableItem>
    <TimetableItem day="mo" time="12">
    <CourseID>Bla</CourseID>
    <Lesson>Lecture</Lesson>
    <Location>
    <Building>bldg1</Building>
    <Room>room1</Room>
    </Location>
    </TimetableItem>
    </timetable>
    -------------------------------------------------------------------

    The TimeTableItem changed obviously, now having attributes making it
    more easy to filter the right items for each grid-cell of the resulting
    table.

    This is the XSL file I wrote, using nested for-each loops to generate
    the HTML table:

    ---timetable.xsl---------------------------------------------------
    <?xml version="1.0"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="html">

    <xsl:template match="/timetable">
    <html>
    <head>
    <title>Timetable</title>
    </head>
    <body>
    <table border="1">
    <tr>
    <!-- The   represents the &nbsp; which is not defined in XML
    an en ENTITY, but is needed by IE to render empty td's
    correctly -->
    <!-- making the table header row -->
    <td> </td>
    <xsl:for-each select="grid/week/day">
    <td><xsl:value-of select="."/></td>
    </xsl:for-each>
    </tr>
    <!-- making the the content -->
    <xsl:for-each select="grid/day/time">
    <xsl:variable name="time" select="@id"/>
    <tr>
    <td><xsl:value-of select="."/></td>
    <xsl:for-each select="../../week/day">
    <xsl:variable name="day" select="@id"/>
    <td>
    <xsl:choose>
    <xsl:when test="count(/timetable/TimetableItem[@day=$day and
    @time=$time]) != 0">
    <xsl:apply-templates select="/timetable/TimetableItem[@day=$day
    and @time=$time]"/>
    </xsl:when>
    <xsl:eek:therwise>
      <!-- if there is nothing else, the &nbsp; must be
    written -->
    </xsl:eek:therwise>
    </xsl:choose>
    </td>
    </xsl:for-each>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="TimetableItem">
    <div><strong><xsl:value-of select="CourseID"/></strong></div>
    <div><xsl:value-of select="Lesson"/></div>
    <div>
    <em>
    <xsl:value-of select="Location/Building"/>,
    <xsl:value-of select="Location/Room"/>
    </em>
    </div>
    </xsl:template>

    </xsl:stylesheet>
    -------------------------------------------------------------------

    The XPath Expression
    /timetable/TimetableItem[@day=$day and @time=$time]
    selects exactly the one node for the day/time in question, or nothing.

    If there is nothing for the specific day/time, an non-breaking space is
    needed (at least for IE) to display the empty table cell. I used the
    count function to test for that, but maybe there is a more efficient
    way, as in my solution the same XPath expression is executed twice in
    case there is at least one course.

    But this is academic, as for such a small solution there is no
    performance impact one should worry about.

    I hope I could help you,

    Martin
    Martin Boehm, Sep 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gene Ellis
    Replies:
    5
    Views:
    910
    GIMME
    Apr 26, 2004
  2. Michael
    Replies:
    3
    Views:
    726
    GIMME
    Apr 26, 2004
  3. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    2
    Views:
    992
    George Bina
    May 24, 2006
  5. David

    XML and XSL translation

    David, Feb 25, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    452
    David
    Feb 25, 2007
Loading...

Share This Page