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

Thuan Seah

Usenet Archive
Joined
Sep 10, 2003
Messages
1
Reaction score
0
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
 

Martin Boehm

Usenet Archive
Joined
Sep 12, 2003
Messages
52
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top